import loguru: stage 2

This commit is contained in:
binary-husky
2024-09-11 08:18:01 +00:00
parent 17cd4f8210
commit 80acd9c875
6 changed files with 60 additions and 59 deletions

View File

@@ -83,25 +83,25 @@ def patch_and_restart(path):
import sys import sys
import time import time
import glob 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 using config_private, move origin config.py as config_private.py
if not os.path.exists('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子文件夹下找回旧版的程序。') '另外您可以随时在history子文件夹下找回旧版的程序。')
shutil.copyfile('config.py', 'config_private.py') shutil.copyfile('config.py', 'config_private.py')
path_new_version = glob.glob(path + '/*-master')[0] path_new_version = glob.glob(path + '/*-master')[0]
dir_util.copy_tree(path_new_version, './') dir_util.copy_tree(path_new_version, './')
print亮绿('代码已经更新即将更新pip包依赖……') log亮绿('代码已经更新即将更新pip包依赖……')
for i in reversed(range(5)): time.sleep(1); print(i) for i in reversed(range(5)): time.sleep(1); log亮绿(i)
try: try:
import subprocess import subprocess
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt']) subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt'])
except: except:
print亮红('pip包依赖安装出现问题需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') log亮红('pip包依赖安装出现问题需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')
print亮绿('更新完成您可以随时在history子文件夹下找回旧版的程序5s之后重启') log亮绿('更新完成您可以随时在history子文件夹下找回旧版的程序5s之后重启')
print亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。') log亮红('假如重启失败,您可能需要手动安装新增的依赖库 `python -m pip install -r requirements.txt`,然后在用常规的`python main.py`的方式启动。')
print(' ------------------------------ -----------------------------------') log亮绿(' ------------------------------ -----------------------------------')
for i in reversed(range(8)): time.sleep(1); print(i) for i in reversed(range(8)): time.sleep(1); log亮绿(i)
os.execl(sys.executable, sys.executable, *sys.argv) os.execl(sys.executable, sys.executable, *sys.argv)
@@ -136,9 +136,9 @@ def auto_update(raise_error=False):
current_version = f.read() current_version = f.read()
current_version = json.loads(current_version)['version'] current_version = json.loads(current_version)['version']
if (remote_version - current_version) >= 0.01-1e-5: if (remote_version - current_version) >= 0.01-1e-5:
from shared_utils.colorful import print亮黄 from shared_utils.colorful import log亮黄
print亮黄(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}{new_feature}') log亮黄(f'\n新版本可用。新版本:{remote_version},当前版本:{current_version}{new_feature}')
print('1Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n') logger.info('1Github更新地址:\nhttps://github.com/binary-husky/chatgpt_academic\n')
user_instruction = input('2是否一键更新代码Y+回车=确认,输入其他/无输入+回车=不更新)?') user_instruction = input('2是否一键更新代码Y+回车=确认,输入其他/无输入+回车=不更新)?')
if user_instruction in ['Y', 'y']: if user_instruction in ['Y', 'y']:
path = backup_and_download(current_version, remote_version) path = backup_and_download(current_version, remote_version)

View File

@@ -1,5 +1,6 @@
from toolbox import HotReload # HotReload 的意思是热更新,修改函数插件后,不需要重启程序,代码直接生效 from toolbox import HotReload # HotReload 的意思是热更新,修改函数插件后,不需要重启程序,代码直接生效
from toolbox import trimmed_format_exc from toolbox import trimmed_format_exc
from loguru import logger
def get_crazy_functions(): def get_crazy_functions():
@@ -429,8 +430,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
# try: # try:
# from crazy_functions.联网的ChatGPT import 连接网络回答问题 # from crazy_functions.联网的ChatGPT import 连接网络回答问题
@@ -460,8 +461,8 @@ def get_crazy_functions():
# } # }
# ) # )
# except: # except:
# print(trimmed_format_exc()) # logger.error(trimmed_format_exc())
# print("Load function plugin failed") # logger.error("Load function plugin failed")
try: try:
from crazy_functions.SourceCode_Analyse import 解析任意code项目 from crazy_functions.SourceCode_Analyse import 解析任意code项目
@@ -479,8 +480,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.询问多个大语言模型 import 同时问询_指定模型 from crazy_functions.询问多个大语言模型 import 同时问询_指定模型
@@ -498,8 +499,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
@@ -520,8 +521,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.数学动画生成manim import 动画生成 from crazy_functions.数学动画生成manim import 动画生成
@@ -538,8 +539,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.Markdown_Translate import Markdown翻译指定语言 from crazy_functions.Markdown_Translate import Markdown翻译指定语言
@@ -557,8 +558,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.知识库问答 import 知识库文件注入 from crazy_functions.知识库问答 import 知识库文件注入
@@ -576,8 +577,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.知识库问答 import 读取知识库作答 from crazy_functions.知识库问答 import 读取知识库作答
@@ -595,8 +596,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.交互功能函数模板 import 交互功能模板函数 from crazy_functions.交互功能函数模板 import 交互功能模板函数
@@ -612,8 +613,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
@@ -635,8 +636,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.批量翻译PDF文档_NOUGAT import 批量翻译PDF文档 from crazy_functions.批量翻译PDF文档_NOUGAT import 批量翻译PDF文档
@@ -652,8 +653,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.函数动态生成 import 函数动态生成 from crazy_functions.函数动态生成 import 函数动态生成
@@ -669,8 +670,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.多智能体 import 多智能体终端 from crazy_functions.多智能体 import 多智能体终端
@@ -686,8 +687,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
try: try:
from crazy_functions.互动小游戏 import 随机小游戏 from crazy_functions.互动小游戏 import 随机小游戏
@@ -703,8 +704,8 @@ def get_crazy_functions():
} }
) )
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())
print("Load function plugin failed") logger.error("Load function plugin failed")
# try: # try:
# from crazy_functions.高级功能函数模板 import 测试图表渲染 # from crazy_functions.高级功能函数模板 import 测试图表渲染
@@ -717,7 +718,7 @@ def get_crazy_functions():
# } # }
# }) # })
# except: # except:
# print(trimmed_format_exc()) # logger.error(trimmed_format_exc())
# print('Load function plugin failed') # print('Load function plugin failed')
# try: # try:

