diff --git a/check_proxy.py b/check_proxy.py index 69cd7a0a..b5ee17d3 100644 --- a/check_proxy.py +++ b/check_proxy.py @@ -83,25 +83,25 @@ def patch_and_restart(path): import sys import time import glob - from shared_utils.colorful import print亮黄, print亮绿, print亮红 + from shared_utils.colorful import log亮黄, log亮绿, log亮红 # if not using config_private, move origin config.py as config_private.py if not os.path.exists('config_private.py'): - print亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,', + log亮黄('由于您没有设置config_private.py私密配置,现将您的现有配置移动至config_private.py以防止配置丢失,', '另外您可以随时在history子文件夹下找回旧版的程序。') shutil.copyfile('config.py', 'config_private.py') path_new_version = glob.glob(path + '/*-master')[0] dir_util.copy_tree(path_new_version, './') - print亮绿('代码已经更新,即将更新pip包依赖……') - for i in reversed(range(5)): time.sleep(1); print(i) + log亮绿('代码已经更新,即将更新pip包依赖……') + for i in reversed(range(5)): time.sleep(1); log亮绿(i) try: import subprocess subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt']) except: - print亮红('pip包依赖安装出现问题,需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') - print亮绿('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启') - print亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') - print(' ------------------------------ -----------------------------------') - for i in reversed(range(8)): time.sleep(1); print(i) + log亮红('pip包依赖安装出现问题,需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') + log亮绿('更新完成,您可以随时在history子文件夹下找回旧版的程序,5s之后重启') + log亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') + log亮绿(' ------------------------------ -----------------------------------') + for i in reversed(range(8)): time.sleep(1); log亮绿(i) os.execl(sys.executable, sys.executable, *sys.argv) @@ -136,9 +136,9 @@ def auto_update(raise_error=False): current_version = f.read() current_version = json.loads(current_version)['version'] if (remote_version - current_version) >= 0.01-1e-5: - from shared_utils.colorful import print亮黄 - print亮黄(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}') - print('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n') + from shared_utils.colorful import log亮黄 + log亮黄(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}。{new_feature}') + logger.info('(1)Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n') user_instruction = input('(2)是否一键更新代码(Y+回车=确认,输入其他/无输入+回车=不更新)?') if user_instruction in ['Y', 'y']: path = backup_and_download(current_version, remote_version) diff --git a/crazy_functional.py b/crazy_functional.py index 31bb17c2..1ddd8c20 100644 --- a/crazy_functional.py +++ b/crazy_functional.py @@ -1,5 +1,6 @@ from toolbox import HotReload # HotReload 的意思是热更新,修改函数插件后,不需要重启程序,代码直接生效 from toolbox import trimmed_format_exc +from loguru import logger def get_crazy_functions(): @@ -429,8 +430,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") # try: # from crazy_functions.联网的ChatGPT import 连接网络回答问题 @@ -460,8 +461,8 @@ def get_crazy_functions(): # } # ) # except: - # print(trimmed_format_exc()) - # print("Load function plugin failed") + # logger.error(trimmed_format_exc()) + # logger.error("Load function plugin failed") try: from crazy_functions.SourceCode_Analyse import 解析任意code项目 @@ -479,8 +480,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.询问多个大语言模型 import 同时问询_指定模型 @@ -498,8 +499,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") @@ -520,8 +521,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.数学动画生成manim import 动画生成 @@ -538,8 +539,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.Markdown_Translate import Markdown翻译指定语言 @@ -557,8 +558,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.知识库问答 import 知识库文件注入 @@ -576,8 +577,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.知识库问答 import 读取知识库作答 @@ -595,8 +596,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.交互功能函数模板 import 交互功能模板函数 @@ -612,8 +613,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: @@ -635,8 +636,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.批量翻译PDF文档_NOUGAT import 批量翻译PDF文档 @@ -652,8 +653,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.函数动态生成 import 函数动态生成 @@ -669,8 +670,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.多智能体 import 多智能体终端 @@ -686,8 +687,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") try: from crazy_functions.互动小游戏 import 随机小游戏 @@ -703,8 +704,8 @@ def get_crazy_functions(): } ) except: - print(trimmed_format_exc()) - print("Load function plugin failed") + logger.error(trimmed_format_exc()) + logger.error("Load function plugin failed") # try: # from crazy_functions.高级功能函数模板 import 测试图表渲染 @@ -717,7 +718,7 @@ def get_crazy_functions(): # } # }) # except: - # print(trimmed_format_exc()) + # logger.error(trimmed_format_exc()) # print('Load function plugin failed') # try: diff --git a/crazy_functions/crazy_utils.py b/crazy_functions/crazy_utils.py index 710fd7f1..110debc1 100644 --- a/crazy_functions/crazy_utils.py +++ b/crazy_functions/crazy_utils.py @@ -1,8 +1,9 @@ -from toolbox import update_ui, get_conf, trimmed_format_exc, get_max_token, Singleton -from shared_utils.char_visual_effect import scolling_visual_effect -import threading import os import logging +import threading +from loguru import logger +from shared_utils.char_visual_effect import scolling_visual_effect +from toolbox import update_ui, get_conf, trimmed_format_exc, get_max_token, Singleton def input_clipping(inputs, history, max_token_limit, return_clip_flags=False): """ @@ -133,7 +134,7 @@ def request_gpt_model_in_new_thread_with_ui_alive( except: # 【第三种情况】:其他错误:重试几次 tb_str = '```\n' + trimmed_format_exc() + '```' - print(tb_str) + logger.error(tb_str) mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" if retry_op > 0: retry_op -= 1 @@ -283,7 +284,7 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency( # 【第三种情况】:其他错误 if detect_timeout(): raise RuntimeError("检测到程序终止。") tb_str = '```\n' + trimmed_format_exc() + '```' - print(tb_str) + logger.error(tb_str) gpt_say += f"[Local Message] 警告,线程{index}在执行过程中遭遇问题, Traceback:\n\n{tb_str}\n\n" if len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0] if retry_op > 0: @@ -378,7 +379,7 @@ def read_and_clean_pdf_text(fp): import fitz, copy import re import numpy as np - from shared_utils.colorful import print亮黄, print亮绿 + # from shared_utils.colorful import print亮黄, print亮绿 fc = 0 # Index 0 文本 fs = 1 # Index 1 字体 fb = 2 # Index 2 框框 @@ -603,7 +604,7 @@ class nougat_interface(): except subprocess.TimeoutExpired: process.kill() stdout, stderr = process.communicate() - print("Process timed out!") + logger.error("Process timed out!") return False return True diff --git a/crazy_functions/交互功能函数模板.py b/crazy_functions/交互功能函数模板.py index 4a8ae6f6..d0266046 100644 --- a/crazy_functions/交互功能函数模板.py +++ b/crazy_functions/交互功能函数模板.py @@ -1,7 +1,6 @@ from toolbox import CatchException, update_ui from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive - @CatchException def 交互功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request): """ diff --git a/shared_utils/colorful.py b/shared_utils/colorful.py index df0c9d26..b014c671 100644 --- a/shared_utils/colorful.py +++ b/shared_utils/colorful.py @@ -61,7 +61,6 @@ def sprint亮紫(*kw): def sprint亮靛(*kw): return "\033[1;36m"+' '.join(kw)+"\033[0m" - def log红(*kw,**kargs): logger.opt(depth=1).info(sprint红(*kw)) def log绿(*kw,**kargs): diff --git a/toolbox.py b/toolbox.py index 900cf234..ca2e7f6e 100644 --- a/toolbox.py +++ b/toolbox.py @@ -10,6 +10,7 @@ import shutil import glob import logging import uuid +from loguru import logger from functools import wraps from textwrap import dedent from shared_utils.config_loader import get_conf @@ -673,7 +674,7 @@ def run_gradio_in_subpath(demo, auth, port, custom_path): if path == "/": return True if len(path) == 0: - print( + logger.info( "ilegal custom path: {}\npath must not be empty\ndeploy on root url".format( path ) @@ -681,10 +682,10 @@ def run_gradio_in_subpath(demo, auth, port, custom_path): return False if path[0] == "/": if path[1] != "/": - print("deploy on sub-path {}".format(path)) + logger.info("deploy on sub-path {}".format(path)) return True return False - print( + logger.info( "ilegal custom path: {}\npath should begin with '/'\ndeploy on root url".format( path ) @@ -787,12 +788,12 @@ def zip_folder(source_folder, dest_folder, zip_name): # Make sure the source folder exists if not os.path.exists(source_folder): - print(f"{source_folder} does not exist") + logger.info(f"{source_folder} does not exist") return # Make sure the destination folder exists if not os.path.exists(dest_folder): - print(f"{dest_folder} does not exist") + logger.info(f"{dest_folder} does not exist") return # Create the name for the zip file @@ -811,7 +812,7 @@ def zip_folder(source_folder, dest_folder, zip_name): os.rename(zip_file, pj(dest_folder, os.path.basename(zip_file))) zip_file = pj(dest_folder, os.path.basename(zip_file)) - print(f"Zip file created at {zip_file}") + logger.info(f"Zip file created at {zip_file}") def zip_result(folder): @@ -1039,4 +1040,4 @@ def log_chat(llm_model: str, input_str: str, output_str: str): output_str = output_str.rstrip('\n') logging.info(f"[Response({uid})]\n{output_str}\n\n") except: - print(trimmed_format_exc()) + logger.error(trimmed_format_exc())