View File

@@ -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 os
import logging 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): 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: except:
# 【第三种情况】:其他错误:重试几次 # 【第三种情况】:其他错误:重试几次
tb_str = '```\n' + trimmed_format_exc() + '```' 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" mutable[0] += f"[Local Message] 警告,在执行过程中遭遇问题, Traceback\n\n{tb_str}\n\n"
if retry_op > 0: if retry_op > 0:
retry_op -= 1 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("检测到程序终止。") if detect_timeout(): raise RuntimeError("检测到程序终止。")
tb_str = '```\n' + trimmed_format_exc() + '```' 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" 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 len(mutable[index][0]) > 0: gpt_say += "此线程失败前收到的回答:\n\n" + mutable[index][0]
if retry_op > 0: if retry_op > 0:
@@ -378,7 +379,7 @@ def read_and_clean_pdf_text(fp):
import fitz, copy import fitz, copy
import re import re
import numpy as np import numpy as np
from shared_utils.colorful import print亮黄, print亮绿 # from shared_utils.colorful import print亮黄, print亮绿
fc = 0 # Index 0 文本 fc = 0 # Index 0 文本
fs = 1 # Index 1 字体 fs = 1 # Index 1 字体
fb = 2 # Index 2 框框 fb = 2 # Index 2 框框
@@ -603,7 +604,7 @@ class nougat_interface():
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
process.kill() process.kill()
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
print("Process timed out!") logger.error("Process timed out!")
return False return False
return True return True

View File

@@ -1,7 +1,6 @@
from toolbox import CatchException, update_ui from toolbox import CatchException, update_ui
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
@CatchException @CatchException
def 交互功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request): def 交互功能模板函数(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, user_request):
""" """

View File

@@ -61,7 +61,6 @@ def sprint亮紫(*kw):
def sprint亮靛(*kw): def sprint亮靛(*kw):
return "\033[1;36m"+' '.join(kw)+"\033[0m" return "\033[1;36m"+' '.join(kw)+"\033[0m"
def log红(*kw,**kargs): def log红(*kw,**kargs):
logger.opt(depth=1).info(sprint红(*kw)) logger.opt(depth=1).info(sprint红(*kw))
def log绿(*kw,**kargs): def log绿(*kw,**kargs):

View File

@@ -10,6 +10,7 @@ import shutil
import glob import glob
import logging import logging
import uuid import uuid
from loguru import logger
from functools import wraps from functools import wraps
from textwrap import dedent from textwrap import dedent
from shared_utils.config_loader import get_conf from shared_utils.config_loader import get_conf
@@ -673,7 +674,7 @@ def run_gradio_in_subpath(demo, auth, port, custom_path):
if path == "/": if path == "/":
return True return True
if len(path) == 0: if len(path) == 0:
print( logger.info(
"ilegal custom path: {}\npath must not be empty\ndeploy on root url".format( "ilegal custom path: {}\npath must not be empty\ndeploy on root url".format(
path path
) )
@@ -681,10 +682,10 @@ def run_gradio_in_subpath(demo, auth, port, custom_path):
return False return False
if path[0] == "/": if path[0] == "/":
if path[1] != "/": if path[1] != "/":
print("deploy on sub-path {}".format(path)) logger.info("deploy on sub-path {}".format(path))
return True return True
return False return False
print( logger.info(
"ilegal custom path: {}\npath should begin with '/'\ndeploy on root url".format( "ilegal custom path: {}\npath should begin with '/'\ndeploy on root url".format(
path path
) )
@@ -787,12 +788,12 @@ def zip_folder(source_folder, dest_folder, zip_name):
# Make sure the source folder exists # Make sure the source folder exists
if not os.path.exists(source_folder): if not os.path.exists(source_folder):
print(f"{source_folder} does not exist") logger.info(f"{source_folder} does not exist")
return return
# Make sure the destination folder exists # Make sure the destination folder exists
if not os.path.exists(dest_folder): if not os.path.exists(dest_folder):
print(f"{dest_folder} does not exist") logger.info(f"{dest_folder} does not exist")
return return
# Create the name for the zip file # 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))) os.rename(zip_file, pj(dest_folder, os.path.basename(zip_file)))
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): 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') output_str = output_str.rstrip('\n')
logging.info(f"[Response({uid})]\n{output_str}\n\n") logging.info(f"[Response({uid})]\n{output_str}\n\n")
except: except:
print(trimmed_format_exc()) logger.error(trimmed_format_exc())