Spaces:
Running
Running
new version
Browse files- README.md +17 -15
- app.py +106 -34
- check_proxy.py +6 -4
- config.py +16 -3
- core_functional.py +9 -7
- crazy_functional.py +38 -15
- crazy_functions/Langchain知识库.py +2 -2
- crazy_functions/Latex输出PDF结果.py +6 -3
- crazy_functions/crazy_utils.py +8 -1
- crazy_functions/gen_fns/gen_fns_shared.py +70 -0
- crazy_functions/pdf_fns/parse_pdf.py +145 -4
- crazy_functions/函数动态生成.py +252 -0
- crazy_functions/批量翻译PDF文档_NOUGAT.py +7 -95
- crazy_functions/批量翻译PDF文档_多线程.py +15 -96
- crazy_functions/解析项目源代码.py +17 -0
- docker-compose.yml +78 -1
- docs/GithubAction+AllCapacity +2 -3
- docs/GithubAction+NoLocal+Latex +3 -0
- docs/translate_english.json +138 -2
- docs/translate_japanese.json +0 -1
- docs/translate_std.json +3 -1
- docs/translate_traditionalchinese.json +0 -1
- docs/use_azure.md +6 -0
- request_llm/bridge_all.py +19 -0
- request_llm/bridge_chatglm.py +11 -10
- request_llm/bridge_llama2.py +1 -1
- request_llm/requirements_chatglm.txt +0 -1
- request_llm/requirements_chatglm_onnx.txt +0 -1
- request_llm/requirements_jittorllms.txt +0 -1
- request_llm/requirements_moss.txt +0 -1
- requirements.txt +1 -1
- tests/test_plugins.py +5 -2
- tests/test_utils.py +1 -1
- themes/common.css +93 -0
- themes/common.js +89 -3
- themes/contrast.css +2 -2
- themes/contrast.py +3 -6
- themes/default.css +4 -4
- themes/default.py +2 -5
- themes/gradios.py +13 -7
- themes/green.py +2 -6
- themes/theme.py +18 -13
- toolbox.py +18 -5
- version +2 -2
README.md
CHANGED
@@ -113,9 +113,11 @@ cd gpt_academic
|
|
113 |
|
114 |
2. 配置API_KEY
|
115 |
|
116 |
-
在`config.py`中,配置API KEY等设置,[点击查看特殊网络环境设置方法](https://github.com/binary-husky/gpt_academic/issues/1) 。
|
117 |
|
118 |
-
|
|
|
|
|
119 |
|
120 |
|
121 |
3. 安装依赖
|
@@ -123,7 +125,7 @@ cd gpt_academic
|
|
123 |
# (选择I: 如熟悉python)(python版本3.9以上,越新越好),备注:使用官方pip源或者阿里pip源,临时换源方法:python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
124 |
python -m pip install -r requirements.txt
|
125 |
|
126 |
-
# (选择II:
|
127 |
conda create -n gptac_venv python=3.11 # 创建anaconda环境
|
128 |
conda activate gptac_venv # 激活anaconda环境
|
129 |
python -m pip install -r requirements.txt # 这个步骤和pip安装一样的步骤
|
@@ -161,26 +163,25 @@ python main.py
|
|
161 |
|
162 |
### 安装方法II:使用Docker
|
163 |
|
|
|
164 |
[![fullcapacity](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-all-capacity.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml)
|
165 |
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
167 |
[![basic](https://github.com/binary-husky/gpt_academic/actions/workflows/build-without-local-llms.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-without-local-llms.yml)
|
168 |
[![basiclatex](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-latex.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-latex.yml)
|
169 |
[![basicaudio](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml)
|
170 |
|
171 |
-
|
172 |
``` sh
|
173 |
-
|
174 |
-
|
175 |
-
nano config.py # 用任意文本编辑器编辑config.py, 配置 “Proxy”, “API_KEY” 以及 “WEB_PORT” (例如50923) 等
|
176 |
-
docker build -t gpt-academic . # 安装
|
177 |
-
|
178 |
-
#(最后一步-Linux操作系统)用`--net=host`更方便快捷
|
179 |
-
docker run --rm -it --net=host gpt-academic
|
180 |
-
#(最后一步-MacOS/Windows操作系统)只能用-p选项将容器上的端口(例如50923)暴露给主机上的端口
|
181 |
-
docker run --rm -it -e WEB_PORT=50923 -p 50923:50923 gpt-academic
|
182 |
```
|
183 |
-
|
|
|
184 |
|
185 |
2. ChatGPT + ChatGLM2 + MOSS + LLAMA2 + 通义千问(需要熟悉[Nvidia Docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian)运行时)
|
186 |
[![chatglm](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml)
|
@@ -321,6 +322,7 @@ Tip:不指定文件直接点击 `载入对话历史存档` 可以查看历史h
|
|
321 |
|
322 |
### II:版本:
|
323 |
- version 3.60(todo): 优化虚空终端,引入code interpreter和更多插件
|
|
|
324 |
- version 3.50: 使用自然语言调用本项目的所有函数插件(虚空终端),支持插件分类,改进UI,设计新主题
|
325 |
- version 3.49: 支持百度千帆平台和文心一言
|
326 |
- version 3.48: 支持阿里达摩院通义千问,上海AI-Lab书生,讯飞星火
|
|
|
113 |
|
114 |
2. 配置API_KEY
|
115 |
|
116 |
+
在`config.py`中,配置API KEY等设置,[点击查看特殊网络环境设置方法](https://github.com/binary-husky/gpt_academic/issues/1) 。[Wiki页面](https://github.com/binary-husky/gpt_academic/wiki/%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E8%AF%B4%E6%98%8E)。
|
117 |
|
118 |
+
「 程序会优先检查是否存在名为`config_private.py`的私密配置文件,并用其中的配置覆盖`config.py`的同名配置。如您能理解该读取逻辑,我们强烈建议您在`config.py`旁边创建一个名为`config_private.py`的新配置文件,并把`config.py`中的配置转移(复制)到`config_private.py`中(仅复制您修改过的配置条目即可)。 」
|
119 |
+
|
120 |
+
「 支持通过`环境变量`配置项目,环境变量的书写格式参考`docker-compose.yml`文件或者我们的[Wiki页面](https://github.com/binary-husky/gpt_academic/wiki/%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE%E8%AF%B4%E6%98%8E)。配置读取优先级: `环境变量` > `config_private.py` > `config.py`。 」
|
121 |
|
122 |
|
123 |
3. 安装依赖
|
|
|
125 |
# (选择I: 如熟悉python)(python版本3.9以上,越新越好),备注:使用官方pip源或者阿里pip源,临时换源方法:python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
|
126 |
python -m pip install -r requirements.txt
|
127 |
|
128 |
+
# (选择II: 使用Anaconda)步骤也是类似的 (https://www.bilibili.com/video/BV1rc411W7Dr):
|
129 |
conda create -n gptac_venv python=3.11 # 创建anaconda环境
|
130 |
conda activate gptac_venv # 激活anaconda环境
|
131 |
python -m pip install -r requirements.txt # 这个步骤和pip安装一样的步骤
|
|
|
163 |
|
164 |
### 安装方法II:使用Docker
|
165 |
|
166 |
+
0. 部署项目的全部能力(这个是包含cuda和latex的大型镜像。如果您网速慢、硬盘小或没有显卡,则不推荐使用这个,建议使用方案1)(需要熟悉[Nvidia Docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian)运行时)
|
167 |
[![fullcapacity](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-all-capacity.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml)
|
168 |
|
169 |
+
``` sh
|
170 |
+
# 修改docker-compose.yml,保留方案0并删除其他方案。修改docker-compose.yml中方案0的配置,参考其中注释即可
|
171 |
+
docker-compose up
|
172 |
+
```
|
173 |
+
|
174 |
+
1. 仅ChatGPT+文心一言+spark等在线模型(推荐大多数人选择)
|
175 |
[![basic](https://github.com/binary-husky/gpt_academic/actions/workflows/build-without-local-llms.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-without-local-llms.yml)
|
176 |
[![basiclatex](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-latex.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-latex.yml)
|
177 |
[![basicaudio](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-audio-assistant.yml)
|
178 |
|
|
|
179 |
``` sh
|
180 |
+
# 修改docker-compose.yml,保留方案1并删除其他方案。修改docker-compose.yml中方案1的配置,参考其中注释即可
|
181 |
+
docker-compose up
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
```
|
183 |
+
|
184 |
+
P.S. 如果需要依赖Latex的插件功能,请见Wiki。另外,您也可以直接使用方案4或者方案0获取Latex功能。
|
185 |
|
186 |
2. ChatGPT + ChatGLM2 + MOSS + LLAMA2 + 通义千问(需要熟悉[Nvidia Docker](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installing-on-ubuntu-and-debian)运行时)
|
187 |
[![chatglm](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml/badge.svg?branch=master)](https://github.com/binary-husky/gpt_academic/actions/workflows/build-with-chatglm.yml)
|
|
|
322 |
|
323 |
### II:版本:
|
324 |
- version 3.60(todo): 优化虚空终端,引入code interpreter和更多插件
|
325 |
+
- version 3.53: 支持动态选择不同界面主题,提高稳定性&解决多用户冲突问题
|
326 |
- version 3.50: 使用自然语言调用本项目的所有函数插件(虚空终端),支持插件分类,改进UI,设计新主题
|
327 |
- version 3.49: 支持百度千帆平台和文心一言
|
328 |
- version 3.48: 支持阿里达摩院通义千问,上海AI-Lab书生,讯飞星火
|
app.py
CHANGED
@@ -2,23 +2,35 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染
|
|
2 |
|
3 |
def main():
|
4 |
import subprocess, sys
|
5 |
-
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'gradio-
|
6 |
import gradio as gr
|
|
|
|
|
7 |
from request_llm.bridge_all import predict
|
8 |
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
|
9 |
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
|
10 |
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
11 |
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
|
12 |
-
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING')
|
|
|
13 |
|
14 |
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
15 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
16 |
from check_proxy import get_current_version
|
17 |
-
from themes.theme import adjust_theme, advanced_css, theme_declaration
|
|
|
18 |
initial_prompt = "Serve me as a writing and programming assistant."
|
19 |
title_html = f"<h1 align=\"center\">GPT 学术优化 {get_current_version()}</h1>{theme_declaration}"
|
20 |
-
description = "
|
21 |
-
description += "感谢热情的[开发者们❤️](https://github.com/binary-husky/gpt_academic/graphs/contributors)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# 问询记录, python 版本建议3.9+(越新越好)
|
24 |
import logging, uuid
|
@@ -61,6 +73,7 @@ def main():
|
|
61 |
with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
62 |
gr.HTML(title_html)
|
63 |
gr.HTML('''<center><a href="https://huggingface.co/spaces/qingxu98/gpt-academic?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>请您打开此页面后务必点击上方的“复制空间”(Duplicate Space)按钮!<font color="#FF00FF">使用时,先在输入框填入API-KEY然后回车。</font><br/>切忌在“复制空间”(Duplicate Space)之前填入API_KEY或进行提问,否则您的API_KEY将极可能被空间所有者攫取!<br/>支持任意数量的OpenAI的密钥和API2D的密钥共存,例如输入"OpenAI密钥1,API2D密钥2",然后提交,即可同时使用两种模型接口。</center>''')
|
|
|
64 |
cookies = gr.State(load_chat_cookies())
|
65 |
with gr_L1():
|
66 |
with gr_L2(scale=2, elem_id="gpt-chat"):
|
@@ -72,11 +85,11 @@ def main():
|
|
72 |
with gr.Row():
|
73 |
txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持OpenAI密钥和API2D密钥共存。").style(container=False)
|
74 |
with gr.Row():
|
75 |
-
submitBtn = gr.Button("提交", variant="primary")
|
76 |
with gr.Row():
|
77 |
-
resetBtn = gr.Button("重置", variant="secondary"); resetBtn.style(size="sm")
|
78 |
-
stopBtn = gr.Button("停止", variant="secondary"); stopBtn.style(size="sm")
|
79 |
-
clearBtn = gr.Button("清除", variant="secondary", visible=False); clearBtn.style(size="sm")
|
80 |
if ENABLE_AUDIO:
|
81 |
with gr.Row():
|
82 |
audio_mic = gr.Audio(source="microphone", type="numpy", streaming=True, show_label=False).style(container=False)
|
@@ -87,7 +100,7 @@ def main():
|
|
87 |
for k in functional:
|
88 |
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
89 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
90 |
-
functional[k]["Button"] = gr.Button(k, variant=variant)
|
91 |
functional[k]["Button"].style(size="sm")
|
92 |
with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
|
93 |
with gr.Row():
|
@@ -100,7 +113,9 @@ def main():
|
|
100 |
if not plugin.get("AsButton", True): continue
|
101 |
visible = True if match_group(plugin['Group'], DEFAULT_FN_GROUPS) else False
|
102 |
variant = plugins[k]["Color"] if "Color" in plugin else "secondary"
|
103 |
-
|
|
|
|
|
104 |
with gr.Row():
|
105 |
with gr.Accordion("更多函数插件", open=True):
|
106 |
dropdown_fn_list = []
|
@@ -117,15 +132,27 @@ def main():
|
|
117 |
switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary").style(size="sm")
|
118 |
with gr.Row():
|
119 |
with gr.Accordion("点击展开“文件上传区”。上传本地文件/压缩包供函数插件调用。", open=False) as area_file_up:
|
120 |
-
file_upload = gr.Files(label="任何文件,
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
124 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
125 |
-
max_length_sl = gr.Slider(minimum=256, maximum=
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
129 |
dark_mode_btn.click(None, None, None, _js="""() => {
|
130 |
if (document.querySelectorAll('.dark').length) {
|
131 |
document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
|
@@ -134,13 +161,17 @@ def main():
|
|
134 |
}
|
135 |
}""",
|
136 |
)
|
|
|
137 |
gr.Markdown(description)
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
with gr.
|
|
|
|
|
|
|
144 |
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
|
145 |
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
|
146 |
clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn2.style(size="sm")
|
@@ -150,12 +181,12 @@ def main():
|
|
150 |
ret = {}
|
151 |
ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
|
152 |
ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
|
153 |
-
ret.update({area_input_primary: gr.update(visible=("
|
154 |
-
ret.update({area_input_secondary: gr.update(visible=("
|
155 |
ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
|
156 |
ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
|
157 |
ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
|
158 |
-
if "
|
159 |
return ret
|
160 |
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
|
161 |
# 整理反复出现的控件句柄组合
|
@@ -183,6 +214,7 @@ def main():
|
|
183 |
cancel_handles.append(click_handle)
|
184 |
# 文件上传区,接收文件后与chatbot的互动
|
185 |
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes, cookies], [chatbot, txt, txt2, cookies])
|
|
|
186 |
# 函数插件-固定按钮区
|
187 |
for k in plugins:
|
188 |
if not plugins[k].get("AsButton", True): continue
|
@@ -192,16 +224,45 @@ def main():
|
|
192 |
# 函数插件-下拉菜单与随变按钮的互动
|
193 |
def on_dropdown_changed(k):
|
194 |
variant = plugins[k]["Color"] if "Color" in plugins[k] else "secondary"
|
195 |
-
|
|
|
196 |
if plugins[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
|
197 |
ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + plugins[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
|
198 |
else:
|
199 |
ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
|
200 |
return ret
|
201 |
dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
|
|
|
202 |
def on_md_dropdown_changed(k):
|
203 |
return {chatbot: gr.update(label="当前模型:"+k)}
|
204 |
md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
# 随变按钮的回调函数注册
|
206 |
def route(request: gr.Request, k, *args, **kwargs):
|
207 |
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
@@ -237,19 +298,30 @@ def main():
|
|
237 |
cookies.update({'uuid': uuid.uuid4()})
|
238 |
return cookies
|
239 |
demo.load(init_cookie, inputs=[cookies, chatbot], outputs=[cookies])
|
240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
243 |
def auto_opentab_delay():
|
244 |
import threading, webbrowser, time
|
245 |
print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
|
246 |
-
print(f"\t
|
247 |
-
print(f"\t
|
248 |
def open():
|
249 |
time.sleep(2) # 打开浏览器
|
250 |
-
|
251 |
-
if DARK_MODE: webbrowser.open_new_tab(f"http://localhost:{PORT}/?__theme=dark")
|
252 |
-
else: webbrowser.open_new_tab(f"http://localhost:{PORT}")
|
253 |
threading.Thread(target=open, name="open-browser", daemon=True).start()
|
254 |
threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
|
255 |
threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
|
|
|
2 |
|
3 |
def main():
|
4 |
import subprocess, sys
|
5 |
+
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'https://github.com/binary-husky/gpt_academic/raw/master/docs/gradio-3.32.6-py3-none-any.whl'])
|
6 |
import gradio as gr
|
7 |
+
if gr.__version__ not in ['3.32.6']:
|
8 |
+
raise ModuleNotFoundError("使用项目内置Gradio获取最优体验! 请运行 `pip install -r requirements.txt` 指令安装内置Gradio及其他依赖, 详情信息见requirements.txt.")
|
9 |
from request_llm.bridge_all import predict
|
10 |
from toolbox import format_io, find_free_port, on_file_uploaded, on_report_generated, get_conf, ArgsGeneralWrapper, load_chat_cookies, DummyWith
|
11 |
# 建议您复制一个config_private.py放自己的秘密, 如API和代理网址, 避免不小心传github被别人看到
|
12 |
proxies, WEB_PORT, LLM_MODEL, CONCURRENT_COUNT, AUTHENTICATION = get_conf('proxies', 'WEB_PORT', 'LLM_MODEL', 'CONCURRENT_COUNT', 'AUTHENTICATION')
|
13 |
CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT')
|
14 |
+
ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME')
|
15 |
+
DARK_MODE, = get_conf('DARK_MODE')
|
16 |
|
17 |
# 如果WEB_PORT是-1, 则随机选取WEB端口
|
18 |
PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT
|
19 |
from check_proxy import get_current_version
|
20 |
+
from themes.theme import adjust_theme, advanced_css, theme_declaration, load_dynamic_theme
|
21 |
+
|
22 |
initial_prompt = "Serve me as a writing and programming assistant."
|
23 |
title_html = f"<h1 align=\"center\">GPT 学术优化 {get_current_version()}</h1>{theme_declaration}"
|
24 |
+
description = "Github源代码开源和更新[地址🚀](https://github.com/binary-husky/gpt_academic), "
|
25 |
+
description += "感谢热情的[开发者们❤️](https://github.com/binary-husky/gpt_academic/graphs/contributors)."
|
26 |
+
description += "</br></br>常见问题请查阅[项目Wiki](https://github.com/binary-husky/gpt_academic/wiki), "
|
27 |
+
description += "如遇到Bug请前往[Bug反馈](https://github.com/binary-husky/gpt_academic/issues)."
|
28 |
+
description += "</br></br>普通对话使用说明: 1. 输入问题; 2. 点击提交"
|
29 |
+
description += "</br></br>基础功能区使用说明: 1. 输入文本; 2. 点击任意基础功能区按钮"
|
30 |
+
description += "</br></br>函数插件区使用说明: 1. 输入路径/问题, 或者上传文件; 2. 点击任意函数插件区按钮"
|
31 |
+
description += "</br></br>虚空终端使用说明: 点击虚空终端, 然后根据提示输入指令, 再次点击虚空终端"
|
32 |
+
description += "</br></br>如何保存对话: 点击保存当前的对话按钮"
|
33 |
+
description += "</br></br>如何语音对话: 请阅读Wiki"
|
34 |
|
35 |
# 问询记录, python 版本建议3.9+(越新越好)
|
36 |
import logging, uuid
|
|
|
73 |
with gr.Blocks(title="GPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo:
|
74 |
gr.HTML(title_html)
|
75 |
gr.HTML('''<center><a href="https://huggingface.co/spaces/qingxu98/gpt-academic?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>请您打开此页面后务必点击上方的“复制空间”(Duplicate Space)按钮!<font color="#FF00FF">使用时,先在输入框填入API-KEY然后回车。</font><br/>切忌在“复制空间”(Duplicate Space)之前填入API_KEY或进行提问,否则您的API_KEY将极可能被空间所有者攫取!<br/>支持任意数量的OpenAI的密钥和API2D的密钥共存,例如输入"OpenAI密钥1,API2D密钥2",然后提交,即可同时使用两种模型接口。</center>''')
|
76 |
+
secret_css, dark_mode = gr.Textbox(visible=False), gr.Textbox(DARK_MODE, visible=False)
|
77 |
cookies = gr.State(load_chat_cookies())
|
78 |
with gr_L1():
|
79 |
with gr_L2(scale=2, elem_id="gpt-chat"):
|
|
|
85 |
with gr.Row():
|
86 |
txt = gr.Textbox(show_label=False, lines=2, placeholder="输入问题或API密钥,输入多个密钥时,用英文逗号间隔。支持OpenAI密钥和API2D密钥共存。").style(container=False)
|
87 |
with gr.Row():
|
88 |
+
submitBtn = gr.Button("提交", elem_id="elem_submit", variant="primary")
|
89 |
with gr.Row():
|
90 |
+
resetBtn = gr.Button("重置", elem_id="elem_reset", variant="secondary"); resetBtn.style(size="sm")
|
91 |
+
stopBtn = gr.Button("停止", elem_id="elem_stop", variant="secondary"); stopBtn.style(size="sm")
|
92 |
+
clearBtn = gr.Button("清除", elem_id="elem_clear", variant="secondary", visible=False); clearBtn.style(size="sm")
|
93 |
if ENABLE_AUDIO:
|
94 |
with gr.Row():
|
95 |
audio_mic = gr.Audio(source="microphone", type="numpy", streaming=True, show_label=False).style(container=False)
|
|
|
100 |
for k in functional:
|
101 |
if ("Visible" in functional[k]) and (not functional[k]["Visible"]): continue
|
102 |
variant = functional[k]["Color"] if "Color" in functional[k] else "secondary"
|
103 |
+
functional[k]["Button"] = gr.Button(k, variant=variant, info_str=f'基础功能区: {k}')
|
104 |
functional[k]["Button"].style(size="sm")
|
105 |
with gr.Accordion("函数插件区", open=True, elem_id="plugin-panel") as area_crazy_fn:
|
106 |
with gr.Row():
|
|
|
113 |
if not plugin.get("AsButton", True): continue
|
114 |
visible = True if match_group(plugin['Group'], DEFAULT_FN_GROUPS) else False
|
115 |
variant = plugins[k]["Color"] if "Color" in plugin else "secondary"
|
116 |
+
info = plugins[k].get("Info", k)
|
117 |
+
plugin['Button'] = plugins[k]['Button'] = gr.Button(k, variant=variant,
|
118 |
+
visible=visible, info_str=f'函数插件区: {info}').style(size="sm")
|
119 |
with gr.Row():
|
120 |
with gr.Accordion("更多函数插件", open=True):
|
121 |
dropdown_fn_list = []
|
|
|
132 |
switchy_bt = gr.Button(r"请先从插件列表中选择", variant="secondary").style(size="sm")
|
133 |
with gr.Row():
|
134 |
with gr.Accordion("点击展开“文件上传区”。上传本地文件/压缩包供函数插件调用。", open=False) as area_file_up:
|
135 |
+
file_upload = gr.Files(label="任何文件, 推荐上传压缩文件(zip, tar)", file_count="multiple", elem_id="elem_upload")
|
136 |
+
|
137 |
+
|
138 |
+
with gr.Floating(init_x="0%", init_y="0%", visible=True, width=None, drag="forbidden"):
|
139 |
+
with gr.Row():
|
140 |
+
with gr.Tab("上传文件", elem_id="interact-panel"):
|
141 |
+
gr.Markdown("请上传本地文件/压缩包供“函数插件区”功能调用。请注意: 上传文件后会自动把输入区修改为相应路径。")
|
142 |
+
file_upload_2 = gr.Files(label="任何文件, 推荐上传压缩文件(zip, tar)", file_count="multiple")
|
143 |
+
|
144 |
+
with gr.Tab("更换模型 & Prompt", elem_id="interact-panel"):
|
145 |
+
md_dropdown = gr.Dropdown(AVAIL_LLM_MODELS, value=LLM_MODEL, label="更换LLM模型/请求源").style(container=False)
|
146 |
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",)
|
147 |
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
148 |
+
max_length_sl = gr.Slider(minimum=256, maximum=1024*32, value=4096, step=128, interactive=True, label="Local LLM MaxLength",)
|
149 |
+
system_prompt = gr.Textbox(show_label=True, lines=2, placeholder=f"System Prompt", label="System prompt", value=initial_prompt)
|
150 |
+
|
151 |
+
with gr.Tab("界面外观", elem_id="interact-panel"):
|
152 |
+
theme_dropdown = gr.Dropdown(AVAIL_THEMES, value=THEME, label="更换UI主题").style(container=False)
|
153 |
+
checkboxes = gr.CheckboxGroup(["基础功能区", "函数插件区", "浮动输入区", "输入清除键", "插件参数区"],
|
154 |
+
value=["基础功能区", "函数插件区"], label="显示/隐藏功能区", elem_id='cbs').style(container=False)
|
155 |
+
dark_mode_btn = gr.Button("切换界面明暗 ☀", variant="secondary").style(size="sm")
|
156 |
dark_mode_btn.click(None, None, None, _js="""() => {
|
157 |
if (document.querySelectorAll('.dark').length) {
|
158 |
document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
|
|
|
161 |
}
|
162 |
}""",
|
163 |
)
|
164 |
+
with gr.Tab("帮助", elem_id="interact-panel"):
|
165 |
gr.Markdown(description)
|
166 |
+
|
167 |
+
with gr.Floating(init_x="20%", init_y="50%", visible=False, width="40%", drag="top") as area_input_secondary:
|
168 |
+
with gr.Accordion("浮动输入区", open=True, elem_id="input-panel2"):
|
169 |
+
with gr.Row() as row:
|
170 |
+
row.style(equal_height=True)
|
171 |
+
with gr.Column(scale=10):
|
172 |
+
txt2 = gr.Textbox(show_label=False, placeholder="Input question here.", lines=8, label="输入区2").style(container=False)
|
173 |
+
with gr.Column(scale=1, min_width=40):
|
174 |
+
submitBtn2 = gr.Button("提交", variant="primary"); submitBtn2.style(size="sm")
|
175 |
resetBtn2 = gr.Button("重置", variant="secondary"); resetBtn2.style(size="sm")
|
176 |
stopBtn2 = gr.Button("停止", variant="secondary"); stopBtn2.style(size="sm")
|
177 |
clearBtn2 = gr.Button("清除", variant="secondary", visible=False); clearBtn2.style(size="sm")
|
|
|
181 |
ret = {}
|
182 |
ret.update({area_basic_fn: gr.update(visible=("基础功能区" in a))})
|
183 |
ret.update({area_crazy_fn: gr.update(visible=("函数插件区" in a))})
|
184 |
+
ret.update({area_input_primary: gr.update(visible=("浮动输入区" not in a))})
|
185 |
+
ret.update({area_input_secondary: gr.update(visible=("浮动输入区" in a))})
|
186 |
ret.update({clearBtn: gr.update(visible=("输入清除键" in a))})
|
187 |
ret.update({clearBtn2: gr.update(visible=("输入清除键" in a))})
|
188 |
ret.update({plugin_advanced_arg: gr.update(visible=("插件参数区" in a))})
|
189 |
+
if "浮动输入区" in a: ret.update({txt: gr.update(value="")})
|
190 |
return ret
|
191 |
checkboxes.select(fn_area_visibility, [checkboxes], [area_basic_fn, area_crazy_fn, area_input_primary, area_input_secondary, txt, txt2, clearBtn, clearBtn2, plugin_advanced_arg] )
|
192 |
# 整理反复出现的控件句柄组合
|
|
|
214 |
cancel_handles.append(click_handle)
|
215 |
# 文件上传区,接收文件后与chatbot的互动
|
216 |
file_upload.upload(on_file_uploaded, [file_upload, chatbot, txt, txt2, checkboxes, cookies], [chatbot, txt, txt2, cookies])
|
217 |
+
file_upload_2.upload(on_file_uploaded, [file_upload_2, chatbot, txt, txt2, checkboxes, cookies], [chatbot, txt, txt2, cookies])
|
218 |
# 函数插件-固定按钮区
|
219 |
for k in plugins:
|
220 |
if not plugins[k].get("AsButton", True): continue
|
|
|
224 |
# 函数插件-下拉菜单与随变按钮的互动
|
225 |
def on_dropdown_changed(k):
|
226 |
variant = plugins[k]["Color"] if "Color" in plugins[k] else "secondary"
|
227 |
+
info = plugins[k].get("Info", k)
|
228 |
+
ret = {switchy_bt: gr.update(value=k, variant=variant, info_str=f'函数插件区: {info}')}
|
229 |
if plugins[k].get("AdvancedArgs", False): # 是否唤起高级插件参数区
|
230 |
ret.update({plugin_advanced_arg: gr.update(visible=True, label=f"插件[{k}]的高级参数说明:" + plugins[k].get("ArgsReminder", [f"没有提供高级参数功能说明"]))})
|
231 |
else:
|
232 |
ret.update({plugin_advanced_arg: gr.update(visible=False, label=f"插件[{k}]不需要高级参数。")})
|
233 |
return ret
|
234 |
dropdown.select(on_dropdown_changed, [dropdown], [switchy_bt, plugin_advanced_arg] )
|
235 |
+
|
236 |
def on_md_dropdown_changed(k):
|
237 |
return {chatbot: gr.update(label="当前模型:"+k)}
|
238 |
md_dropdown.select(on_md_dropdown_changed, [md_dropdown], [chatbot] )
|
239 |
+
|
240 |
+
def on_theme_dropdown_changed(theme, secret_css):
|
241 |
+
adjust_theme, css_part1, _, adjust_dynamic_theme = load_dynamic_theme(theme)
|
242 |
+
if adjust_dynamic_theme:
|
243 |
+
css_part2 = adjust_dynamic_theme._get_theme_css()
|
244 |
+
else:
|
245 |
+
css_part2 = adjust_theme()._get_theme_css()
|
246 |
+
return css_part2 + css_part1
|
247 |
+
|
248 |
+
theme_handle = theme_dropdown.select(on_theme_dropdown_changed, [theme_dropdown, secret_css], [secret_css])
|
249 |
+
theme_handle.then(
|
250 |
+
None,
|
251 |
+
[secret_css],
|
252 |
+
None,
|
253 |
+
_js="""(css) => {
|
254 |
+
var existingStyles = document.querySelectorAll("style[data-loaded-css]");
|
255 |
+
for (var i = 0; i < existingStyles.length; i++) {
|
256 |
+
var style = existingStyles[i];
|
257 |
+
style.parentNode.removeChild(style);
|
258 |
+
}
|
259 |
+
var styleElement = document.createElement('style');
|
260 |
+
styleElement.setAttribute('data-loaded-css', css);
|
261 |
+
styleElement.innerHTML = css;
|
262 |
+
document.head.appendChild(styleElement);
|
263 |
+
}
|
264 |
+
"""
|
265 |
+
)
|
266 |
# 随变按钮的回调函数注册
|
267 |
def route(request: gr.Request, k, *args, **kwargs):
|
268 |
if k in [r"打开插件列表", r"请先从插件列表中选择"]: return
|
|
|
298 |
cookies.update({'uuid': uuid.uuid4()})
|
299 |
return cookies
|
300 |
demo.load(init_cookie, inputs=[cookies, chatbot], outputs=[cookies])
|
301 |
+
darkmode_js = """(dark) => {
|
302 |
+
dark = dark == "True";
|
303 |
+
if (document.querySelectorAll('.dark').length) {
|
304 |
+
if (!dark){
|
305 |
+
document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark'));
|
306 |
+
}
|
307 |
+
} else {
|
308 |
+
if (dark){
|
309 |
+
document.querySelector('body').classList.add('dark');
|
310 |
+
}
|
311 |
+
}
|
312 |
+
}"""
|
313 |
+
demo.load(None, inputs=[dark_mode], outputs=None, _js=darkmode_js) # 配置暗色主题或亮色主题
|
314 |
+
demo.load(None, inputs=[gr.Textbox(LAYOUT, visible=False)], outputs=None, _js='(LAYOUT)=>{GptAcademicJavaScriptInit(LAYOUT);}')
|
315 |
|
316 |
# gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数
|
317 |
def auto_opentab_delay():
|
318 |
import threading, webbrowser, time
|
319 |
print(f"如果浏览器没有自动打开,请复制并转到以下URL:")
|
320 |
+
if DARK_MODE: print(f"\t「暗色主题已启用(支持动态切换主题)」: http://localhost:{PORT}")
|
321 |
+
else: print(f"\t「亮色主题已启用(支持动态切换主题)」: http://localhost:{PORT}")
|
322 |
def open():
|
323 |
time.sleep(2) # 打开浏览器
|
324 |
+
webbrowser.open_new_tab(f"http://localhost:{PORT}")
|
|
|
|
|
325 |
threading.Thread(target=open, name="open-browser", daemon=True).start()
|
326 |
threading.Thread(target=auto_update, name="self-upgrade", daemon=True).start()
|
327 |
threading.Thread(target=warm_up_modules, name="warm-up", daemon=True).start()
|
check_proxy.py
CHANGED
@@ -155,11 +155,13 @@ def auto_update(raise_error=False):
|
|
155 |
|
156 |
def warm_up_modules():
|
157 |
print('正在执行一些模块的预热...')
|
|
|
158 |
from request_llm.bridge_all import model_info
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
163 |
|
164 |
if __name__ == '__main__':
|
165 |
import os
|
|
|
155 |
|
156 |
def warm_up_modules():
|
157 |
print('正在执行一些模块的预热...')
|
158 |
+
from toolbox import ProxyNetworkActivate
|
159 |
from request_llm.bridge_all import model_info
|
160 |
+
with ProxyNetworkActivate("Warmup_Modules"):
|
161 |
+
enc = model_info["gpt-3.5-turbo"]['tokenizer']
|
162 |
+
enc.encode("模块预热", disallowed_special=())
|
163 |
+
enc = model_info["gpt-4"]['tokenizer']
|
164 |
+
enc.encode("模块预热", disallowed_special=())
|
165 |
|
166 |
if __name__ == '__main__':
|
167 |
import os
|
config.py
CHANGED
@@ -50,6 +50,7 @@ DEFAULT_WORKER_NUM = 3
|
|
50 |
# 色彩主题, 可选 ["Default", "Chuanhu-Small-and-Beautiful", "High-Contrast"]
|
51 |
# 更多主题, 请查阅Gradio主题商店: https://huggingface.co/spaces/gradio/theme-gallery 可选 ["Gstaff/Xkcd", "NoCrypt/Miku", ...]
|
52 |
THEME = "Chuanhu-Small-and-Beautiful"
|
|
|
53 |
|
54 |
|
55 |
# 对话窗的高度 (仅在LAYOUT="TOP-DOWN"时生效)
|
@@ -62,7 +63,10 @@ CODE_HIGHLIGHT = True
|
|
62 |
|
63 |
# 窗口布局
|
64 |
LAYOUT = "LEFT-RIGHT" # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局)
|
65 |
-
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
# 发送请求到OpenAI后,等待多久判定为超时
|
@@ -81,13 +85,13 @@ LLM_MODEL = "gpt-3.5-turbo" # 可选 "chatglm"
|
|
81 |
AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo", "spark", "azure-gpt-3.5"]
|
82 |
|
83 |
# 插件分类默认选项
|
84 |
-
DEFAULT_FN_GROUPS = ['对话', '编程', '学术']
|
85 |
|
86 |
|
87 |
# 模型选择是 (注意: LLM_MODEL是默认选中的模型, 它*必须*被包含在AVAIL_LLM_MODELS列表中 )
|
88 |
LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓
|
89 |
AVAIL_LLM_MODELS = ["gpt-3.5-turbo-16k", "gpt-3.5-turbo", "azure-gpt-3.5", "api2d-gpt-3.5-turbo",
|
90 |
-
"gpt-4", "api2d-gpt-4", "chatglm", "moss", "newbing", "stack-claude"]
|
91 |
# P.S. 其他可用的模型还包括 ["qianfan", "llama2", "qwen", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613",
|
92 |
# "spark", "sparkv2", "chatglm_onnx", "claude-1-100k", "claude-2", "internlm", "jittorllms_pangualpha", "jittorllms_llama"]
|
93 |
|
@@ -186,11 +190,20 @@ GROBID_URLS = [
|
|
186 |
|
187 |
# 是否允许通过自然语言描述修改本页的配置,该功能具有一定的危险性,默认关闭
|
188 |
ALLOW_RESET_CONFIG = False
|
|
|
|
|
189 |
# 临时的上传文件夹位置,请勿修改
|
190 |
PATH_PRIVATE_UPLOAD = "private_upload"
|
|
|
|
|
191 |
# 日志文件夹的位置,请勿修改
|
192 |
PATH_LOGGING = "gpt_log"
|
193 |
|
|
|
|
|
|
|
|
|
|
|
194 |
"""
|
195 |
在线大模型配置关联关系示意图
|
196 |
│
|
|
|
50 |
# 色彩主题, 可选 ["Default", "Chuanhu-Small-and-Beautiful", "High-Contrast"]
|
51 |
# 更多主题, 请查阅Gradio主题商店: https://huggingface.co/spaces/gradio/theme-gallery 可选 ["Gstaff/Xkcd", "NoCrypt/Miku", ...]
|
52 |
THEME = "Chuanhu-Small-and-Beautiful"
|
53 |
+
AVAIL_THEMES = ["Default", "Chuanhu-Small-and-Beautiful", "High-Contrast", "Gstaff/Xkcd", "NoCrypt/Miku"]
|
54 |
|
55 |
|
56 |
# 对话窗的高度 (仅在LAYOUT="TOP-DOWN"时生效)
|
|
|
63 |
|
64 |
# 窗口布局
|
65 |
LAYOUT = "LEFT-RIGHT" # "LEFT-RIGHT"(左右布局) # "TOP-DOWN"(上下布局)
|
66 |
+
|
67 |
+
|
68 |
+
# 暗色模式 / 亮色模式
|
69 |
+
DARK_MODE = True
|
70 |
|
71 |
|
72 |
# 发送请求到OpenAI后,等待多久判定为超时
|
|
|
85 |
AVAIL_LLM_MODELS = ["gpt-3.5-turbo", "gpt-4", "api2d-gpt-4", "api2d-gpt-3.5-turbo", "spark", "azure-gpt-3.5"]
|
86 |
|
87 |
# 插件分类默认选项
|
88 |
+
DEFAULT_FN_GROUPS = ['对话', '编程', '学术', '智能体']
|
89 |
|
90 |
|
91 |
# 模型选择是 (注意: LLM_MODEL是默认选中的模型, 它*必须*被包含在AVAIL_LLM_MODELS列表中 )
|
92 |
LLM_MODEL = "gpt-3.5-turbo" # 可选 ↓↓↓
|
93 |
AVAIL_LLM_MODELS = ["gpt-3.5-turbo-16k", "gpt-3.5-turbo", "azure-gpt-3.5", "api2d-gpt-3.5-turbo",
|
94 |
+
"gpt-4", "gpt-4-32k", "azure-gpt-4", "api2d-gpt-4", "chatglm", "moss", "newbing", "stack-claude"]
|
95 |
# P.S. 其他可用的模型还包括 ["qianfan", "llama2", "qwen", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613",
|
96 |
# "spark", "sparkv2", "chatglm_onnx", "claude-1-100k", "claude-2", "internlm", "jittorllms_pangualpha", "jittorllms_llama"]
|
97 |
|
|
|
190 |
|
191 |
# 是否允许通过自然语言描述修改本页的配置,该功能具有一定的危险性,默认关闭
|
192 |
ALLOW_RESET_CONFIG = False
|
193 |
+
|
194 |
+
|
195 |
# 临时的上传文件夹位置,请勿修改
|
196 |
PATH_PRIVATE_UPLOAD = "private_upload"
|
197 |
+
|
198 |
+
|
199 |
# 日志文件夹的位置,请勿修改
|
200 |
PATH_LOGGING = "gpt_log"
|
201 |
|
202 |
+
|
203 |
+
# 除了连接OpenAI之外,还有哪些场合允许使用代理,请勿修改
|
204 |
+
WHEN_TO_USE_PROXY = ["Download_LLM", "Download_Gradio_Theme", "Connect_Grobid", "Warmup_Modules"]
|
205 |
+
|
206 |
+
|
207 |
"""
|
208 |
在线大模型配置关联关系示意图
|
209 |
│
|
core_functional.py
CHANGED
@@ -11,7 +11,8 @@ def get_core_functions():
|
|
11 |
# 前缀,会被加在你的输入之前。例如,用来描述你的要求,例如翻译、解释代码、润色等等
|
12 |
"Prefix": r"Below is a paragraph from an academic paper. Polish the writing to meet the academic style, " +
|
13 |
r"improve the spelling, grammar, clarity, concision and overall readability. When necessary, rewrite the whole sentence. " +
|
14 |
-
r"
|
|
|
15 |
# 后缀,会被加在你的输入之后。例如,配合前缀可以把你的输入内容用引号圈起来
|
16 |
"Suffix": r"",
|
17 |
# 按钮颜色 (默认 secondary)
|
@@ -27,17 +28,18 @@ def get_core_functions():
|
|
27 |
"Suffix": r"",
|
28 |
},
|
29 |
"查找语法错误": {
|
30 |
-
"Prefix": r"
|
31 |
-
r"Do not try to polish the text, if no mistake is found, tell me that this paragraph is good."
|
32 |
-
r"If you find grammar or spelling mistakes, please list mistakes you find in a two-column markdown table, "
|
33 |
-
r"put the original text the first column, "
|
34 |
-
r"put the corrected text in the second column and highlight the key words you fixed."
|
|
|
35 |
r"Example:""\n"
|
36 |
r"Paragraph: How is you? Do you knows what is it?""\n"
|
37 |
r"| Original sentence | Corrected sentence |""\n"
|
38 |
r"| :--- | :--- |""\n"
|
39 |
r"| How **is** you? | How **are** you? |""\n"
|
40 |
-
r"| Do you **knows** what **is** **it**? | Do you **know** what **it** **is** ? |""\n"
|
41 |
r"Below is a paragraph from an academic paper. "
|
42 |
r"You need to report all grammar and spelling mistakes as the example before."
|
43 |
+ "\n\n",
|
|
|
11 |
# 前缀,会被加在你的输入之前。例如,用来描述你的要求,例如翻译、解释代码、润色等等
|
12 |
"Prefix": r"Below is a paragraph from an academic paper. Polish the writing to meet the academic style, " +
|
13 |
r"improve the spelling, grammar, clarity, concision and overall readability. When necessary, rewrite the whole sentence. " +
|
14 |
+
r"Firstly, you should provide the polished paragraph. "
|
15 |
+
r"Secondly, you should list all your modification and explain the reasons to do so in markdown table." + "\n\n",
|
16 |
# 后缀,会被加在你的输入之后。例如,配合前缀可以把你的输入内容用引号圈起来
|
17 |
"Suffix": r"",
|
18 |
# 按钮颜色 (默认 secondary)
|
|
|
28 |
"Suffix": r"",
|
29 |
},
|
30 |
"查找语法错误": {
|
31 |
+
"Prefix": r"Help me ensure that the grammar and the spelling is correct. "
|
32 |
+
r"Do not try to polish the text, if no mistake is found, tell me that this paragraph is good. "
|
33 |
+
r"If you find grammar or spelling mistakes, please list mistakes you find in a two-column markdown table, "
|
34 |
+
r"put the original text the first column, "
|
35 |
+
r"put the corrected text in the second column and highlight the key words you fixed. "
|
36 |
+
r"Finally, please provide the proofreaded text.""\n\n"
|
37 |
r"Example:""\n"
|
38 |
r"Paragraph: How is you? Do you knows what is it?""\n"
|
39 |
r"| Original sentence | Corrected sentence |""\n"
|
40 |
r"| :--- | :--- |""\n"
|
41 |
r"| How **is** you? | How **are** you? |""\n"
|
42 |
+
r"| Do you **knows** what **is** **it**? | Do you **know** what **it** **is** ? |""\n\n"
|
43 |
r"Below is a paragraph from an academic paper. "
|
44 |
r"You need to report all grammar and spelling mistakes as the example before."
|
45 |
+ "\n\n",
|
crazy_functional.py
CHANGED
@@ -6,6 +6,7 @@ def get_crazy_functions():
|
|
6 |
from crazy_functions.生成函数注释 import 批量生成函数注释
|
7 |
from crazy_functions.解析项目源代码 import 解析项目本身
|
8 |
from crazy_functions.解析项目源代码 import 解析一个Python项目
|
|
|
9 |
from crazy_functions.解析项目源代码 import 解析一个C项目的头文件
|
10 |
from crazy_functions.解析项目源代码 import 解析一个C项目
|
11 |
from crazy_functions.解析项目源代码 import 解析一个Golang项目
|
@@ -38,7 +39,7 @@ def get_crazy_functions():
|
|
38 |
|
39 |
function_plugins = {
|
40 |
"虚空终端": {
|
41 |
-
"Group": "
|
42 |
"Color": "stop",
|
43 |
"AsButton": True,
|
44 |
"Function": HotReload(虚空终端)
|
@@ -77,6 +78,13 @@ def get_crazy_functions():
|
|
77 |
"Info": "批量总结word文档 | 输入参数为路径",
|
78 |
"Function": HotReload(总结word文档)
|
79 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
"解析整个C++项目头文件": {
|
81 |
"Group": "编程",
|
82 |
"Color": "stop",
|
@@ -243,20 +251,23 @@ def get_crazy_functions():
|
|
243 |
"Info": "对中文Latex项目全文进行润色处理 | 输入参数为路径或上传压缩包",
|
244 |
"Function": HotReload(Latex中文润色)
|
245 |
},
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
"
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
260 |
"批量Markdown中译英(输入路径或上传压缩包)": {
|
261 |
"Group": "编程",
|
262 |
"Color": "stop",
|
@@ -513,6 +524,18 @@ def get_crazy_functions():
|
|
513 |
except:
|
514 |
print('Load function plugin failed')
|
515 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
|
517 |
# try:
|
518 |
# from crazy_functions.CodeInterpreter import 虚空终端CodeInterpreter
|
|
|
6 |
from crazy_functions.生成函数注释 import 批量生成函数注释
|
7 |
from crazy_functions.解析项目源代码 import 解析项目本身
|
8 |
from crazy_functions.解析项目源代码 import 解析一个Python项目
|
9 |
+
from crazy_functions.解析项目源代码 import 解析一个Matlab项目
|
10 |
from crazy_functions.解析项目源代码 import 解析一个C项目的头文件
|
11 |
from crazy_functions.解析项目源代码 import 解析一个C项目
|
12 |
from crazy_functions.解析项目源代码 import 解析一个Golang项目
|
|
|
39 |
|
40 |
function_plugins = {
|
41 |
"虚空终端": {
|
42 |
+
"Group": "对话|编程|学术|智能体",
|
43 |
"Color": "stop",
|
44 |
"AsButton": True,
|
45 |
"Function": HotReload(虚空终端)
|
|
|
78 |
"Info": "批量总结word文档 | 输入参数为路径",
|
79 |
"Function": HotReload(总结word文档)
|
80 |
},
|
81 |
+
"解析整个Matlab项目": {
|
82 |
+
"Group": "编程",
|
83 |
+
"Color": "stop",
|
84 |
+
"AsButton": False,
|
85 |
+
"Info": "解析一个Matlab项目的所有源文件(.m) | 输入参数为路径",
|
86 |
+
"Function": HotReload(解析一个Matlab项目)
|
87 |
+
},
|
88 |
"解析整个C++项目头文件": {
|
89 |
"Group": "编程",
|
90 |
"Color": "stop",
|
|
|
251 |
"Info": "对中文Latex项目全文进行润色处理 | 输入参数为路径或上传压缩包",
|
252 |
"Function": HotReload(Latex中文润色)
|
253 |
},
|
254 |
+
|
255 |
+
# 被新插件取代
|
256 |
+
# "Latex项目全文中译英(输入路径或上传压缩包)": {
|
257 |
+
# "Group": "学术",
|
258 |
+
# "Color": "stop",
|
259 |
+
# "AsButton": False, # 加入下拉菜单中
|
260 |
+
# "Info": "对Latex项目全文进行中译英处理 | 输入参数为路径或上传压缩包",
|
261 |
+
# "Function": HotReload(Latex中译英)
|
262 |
+
# },
|
263 |
+
# "Latex项目全文英译中(输入路径或上传压缩包)": {
|
264 |
+
# "Group": "学术",
|
265 |
+
# "Color": "stop",
|
266 |
+
# "AsButton": False, # 加入下拉菜单中
|
267 |
+
# "Info": "对Latex项目全文进行英译中处理 | 输入参数为路径或上传压缩包",
|
268 |
+
# "Function": HotReload(Latex英译中)
|
269 |
+
# },
|
270 |
+
|
271 |
"批量Markdown中译英(输入路径或上传压缩包)": {
|
272 |
"Group": "编程",
|
273 |
"Color": "stop",
|
|
|
524 |
except:
|
525 |
print('Load function plugin failed')
|
526 |
|
527 |
+
try:
|
528 |
+
from crazy_functions.函数动态生成 import 函数动态生成
|
529 |
+
function_plugins.update({
|
530 |
+
"动态代码解释器(CodeInterpreter)": {
|
531 |
+
"Group": "智能体",
|
532 |
+
"Color": "stop",
|
533 |
+
"AsButton": False,
|
534 |
+
"Function": HotReload(函数动态生成)
|
535 |
+
}
|
536 |
+
})
|
537 |
+
except:
|
538 |
+
print('Load function plugin failed')
|
539 |
|
540 |
# try:
|
541 |
# from crazy_functions.CodeInterpreter import 虚空终端CodeInterpreter
|
crazy_functions/Langchain知识库.py
CHANGED
@@ -53,14 +53,14 @@ def 知识库问答(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_pro
|
|
53 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
54 |
print('Checking Text2vec ...')
|
55 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
56 |
-
with ProxyNetworkActivate(): # 临时地激活代理网络
|
57 |
HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
|
58 |
|
59 |
# < -------------------构建知识库--------------- >
|
60 |
chatbot.append(['<br/>'.join(file_manifest), "正在构建知识库..."])
|
61 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
62 |
print('Establishing knowledge archive ...')
|
63 |
-
with ProxyNetworkActivate(): # 临时地激活代理网络
|
64 |
kai = knowledge_archive_interface()
|
65 |
kai.feed_archive(file_manifest=file_manifest, id=kai_id)
|
66 |
kai_files = kai.get_loaded_file()
|
|
|
53 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
54 |
print('Checking Text2vec ...')
|
55 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
56 |
+
with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
|
57 |
HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
|
58 |
|
59 |
# < -------------------构建知识库--------------- >
|
60 |
chatbot.append(['<br/>'.join(file_manifest), "正在构建知识库..."])
|
61 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
62 |
print('Establishing knowledge archive ...')
|
63 |
+
with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
|
64 |
kai = knowledge_archive_interface()
|
65 |
kai.feed_archive(file_manifest=file_manifest, id=kai_id)
|
66 |
kai_files = kai.get_loaded_file()
|
crazy_functions/Latex输出PDF结果.py
CHANGED
@@ -79,7 +79,7 @@ def move_project(project_folder, arxiv_id=None):
|
|
79 |
shutil.copytree(src=project_folder, dst=new_workfolder)
|
80 |
return new_workfolder
|
81 |
|
82 |
-
def arxiv_download(chatbot, history, txt):
|
83 |
def check_cached_translation_pdf(arxiv_id):
|
84 |
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'translation')
|
85 |
if not os.path.exists(translation_dir):
|
@@ -116,7 +116,7 @@ def arxiv_download(chatbot, history, txt):
|
|
116 |
arxiv_id = url_.split('/abs/')[-1]
|
117 |
if 'v' in arxiv_id: arxiv_id = arxiv_id[:10]
|
118 |
cached_translation_pdf = check_cached_translation_pdf(arxiv_id)
|
119 |
-
if cached_translation_pdf: return cached_translation_pdf, arxiv_id
|
120 |
|
121 |
url_tar = url_.replace('/abs/', '/e-print/')
|
122 |
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'e-print')
|
@@ -228,6 +228,9 @@ def Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot,
|
|
228 |
# <-------------- more requirements ------------->
|
229 |
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
230 |
more_req = plugin_kwargs.get("advanced_arg", "")
|
|
|
|
|
|
|
231 |
_switch_prompt_ = partial(switch_prompt, more_requirement=more_req)
|
232 |
|
233 |
# <-------------- check deps ------------->
|
@@ -244,7 +247,7 @@ def Latex翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot,
|
|
244 |
|
245 |
# <-------------- clear history and read input ------------->
|
246 |
history = []
|
247 |
-
txt, arxiv_id = yield from arxiv_download(chatbot, history, txt)
|
248 |
if txt.endswith('.pdf'):
|
249 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"发现已经存在翻译好的PDF文档")
|
250 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
|
|
79 |
shutil.copytree(src=project_folder, dst=new_workfolder)
|
80 |
return new_workfolder
|
81 |
|
82 |
+
def arxiv_download(chatbot, history, txt, allow_cache=True):
|
83 |
def check_cached_translation_pdf(arxiv_id):
|
84 |
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'translation')
|
85 |
if not os.path.exists(translation_dir):
|
|
|
116 |
arxiv_id = url_.split('/abs/')[-1]
|
117 |
if 'v' in arxiv_id: arxiv_id = arxiv_id[:10]
|
118 |
cached_translation_pdf = check_cached_translation_pdf(arxiv_id)
|
119 |
+
if cached_translation_pdf and allow_cache: return cached_translation_pdf, arxiv_id
|
120 |
|
121 |
url_tar = url_.replace('/abs/', '/e-print/')
|
122 |
translation_dir = pj(ARXIV_CACHE_DIR, arxiv_id, 'e-print')
|
|
|
228 |
# <-------------- more requirements ------------->
|
229 |
if ("advanced_arg" in plugin_kwargs) and (plugin_kwargs["advanced_arg"] == ""): plugin_kwargs.pop("advanced_arg")
|
230 |
more_req = plugin_kwargs.get("advanced_arg", "")
|
231 |
+
no_cache = more_req.startswith("--no-cache")
|
232 |
+
if no_cache: more_req.lstrip("--no-cache")
|
233 |
+
allow_cache = not no_cache
|
234 |
_switch_prompt_ = partial(switch_prompt, more_requirement=more_req)
|
235 |
|
236 |
# <-------------- check deps ------------->
|
|
|
247 |
|
248 |
# <-------------- clear history and read input ------------->
|
249 |
history = []
|
250 |
+
txt, arxiv_id = yield from arxiv_download(chatbot, history, txt, allow_cache)
|
251 |
if txt.endswith('.pdf'):
|
252 |
report_execption(chatbot, history, a = f"解析项目: {txt}", b = f"发现已经存在翻译好的PDF文档")
|
253 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
crazy_functions/crazy_utils.py
CHANGED
@@ -651,7 +651,7 @@ class knowledge_archive_interface():
|
|
651 |
from toolbox import ProxyNetworkActivate
|
652 |
print('Checking Text2vec ...')
|
653 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
654 |
-
with ProxyNetworkActivate(): # 临时地激活代理网络
|
655 |
self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
|
656 |
|
657 |
return self.text2vec_large_chinese
|
@@ -807,3 +807,10 @@ class construct_html():
|
|
807 |
with open(os.path.join(get_log_folder(), file_name), 'w', encoding='utf8') as f:
|
808 |
f.write(self.html_string.encode('utf-8', 'ignore').decode())
|
809 |
return os.path.join(get_log_folder(), file_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
651 |
from toolbox import ProxyNetworkActivate
|
652 |
print('Checking Text2vec ...')
|
653 |
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
654 |
+
with ProxyNetworkActivate('Download_LLM'): # 临时地激活代理网络
|
655 |
self.text2vec_large_chinese = HuggingFaceEmbeddings(model_name="GanymedeNil/text2vec-large-chinese")
|
656 |
|
657 |
return self.text2vec_large_chinese
|
|
|
807 |
with open(os.path.join(get_log_folder(), file_name), 'w', encoding='utf8') as f:
|
808 |
f.write(self.html_string.encode('utf-8', 'ignore').decode())
|
809 |
return os.path.join(get_log_folder(), file_name)
|
810 |
+
|
811 |
+
|
812 |
+
def get_plugin_arg(plugin_kwargs, key, default):
|
813 |
+
# 如果参数是空的
|
814 |
+
if (key in plugin_kwargs) and (plugin_kwargs[key] == ""): plugin_kwargs.pop(key)
|
815 |
+
# 正常情况
|
816 |
+
return plugin_kwargs.get(key, default)
|
crazy_functions/gen_fns/gen_fns_shared.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import importlib
|
3 |
+
from toolbox import trimmed_format_exc, gen_time_str, get_log_folder
|
4 |
+
from toolbox import CatchException, update_ui, gen_time_str, trimmed_format_exc, is_the_upload_folder
|
5 |
+
from toolbox import promote_file_to_downloadzone, get_log_folder, update_ui_lastest_msg
|
6 |
+
import multiprocessing
|
7 |
+
|
8 |
+
def get_class_name(class_string):
|
9 |
+
import re
|
10 |
+
# Use regex to extract the class name
|
11 |
+
class_name = re.search(r'class (\w+)\(', class_string).group(1)
|
12 |
+
return class_name
|
13 |
+
|
14 |
+
def try_make_module(code, chatbot):
|
15 |
+
module_file = 'gpt_fn_' + gen_time_str().replace('-','_')
|
16 |
+
fn_path = f'{get_log_folder(plugin_name="gen_plugin_verify")}/{module_file}.py'
|
17 |
+
with open(fn_path, 'w', encoding='utf8') as f: f.write(code)
|
18 |
+
promote_file_to_downloadzone(fn_path, chatbot=chatbot)
|
19 |
+
class_name = get_class_name(code)
|
20 |
+
manager = multiprocessing.Manager()
|
21 |
+
return_dict = manager.dict()
|
22 |
+
p = multiprocessing.Process(target=is_function_successfully_generated, args=(fn_path, class_name, return_dict))
|
23 |
+
# only has 10 seconds to run
|
24 |
+
p.start(); p.join(timeout=10)
|
25 |
+
if p.is_alive(): p.terminate(); p.join()
|
26 |
+
p.close()
|
27 |
+
return return_dict["success"], return_dict['traceback']
|
28 |
+
|
29 |
+
# check is_function_successfully_generated
|
30 |
+
def is_function_successfully_generated(fn_path, class_name, return_dict):
|
31 |
+
return_dict['success'] = False
|
32 |
+
return_dict['traceback'] = ""
|
33 |
+
try:
|
34 |
+
# Create a spec for the module
|
35 |
+
module_spec = importlib.util.spec_from_file_location('example_module', fn_path)
|
36 |
+
# Load the module
|
37 |
+
example_module = importlib.util.module_from_spec(module_spec)
|
38 |
+
module_spec.loader.exec_module(example_module)
|
39 |
+
# Now you can use the module
|
40 |
+
some_class = getattr(example_module, class_name)
|
41 |
+
# Now you can create an instance of the class
|
42 |
+
instance = some_class()
|
43 |
+
return_dict['success'] = True
|
44 |
+
return
|
45 |
+
except:
|
46 |
+
return_dict['traceback'] = trimmed_format_exc()
|
47 |
+
return
|
48 |
+
|
49 |
+
def subprocess_worker(code, file_path, return_dict):
|
50 |
+
return_dict['result'] = None
|
51 |
+
return_dict['success'] = False
|
52 |
+
return_dict['traceback'] = ""
|
53 |
+
try:
|
54 |
+
module_file = 'gpt_fn_' + gen_time_str().replace('-','_')
|
55 |
+
fn_path = f'{get_log_folder(plugin_name="gen_plugin_run")}/{module_file}.py'
|
56 |
+
with open(fn_path, 'w', encoding='utf8') as f: f.write(code)
|
57 |
+
class_name = get_class_name(code)
|
58 |
+
# Create a spec for the module
|
59 |
+
module_spec = importlib.util.spec_from_file_location('example_module', fn_path)
|
60 |
+
# Load the module
|
61 |
+
example_module = importlib.util.module_from_spec(module_spec)
|
62 |
+
module_spec.loader.exec_module(example_module)
|
63 |
+
# Now you can use the module
|
64 |
+
some_class = getattr(example_module, class_name)
|
65 |
+
# Now you can create an instance of the class
|
66 |
+
instance = some_class()
|
67 |
+
return_dict['result'] = instance.run(file_path)
|
68 |
+
return_dict['success'] = True
|
69 |
+
except:
|
70 |
+
return_dict['traceback'] = trimmed_format_exc()
|
crazy_functions/pdf_fns/parse_pdf.py
CHANGED
@@ -1,16 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
import random
|
3 |
-
|
|
|
|
|
|
|
4 |
class GROBID_OFFLINE_EXCEPTION(Exception): pass
|
5 |
|
6 |
def get_avail_grobid_url():
|
7 |
-
from toolbox import get_conf
|
8 |
GROBID_URLS, = get_conf('GROBID_URLS')
|
9 |
if len(GROBID_URLS) == 0: return None
|
10 |
try:
|
11 |
_grobid_url = random.choice(GROBID_URLS) # 随机负载均衡
|
12 |
if _grobid_url.endswith('/'): _grobid_url = _grobid_url.rstrip('/')
|
13 |
-
|
|
|
14 |
if res.text=='true': return _grobid_url
|
15 |
else: return None
|
16 |
except:
|
@@ -21,10 +31,141 @@ def parse_pdf(pdf_path, grobid_url):
|
|
21 |
import scipdf # pip install scipdf_parser
|
22 |
if grobid_url.endswith('/'): grobid_url = grobid_url.rstrip('/')
|
23 |
try:
|
24 |
-
|
|
|
25 |
except GROBID_OFFLINE_EXCEPTION:
|
26 |
raise GROBID_OFFLINE_EXCEPTION("GROBID服务不可用,请修改config中的GROBID_URL,可修改成本地GROBID服务。")
|
27 |
except:
|
28 |
raise RuntimeError("解析PDF失败,请检查PDF是否损坏。")
|
29 |
return article_dict
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import lru_cache
|
2 |
+
from toolbox import gen_time_str
|
3 |
+
from toolbox import promote_file_to_downloadzone
|
4 |
+
from toolbox import write_history_to_file, promote_file_to_downloadzone
|
5 |
+
from toolbox import get_conf
|
6 |
+
from toolbox import ProxyNetworkActivate
|
7 |
+
from colorful import *
|
8 |
import requests
|
9 |
import random
|
10 |
+
import copy
|
11 |
+
import os
|
12 |
+
import math
|
13 |
+
|
14 |
class GROBID_OFFLINE_EXCEPTION(Exception): pass
|
15 |
|
16 |
def get_avail_grobid_url():
|
|
|
17 |
GROBID_URLS, = get_conf('GROBID_URLS')
|
18 |
if len(GROBID_URLS) == 0: return None
|
19 |
try:
|
20 |
_grobid_url = random.choice(GROBID_URLS) # 随机负载均衡
|
21 |
if _grobid_url.endswith('/'): _grobid_url = _grobid_url.rstrip('/')
|
22 |
+
with ProxyNetworkActivate('Connect_Grobid'):
|
23 |
+
res = requests.get(_grobid_url+'/api/isalive')
|
24 |
if res.text=='true': return _grobid_url
|
25 |
else: return None
|
26 |
except:
|
|
|
31 |
import scipdf # pip install scipdf_parser
|
32 |
if grobid_url.endswith('/'): grobid_url = grobid_url.rstrip('/')
|
33 |
try:
|
34 |
+
with ProxyNetworkActivate('Connect_Grobid'):
|
35 |
+
article_dict = scipdf.parse_pdf_to_dict(pdf_path, grobid_url=grobid_url)
|
36 |
except GROBID_OFFLINE_EXCEPTION:
|
37 |
raise GROBID_OFFLINE_EXCEPTION("GROBID服务不可用,请修改config中的GROBID_URL,可修改成本地GROBID服务。")
|
38 |
except:
|
39 |
raise RuntimeError("解析PDF失败,请检查PDF是否损坏。")
|
40 |
return article_dict
|
41 |
|
42 |
+
|
43 |
+
def produce_report_markdown(gpt_response_collection, meta, paper_meta_info, chatbot, fp, generated_conclusion_files):
|
44 |
+
# -=-=-=-=-=-=-=-= 写出第1个文件:翻译前后混合 -=-=-=-=-=-=-=-=
|
45 |
+
res_path = write_history_to_file(meta + ["# Meta Translation" , paper_meta_info] + gpt_response_collection, file_basename=f"{gen_time_str()}translated_and_original.md", file_fullname=None)
|
46 |
+
promote_file_to_downloadzone(res_path, rename_file=os.path.basename(res_path)+'.md', chatbot=chatbot)
|
47 |
+
generated_conclusion_files.append(res_path)
|
48 |
+
|
49 |
+
# -=-=-=-=-=-=-=-= 写出第2个文件:仅翻译后的文本 -=-=-=-=-=-=-=-=
|
50 |
+
translated_res_array = []
|
51 |
+
# 记录当前的大章节标题:
|
52 |
+
last_section_name = ""
|
53 |
+
for index, value in enumerate(gpt_response_collection):
|
54 |
+
# 先挑选偶数序列号:
|
55 |
+
if index % 2 != 0:
|
56 |
+
# 先提取当前英文标题:
|
57 |
+
cur_section_name = gpt_response_collection[index-1].split('\n')[0].split(" Part")[0]
|
58 |
+
# 如果index是1的话,则直接使用first section name:
|
59 |
+
if cur_section_name != last_section_name:
|
60 |
+
cur_value = cur_section_name + '\n'
|
61 |
+
last_section_name = copy.deepcopy(cur_section_name)
|
62 |
+
else:
|
63 |
+
cur_value = ""
|
64 |
+
# 再做一个小修改:重新修改当前part的标题,默认用英文的
|
65 |
+
cur_value += value
|
66 |
+
translated_res_array.append(cur_value)
|
67 |
+
res_path = write_history_to_file(meta + ["# Meta Translation" , paper_meta_info] + translated_res_array,
|
68 |
+
file_basename = f"{gen_time_str()}-translated_only.md",
|
69 |
+
file_fullname = None,
|
70 |
+
auto_caption = False)
|
71 |
+
promote_file_to_downloadzone(res_path, rename_file=os.path.basename(res_path)+'.md', chatbot=chatbot)
|
72 |
+
generated_conclusion_files.append(res_path)
|
73 |
+
return res_path
|
74 |
+
|
75 |
+
def translate_pdf(article_dict, llm_kwargs, chatbot, fp, generated_conclusion_files, TOKEN_LIMIT_PER_FRAGMENT, DST_LANG):
|
76 |
+
from crazy_functions.crazy_utils import construct_html
|
77 |
+
from crazy_functions.crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
78 |
+
from crazy_functions.crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
79 |
+
from crazy_functions.crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
80 |
+
|
81 |
+
prompt = "以下是一篇学术论文的基本信息:\n"
|
82 |
+
# title
|
83 |
+
title = article_dict.get('title', '无法获取 title'); prompt += f'title:{title}\n\n'
|
84 |
+
# authors
|
85 |
+
authors = article_dict.get('authors', '无法获取 authors'); prompt += f'authors:{authors}\n\n'
|
86 |
+
# abstract
|
87 |
+
abstract = article_dict.get('abstract', '无法获取 abstract'); prompt += f'abstract:{abstract}\n\n'
|
88 |
+
# command
|
89 |
+
prompt += f"请将题目和摘要翻译为{DST_LANG}。"
|
90 |
+
meta = [f'# Title:\n\n', title, f'# Abstract:\n\n', abstract ]
|
91 |
+
|
92 |
+
# 单线,获取文章meta信息
|
93 |
+
paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
94 |
+
inputs=prompt,
|
95 |
+
inputs_show_user=prompt,
|
96 |
+
llm_kwargs=llm_kwargs,
|
97 |
+
chatbot=chatbot, history=[],
|
98 |
+
sys_prompt="You are an academic paper reader。",
|
99 |
+
)
|
100 |
+
|
101 |
+
# 多线,翻译
|
102 |
+
inputs_array = []
|
103 |
+
inputs_show_user_array = []
|
104 |
+
|
105 |
+
# get_token_num
|
106 |
+
from request_llm.bridge_all import model_info
|
107 |
+
enc = model_info[llm_kwargs['llm_model']]['tokenizer']
|
108 |
+
def get_token_num(txt): return len(enc.encode(txt, disallowed_special=()))
|
109 |
+
|
110 |
+
def break_down(txt):
|
111 |
+
raw_token_num = get_token_num(txt)
|
112 |
+
if raw_token_num <= TOKEN_LIMIT_PER_FRAGMENT:
|
113 |
+
return [txt]
|
114 |
+
else:
|
115 |
+
# raw_token_num > TOKEN_LIMIT_PER_FRAGMENT
|
116 |
+
# find a smooth token limit to achieve even seperation
|
117 |
+
count = int(math.ceil(raw_token_num / TOKEN_LIMIT_PER_FRAGMENT))
|
118 |
+
token_limit_smooth = raw_token_num // count + count
|
119 |
+
return breakdown_txt_to_satisfy_token_limit_for_pdf(txt, get_token_fn=get_token_num, limit=token_limit_smooth)
|
120 |
+
|
121 |
+
for section in article_dict.get('sections'):
|
122 |
+
if len(section['text']) == 0: continue
|
123 |
+
section_frags = break_down(section['text'])
|
124 |
+
for i, fragment in enumerate(section_frags):
|
125 |
+
heading = section['heading']
|
126 |
+
if len(section_frags) > 1: heading += f' Part-{i+1}'
|
127 |
+
inputs_array.append(
|
128 |
+
f"你需要翻译{heading}章节,内容如下: \n\n{fragment}"
|
129 |
+
)
|
130 |
+
inputs_show_user_array.append(
|
131 |
+
f"# {heading}\n\n{fragment}"
|
132 |
+
)
|
133 |
+
|
134 |
+
gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
135 |
+
inputs_array=inputs_array,
|
136 |
+
inputs_show_user_array=inputs_show_user_array,
|
137 |
+
llm_kwargs=llm_kwargs,
|
138 |
+
chatbot=chatbot,
|
139 |
+
history_array=[meta for _ in inputs_array],
|
140 |
+
sys_prompt_array=[
|
141 |
+
"请你作为一个学术翻译,负责把学术论文准确翻译成中文。注意文章中的每一句话都要翻译。" for _ in inputs_array],
|
142 |
+
)
|
143 |
+
# -=-=-=-=-=-=-=-= 写出Markdown文件 -=-=-=-=-=-=-=-=
|
144 |
+
produce_report_markdown(gpt_response_collection, meta, paper_meta_info, chatbot, fp, generated_conclusion_files)
|
145 |
+
|
146 |
+
# -=-=-=-=-=-=-=-= 写出HTML文件 -=-=-=-=-=-=-=-=
|
147 |
+
ch = construct_html()
|
148 |
+
orig = ""
|
149 |
+
trans = ""
|
150 |
+
gpt_response_collection_html = copy.deepcopy(gpt_response_collection)
|
151 |
+
for i,k in enumerate(gpt_response_collection_html):
|
152 |
+
if i%2==0:
|
153 |
+
gpt_response_collection_html[i] = inputs_show_user_array[i//2]
|
154 |
+
else:
|
155 |
+
# 先提取当前英文标题:
|
156 |
+
cur_section_name = gpt_response_collection[i-1].split('\n')[0].split(" Part")[0]
|
157 |
+
cur_value = cur_section_name + "\n" + gpt_response_collection_html[i]
|
158 |
+
gpt_response_collection_html[i] = cur_value
|
159 |
+
|
160 |
+
final = ["", "", "一、论文概况", "", "Abstract", paper_meta_info, "二、论文翻译", ""]
|
161 |
+
final.extend(gpt_response_collection_html)
|
162 |
+
for i, k in enumerate(final):
|
163 |
+
if i%2==0:
|
164 |
+
orig = k
|
165 |
+
if i%2==1:
|
166 |
+
trans = k
|
167 |
+
ch.add_row(a=orig, b=trans)
|
168 |
+
create_report_file_name = f"{os.path.basename(fp)}.trans.html"
|
169 |
+
html_file = ch.save_file(create_report_file_name)
|
170 |
+
generated_conclusion_files.append(html_file)
|
171 |
+
promote_file_to_downloadzone(html_file, rename_file=os.path.basename(html_file), chatbot=chatbot)
|
crazy_functions/函数动态生成.py
ADDED
@@ -0,0 +1,252 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 本源代码中, ⭐ = 关键步骤
|
2 |
+
"""
|
3 |
+
测试:
|
4 |
+
- 裁剪图像,保留下半部分
|
5 |
+
- 交换图像的蓝色通道和红色通道
|
6 |
+
- 将图像转为灰度图像
|
7 |
+
- 将csv文件转excel表格
|
8 |
+
|
9 |
+
Testing:
|
10 |
+
- Crop the image, keeping the bottom half.
|
11 |
+
- Swap the blue channel and red channel of the image.
|
12 |
+
- Convert the image to grayscale.
|
13 |
+
- Convert the CSV file to an Excel spreadsheet.
|
14 |
+
"""
|
15 |
+
|
16 |
+
|
17 |
+
from toolbox import CatchException, update_ui, gen_time_str, trimmed_format_exc, is_the_upload_folder
|
18 |
+
from toolbox import promote_file_to_downloadzone, get_log_folder, update_ui_lastest_msg
|
19 |
+
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive, get_plugin_arg
|
20 |
+
from .crazy_utils import input_clipping, try_install_deps
|
21 |
+
from crazy_functions.gen_fns.gen_fns_shared import is_function_successfully_generated
|
22 |
+
from crazy_functions.gen_fns.gen_fns_shared import get_class_name
|
23 |
+
from crazy_functions.gen_fns.gen_fns_shared import subprocess_worker
|
24 |
+
from crazy_functions.gen_fns.gen_fns_shared import try_make_module
|
25 |
+
import os
|
26 |
+
import time
|
27 |
+
import glob
|
28 |
+
import multiprocessing
|
29 |
+
|
30 |
+
templete = """
|
31 |
+
```python
|
32 |
+
import ... # Put dependencies here, e.g. import numpy as np.
|
33 |
+
|
34 |
+
class TerminalFunction(object): # Do not change the name of the class, The name of the class must be `TerminalFunction`
|
35 |
+
|
36 |
+
def run(self, path): # The name of the function must be `run`, it takes only a positional argument.
|
37 |
+
# rewrite the function you have just written here
|
38 |
+
...
|
39 |
+
return generated_file_path
|
40 |
+
```
|
41 |
+
"""
|
42 |
+
|
43 |
+
def inspect_dependency(chatbot, history):
|
44 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
45 |
+
return True
|
46 |
+
|
47 |
+
def get_code_block(reply):
|
48 |
+
import re
|
49 |
+
pattern = r"```([\s\S]*?)```" # regex pattern to match code blocks
|
50 |
+
matches = re.findall(pattern, reply) # find all code blocks in text
|
51 |
+
if len(matches) == 1:
|
52 |
+
return matches[0].strip('python') # code block
|
53 |
+
for match in matches:
|
54 |
+
if 'class TerminalFunction' in match:
|
55 |
+
return match.strip('python') # code block
|
56 |
+
raise RuntimeError("GPT is not generating proper code.")
|
57 |
+
|
58 |
+
def gpt_interact_multi_step(txt, file_type, llm_kwargs, chatbot, history):
|
59 |
+
# 输入
|
60 |
+
prompt_compose = [
|
61 |
+
f'Your job:\n'
|
62 |
+
f'1. write a single Python function, which takes a path of a `{file_type}` file as the only argument and returns a `string` containing the result of analysis or the path of generated files. \n',
|
63 |
+
f"2. You should write this function to perform following task: " + txt + "\n",
|
64 |
+
f"3. Wrap the output python function with markdown codeblock."
|
65 |
+
]
|
66 |
+
i_say = "".join(prompt_compose)
|
67 |
+
demo = []
|
68 |
+
|
69 |
+
# 第一步
|
70 |
+
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
71 |
+
inputs=i_say, inputs_show_user=i_say,
|
72 |
+
llm_kwargs=llm_kwargs, chatbot=chatbot, history=demo,
|
73 |
+
sys_prompt= r"You are a world-class programmer."
|
74 |
+
)
|
75 |
+
history.extend([i_say, gpt_say])
|
76 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
77 |
+
|
78 |
+
# 第二步
|
79 |
+
prompt_compose = [
|
80 |
+
"If previous stage is successful, rewrite the function you have just written to satisfy following templete: \n",
|
81 |
+
templete
|
82 |
+
]
|
83 |
+
i_say = "".join(prompt_compose); inputs_show_user = "If previous stage is successful, rewrite the function you have just written to satisfy executable templete. "
|
84 |
+
gpt_say = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
85 |
+
inputs=i_say, inputs_show_user=inputs_show_user,
|
86 |
+
llm_kwargs=llm_kwargs, chatbot=chatbot, history=history,
|
87 |
+
sys_prompt= r"You are a programmer. You need to replace `...` with valid packages, do not give `...` in your answer!"
|
88 |
+
)
|
89 |
+
code_to_return = gpt_say
|
90 |
+
history.extend([i_say, gpt_say])
|
91 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
92 |
+
|
93 |
+
# # 第三步
|
94 |
+
# i_say = "Please list to packages to install to run the code above. Then show me how to use `try_install_deps` function to install them."
|
95 |
+
# i_say += 'For instance. `try_install_deps(["opencv-python", "scipy", "numpy"])`'
|
96 |
+
# installation_advance = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
97 |
+
# inputs=i_say, inputs_show_user=inputs_show_user,
|
98 |
+
# llm_kwargs=llm_kwargs, chatbot=chatbot, history=history,
|
99 |
+
# sys_prompt= r"You are a programmer."
|
100 |
+
# )
|
101 |
+
|
102 |
+
# # # 第三步
|
103 |
+
# i_say = "Show me how to use `pip` to install packages to run the code above. "
|
104 |
+
# i_say += 'For instance. `pip install -r opencv-python scipy numpy`'
|
105 |
+
# installation_advance = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
106 |
+
# inputs=i_say, inputs_show_user=i_say,
|
107 |
+
# llm_kwargs=llm_kwargs, chatbot=chatbot, history=history,
|
108 |
+
# sys_prompt= r"You are a programmer."
|
109 |
+
# )
|
110 |
+
installation_advance = ""
|
111 |
+
|
112 |
+
return code_to_return, installation_advance, txt, file_type, llm_kwargs, chatbot, history
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
def for_immediate_show_off_when_possible(file_type, fp, chatbot):
|
118 |
+
if file_type in ['png', 'jpg']:
|
119 |
+
image_path = os.path.abspath(fp)
|
120 |
+
chatbot.append(['这是一张图片, 展示如下:',
|
121 |
+
f'本地文件地址: <br/>`{image_path}`<br/>'+
|
122 |
+
f'本地文件预览: <br/><div align="center"><img src="file={image_path}"></div>'
|
123 |
+
])
|
124 |
+
return chatbot
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
def have_any_recent_upload_files(chatbot):
|
129 |
+
_5min = 5 * 60
|
130 |
+
if not chatbot: return False # chatbot is None
|
131 |
+
most_recent_uploaded = chatbot._cookies.get("most_recent_uploaded", None)
|
132 |
+
if not most_recent_uploaded: return False # most_recent_uploaded is None
|
133 |
+
if time.time() - most_recent_uploaded["time"] < _5min: return True # most_recent_uploaded is new
|
134 |
+
else: return False # most_recent_uploaded is too old
|
135 |
+
|
136 |
+
def get_recent_file_prompt_support(chatbot):
|
137 |
+
most_recent_uploaded = chatbot._cookies.get("most_recent_uploaded", None)
|
138 |
+
path = most_recent_uploaded['path']
|
139 |
+
return path
|
140 |
+
|
141 |
+
@CatchException
|
142 |
+
def 函数动态生成(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
143 |
+
"""
|
144 |
+
txt 输入栏用户输入的文本,例如需要翻译的一段话,再例如一个包含了待处理文件的路径
|
145 |
+
llm_kwargs gpt模型参数,如温度和top_p等,一般原样传递下去就行
|
146 |
+
plugin_kwargs 插件模型的参数,暂时没有用武之地
|
147 |
+
chatbot 聊天显示框的句柄,用于显示给用户
|
148 |
+
history 聊天历史,前情提要
|
149 |
+
system_prompt 给gpt的静默提醒
|
150 |
+
web_port 当前软件运行的端口号
|
151 |
+
"""
|
152 |
+
|
153 |
+
# 清空历史
|
154 |
+
history = []
|
155 |
+
|
156 |
+
# 基本信息:功能、贡献者
|
157 |
+
chatbot.append(["正在启动: 插件动态生成插件", "插件动态生成, 执行开始, 作者Binary-Husky."])
|
158 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
159 |
+
|
160 |
+
# ⭐ 文件上传区是否有东西
|
161 |
+
# 1. 如果有文件: 作为函数参数
|
162 |
+
# 2. 如果没有文件:需要用GPT提取参数 (太懒了,以后再写,虚空终端已经实现了类似的代码)
|
163 |
+
file_list = []
|
164 |
+
if get_plugin_arg(plugin_kwargs, key="file_path_arg", default=False):
|
165 |
+
file_path = get_plugin_arg(plugin_kwargs, key="file_path_arg", default=None)
|
166 |
+
file_list.append(file_path)
|
167 |
+
yield from update_ui_lastest_msg(f"当前文件: {file_path}", chatbot, history, 1)
|
168 |
+
elif have_any_recent_upload_files(chatbot):
|
169 |
+
file_dir = get_recent_file_prompt_support(chatbot)
|
170 |
+
file_list = glob.glob(os.path.join(file_dir, '**/*'), recursive=True)
|
171 |
+
yield from update_ui_lastest_msg(f"当前文件处理列表: {file_list}", chatbot, history, 1)
|
172 |
+
else:
|
173 |
+
chatbot.append(["文件检索", "没有发现任何近期上传的文件。"])
|
174 |
+
yield from update_ui_lastest_msg("没有发现任何近期上传的文件。", chatbot, history, 1)
|
175 |
+
return # 2. 如果没有文件
|
176 |
+
if len(file_list) == 0:
|
177 |
+
chatbot.append(["文件检索", "没有发现任何近期上传的文件。"])
|
178 |
+
yield from update_ui_lastest_msg("没有发现任何近期上传的文件。", chatbot, history, 1)
|
179 |
+
return # 2. 如果没有文件
|
180 |
+
|
181 |
+
# 读取文件
|
182 |
+
file_type = file_list[0].split('.')[-1]
|
183 |
+
|
184 |
+
# 粗心检查
|
185 |
+
if is_the_upload_folder(txt):
|
186 |
+
yield from update_ui_lastest_msg(f"请在输入框内填写需求, 然后再次点击该插件! 至于您的文件,不用担心, 文件路径 {txt} 已经被记忆. ", chatbot, history, 1)
|
187 |
+
return
|
188 |
+
|
189 |
+
# 开始干正事
|
190 |
+
MAX_TRY = 3
|
191 |
+
for j in range(MAX_TRY): # 最多重试5次
|
192 |
+
traceback = ""
|
193 |
+
try:
|
194 |
+
# ⭐ 开始啦 !
|
195 |
+
code, installation_advance, txt, file_type, llm_kwargs, chatbot, history = \
|
196 |
+
yield from gpt_interact_multi_step(txt, file_type, llm_kwargs, chatbot, history)
|
197 |
+
chatbot.append(["代码生成阶段结束", ""])
|
198 |
+
yield from update_ui_lastest_msg(f"正在验证上述代码的有效性 ...", chatbot, history, 1)
|
199 |
+
# ⭐ 分离代码块
|
200 |
+
code = get_code_block(code)
|
201 |
+
# ⭐ 检查模块
|
202 |
+
ok, traceback = try_make_module(code, chatbot)
|
203 |
+
# 搞定代码生成
|
204 |
+
if ok: break
|
205 |
+
except Exception as e:
|
206 |
+
if not traceback: traceback = trimmed_format_exc()
|
207 |
+
# 处理异常
|
208 |
+
if not traceback: traceback = trimmed_format_exc()
|
209 |
+
yield from update_ui_lastest_msg(f"第 {j+1}/{MAX_TRY} 次代码生成尝试, 失败了~ 别担心, 我们5秒后再试一次... \n\n此次我们的错误追踪是\n```\n{traceback}\n```\n", chatbot, history, 5)
|
210 |
+
|
211 |
+
# 代码生成结束, 开始执行
|
212 |
+
TIME_LIMIT = 15
|
213 |
+
yield from update_ui_lastest_msg(f"开始创建新进程并执行代码! 时间限制 {TIME_LIMIT} 秒. 请等待任务完成... ", chatbot, history, 1)
|
214 |
+
manager = multiprocessing.Manager()
|
215 |
+
return_dict = manager.dict()
|
216 |
+
|
217 |
+
# �� 到最后一步了,开始逐个文件进行处理
|
218 |
+
for file_path in file_list:
|
219 |
+
if os.path.exists(file_path):
|
220 |
+
chatbot.append([f"正在处理文件: {file_path}", f"请稍等..."])
|
221 |
+
chatbot = for_immediate_show_off_when_possible(file_type, file_path, chatbot)
|
222 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
223 |
+
else:
|
224 |
+
continue
|
225 |
+
|
226 |
+
# ⭐⭐⭐ subprocess_worker ⭐⭐⭐
|
227 |
+
p = multiprocessing.Process(target=subprocess_worker, args=(code, file_path, return_dict))
|
228 |
+
# ⭐ 开始执行,时间限制TIME_LIMIT
|
229 |
+
p.start(); p.join(timeout=TIME_LIMIT)
|
230 |
+
if p.is_alive(): p.terminate(); p.join()
|
231 |
+
p.close()
|
232 |
+
res = return_dict['result']
|
233 |
+
success = return_dict['success']
|
234 |
+
traceback = return_dict['traceback']
|
235 |
+
if not success:
|
236 |
+
if not traceback: traceback = trimmed_format_exc()
|
237 |
+
chatbot.append(["执行失败了", f"错误追踪\n```\n{trimmed_format_exc()}\n```\n"])
|
238 |
+
# chatbot.append(["如果是缺乏依赖,请参考以下建议", installation_advance])
|
239 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
240 |
+
return
|
241 |
+
|
242 |
+
# 顺利完成,收尾
|
243 |
+
res = str(res)
|
244 |
+
if os.path.exists(res):
|
245 |
+
chatbot.append(["执行成功了,结果是一个有效文件", "结果:" + res])
|
246 |
+
new_file_path = promote_file_to_downloadzone(res, chatbot=chatbot)
|
247 |
+
chatbot = for_immediate_show_off_when_possible(file_type, new_file_path, chatbot)
|
248 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
249 |
+
else:
|
250 |
+
chatbot.append(["执行成功了,结果是一个字符串", "结果:" + res])
|
251 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 # 界面更新
|
252 |
+
|
crazy_functions/批量翻译PDF文档_NOUGAT.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
from toolbox import CatchException, report_execption, gen_time_str
|
2 |
from toolbox import update_ui, promote_file_to_downloadzone, update_ui_lastest_msg, disable_auto_promotion
|
3 |
-
from toolbox import write_history_to_file,
|
4 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
5 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
6 |
from .crazy_utils import read_and_clean_pdf_text
|
7 |
-
from .pdf_fns.parse_pdf import parse_pdf, get_avail_grobid_url
|
8 |
from colorful import *
|
|
|
9 |
import os
|
10 |
import math
|
11 |
import logging
|
@@ -92,7 +93,7 @@ def 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst
|
|
92 |
def 解析PDF_基于NOUGAT(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
93 |
import copy
|
94 |
import tiktoken
|
95 |
-
TOKEN_LIMIT_PER_FRAGMENT =
|
96 |
generated_conclusion_files = []
|
97 |
generated_html_files = []
|
98 |
DST_LANG = "中文"
|
@@ -101,101 +102,12 @@ def 解析PDF_基于NOUGAT(file_manifest, project_folder, llm_kwargs, plugin_kwa
|
|
101 |
for index, fp in enumerate(file_manifest):
|
102 |
chatbot.append(["当前进度:", f"正在解析论文,请稍候。(第一次运行时,需要花费较长时间下载NOUGAT参数)"]); yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
103 |
fpp = yield from nougat_handle.NOUGAT_parse_pdf(fp, chatbot, history)
|
104 |
-
|
105 |
with open(fpp, 'r', encoding='utf8') as f:
|
106 |
article_content = f.readlines()
|
107 |
article_dict = markdown_to_dict(article_content)
|
108 |
logging.info(article_dict)
|
109 |
-
|
110 |
-
prompt = "以下是一篇学术论文的基本信息:\n"
|
111 |
-
# title
|
112 |
-
title = article_dict.get('title', '无法获取 title'); prompt += f'title:{title}\n\n'
|
113 |
-
# authors
|
114 |
-
authors = article_dict.get('authors', '无法获取 authors'); prompt += f'authors:{authors}\n\n'
|
115 |
-
# abstract
|
116 |
-
abstract = article_dict.get('abstract', '无法获取 abstract'); prompt += f'abstract:{abstract}\n\n'
|
117 |
-
# command
|
118 |
-
prompt += f"请将题目和摘要翻译为{DST_LANG}。"
|
119 |
-
meta = [f'# Title:\n\n', title, f'# Abstract:\n\n', abstract ]
|
120 |
-
|
121 |
-
# 单线,获取文章meta信息
|
122 |
-
paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
123 |
-
inputs=prompt,
|
124 |
-
inputs_show_user=prompt,
|
125 |
-
llm_kwargs=llm_kwargs,
|
126 |
-
chatbot=chatbot, history=[],
|
127 |
-
sys_prompt="You are an academic paper reader。",
|
128 |
-
)
|
129 |
-
|
130 |
-
# 多线,翻译
|
131 |
-
inputs_array = []
|
132 |
-
inputs_show_user_array = []
|
133 |
-
|
134 |
-
# get_token_num
|
135 |
-
from request_llm.bridge_all import model_info
|
136 |
-
enc = model_info[llm_kwargs['llm_model']]['tokenizer']
|
137 |
-
def get_token_num(txt): return len(enc.encode(txt, disallowed_special=()))
|
138 |
-
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
139 |
-
|
140 |
-
def break_down(txt):
|
141 |
-
raw_token_num = get_token_num(txt)
|
142 |
-
if raw_token_num <= TOKEN_LIMIT_PER_FRAGMENT:
|
143 |
-
return [txt]
|
144 |
-
else:
|
145 |
-
# raw_token_num > TOKEN_LIMIT_PER_FRAGMENT
|
146 |
-
# find a smooth token limit to achieve even seperation
|
147 |
-
count = int(math.ceil(raw_token_num / TOKEN_LIMIT_PER_FRAGMENT))
|
148 |
-
token_limit_smooth = raw_token_num // count + count
|
149 |
-
return breakdown_txt_to_satisfy_token_limit_for_pdf(txt, get_token_fn=get_token_num, limit=token_limit_smooth)
|
150 |
-
|
151 |
-
for section in article_dict.get('sections'):
|
152 |
-
if len(section['text']) == 0: continue
|
153 |
-
section_frags = break_down(section['text'])
|
154 |
-
for i, fragment in enumerate(section_frags):
|
155 |
-
heading = section['heading']
|
156 |
-
if len(section_frags) > 1: heading += f' Part-{i+1}'
|
157 |
-
inputs_array.append(
|
158 |
-
f"你需要翻译{heading}章节,内容如下: \n\n{fragment}"
|
159 |
-
)
|
160 |
-
inputs_show_user_array.append(
|
161 |
-
f"# {heading}\n\n{fragment}"
|
162 |
-
)
|
163 |
-
|
164 |
-
gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
165 |
-
inputs_array=inputs_array,
|
166 |
-
inputs_show_user_array=inputs_show_user_array,
|
167 |
-
llm_kwargs=llm_kwargs,
|
168 |
-
chatbot=chatbot,
|
169 |
-
history_array=[meta for _ in inputs_array],
|
170 |
-
sys_prompt_array=[
|
171 |
-
"请你作为一个学术翻译,负责把学术论文准确翻译成中文。注意文章中的每一句话都要翻译。" for _ in inputs_array],
|
172 |
-
)
|
173 |
-
res_path = write_history_to_file(meta + ["# Meta Translation" , paper_meta_info] + gpt_response_collection, file_basename=None, file_fullname=None)
|
174 |
-
promote_file_to_downloadzone(res_path, rename_file=os.path.basename(fp)+'.md', chatbot=chatbot)
|
175 |
-
generated_conclusion_files.append(res_path)
|
176 |
-
|
177 |
-
ch = construct_html()
|
178 |
-
orig = ""
|
179 |
-
trans = ""
|
180 |
-
gpt_response_collection_html = copy.deepcopy(gpt_response_collection)
|
181 |
-
for i,k in enumerate(gpt_response_collection_html):
|
182 |
-
if i%2==0:
|
183 |
-
gpt_response_collection_html[i] = inputs_show_user_array[i//2]
|
184 |
-
else:
|
185 |
-
gpt_response_collection_html[i] = gpt_response_collection_html[i]
|
186 |
-
|
187 |
-
final = ["", "", "一、论文概况", "", "Abstract", paper_meta_info, "二、论文翻译", ""]
|
188 |
-
final.extend(gpt_response_collection_html)
|
189 |
-
for i, k in enumerate(final):
|
190 |
-
if i%2==0:
|
191 |
-
orig = k
|
192 |
-
if i%2==1:
|
193 |
-
trans = k
|
194 |
-
ch.add_row(a=orig, b=trans)
|
195 |
-
create_report_file_name = f"{os.path.basename(fp)}.trans.html"
|
196 |
-
html_file = ch.save_file(create_report_file_name)
|
197 |
-
generated_html_files.append(html_file)
|
198 |
-
promote_file_to_downloadzone(html_file, rename_file=os.path.basename(html_file), chatbot=chatbot)
|
199 |
|
200 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))
|
201 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
|
|
1 |
+
from toolbox import CatchException, report_execption, get_log_folder, gen_time_str
|
2 |
from toolbox import update_ui, promote_file_to_downloadzone, update_ui_lastest_msg, disable_auto_promotion
|
3 |
+
from toolbox import write_history_to_file, promote_file_to_downloadzone
|
4 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
5 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
6 |
from .crazy_utils import read_and_clean_pdf_text
|
7 |
+
from .pdf_fns.parse_pdf import parse_pdf, get_avail_grobid_url, translate_pdf
|
8 |
from colorful import *
|
9 |
+
import copy
|
10 |
import os
|
11 |
import math
|
12 |
import logging
|
|
|
93 |
def 解析PDF_基于NOUGAT(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
94 |
import copy
|
95 |
import tiktoken
|
96 |
+
TOKEN_LIMIT_PER_FRAGMENT = 1024
|
97 |
generated_conclusion_files = []
|
98 |
generated_html_files = []
|
99 |
DST_LANG = "中文"
|
|
|
102 |
for index, fp in enumerate(file_manifest):
|
103 |
chatbot.append(["当前进度:", f"正在解析论文,请稍候。(第一次运行时,需要花费较长时间下载NOUGAT参数)"]); yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
104 |
fpp = yield from nougat_handle.NOUGAT_parse_pdf(fp, chatbot, history)
|
105 |
+
promote_file_to_downloadzone(fpp, rename_file=os.path.basename(fpp)+'.nougat.mmd', chatbot=chatbot)
|
106 |
with open(fpp, 'r', encoding='utf8') as f:
|
107 |
article_content = f.readlines()
|
108 |
article_dict = markdown_to_dict(article_content)
|
109 |
logging.info(article_dict)
|
110 |
+
yield from translate_pdf(article_dict, llm_kwargs, chatbot, fp, generated_conclusion_files, TOKEN_LIMIT_PER_FRAGMENT, DST_LANG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))
|
113 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
crazy_functions/批量翻译PDF文档_多线程.py
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
-
from toolbox import CatchException, report_execption, get_log_folder
|
2 |
from toolbox import update_ui, promote_file_to_downloadzone, update_ui_lastest_msg, disable_auto_promotion
|
3 |
from toolbox import write_history_to_file, promote_file_to_downloadzone
|
4 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
5 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
6 |
from .crazy_utils import read_and_clean_pdf_text
|
7 |
-
from .pdf_fns.parse_pdf import parse_pdf, get_avail_grobid_url
|
8 |
from colorful import *
|
9 |
-
import
|
10 |
import os
|
11 |
import math
|
12 |
|
@@ -58,8 +58,8 @@ def 批量翻译PDF文档(txt, llm_kwargs, plugin_kwargs, chatbot, history, syst
|
|
58 |
|
59 |
|
60 |
def 解析PDF_基于GROBID(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, grobid_url):
|
61 |
-
import copy
|
62 |
-
TOKEN_LIMIT_PER_FRAGMENT =
|
63 |
generated_conclusion_files = []
|
64 |
generated_html_files = []
|
65 |
DST_LANG = "中文"
|
@@ -67,104 +67,23 @@ def 解析PDF_基于GROBID(file_manifest, project_folder, llm_kwargs, plugin_kwa
|
|
67 |
for index, fp in enumerate(file_manifest):
|
68 |
chatbot.append(["当前进度:", f"正在连接GROBID服务,请稍候: {grobid_url}\n如果等待时间过长,请修改config中的GROBID_URL,可修改成本地GROBID服务。"]); yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
69 |
article_dict = parse_pdf(fp, grobid_url)
|
|
|
|
|
|
|
|
|
|
|
70 |
if article_dict is None: raise RuntimeError("解析PDF失败,请检查PDF是否损坏。")
|
71 |
-
|
72 |
-
# title
|
73 |
-
title = article_dict.get('title', '无法获取 title'); prompt += f'title:{title}\n\n'
|
74 |
-
# authors
|
75 |
-
authors = article_dict.get('authors', '无法获取 authors'); prompt += f'authors:{authors}\n\n'
|
76 |
-
# abstract
|
77 |
-
abstract = article_dict.get('abstract', '无法获取 abstract'); prompt += f'abstract:{abstract}\n\n'
|
78 |
-
# command
|
79 |
-
prompt += f"请将题目和摘要翻译为{DST_LANG}。"
|
80 |
-
meta = [f'# Title:\n\n', title, f'# Abstract:\n\n', abstract ]
|
81 |
-
|
82 |
-
# 单线,获取文章meta信息
|
83 |
-
paper_meta_info = yield from request_gpt_model_in_new_thread_with_ui_alive(
|
84 |
-
inputs=prompt,
|
85 |
-
inputs_show_user=prompt,
|
86 |
-
llm_kwargs=llm_kwargs,
|
87 |
-
chatbot=chatbot, history=[],
|
88 |
-
sys_prompt="You are an academic paper reader。",
|
89 |
-
)
|
90 |
-
|
91 |
-
# 多线,翻译
|
92 |
-
inputs_array = []
|
93 |
-
inputs_show_user_array = []
|
94 |
-
|
95 |
-
# get_token_num
|
96 |
-
from request_llm.bridge_all import model_info
|
97 |
-
enc = model_info[llm_kwargs['llm_model']]['tokenizer']
|
98 |
-
def get_token_num(txt): return len(enc.encode(txt, disallowed_special=()))
|
99 |
-
from .crazy_utils import breakdown_txt_to_satisfy_token_limit_for_pdf
|
100 |
-
|
101 |
-
def break_down(txt):
|
102 |
-
raw_token_num = get_token_num(txt)
|
103 |
-
if raw_token_num <= TOKEN_LIMIT_PER_FRAGMENT:
|
104 |
-
return [txt]
|
105 |
-
else:
|
106 |
-
# raw_token_num > TOKEN_LIMIT_PER_FRAGMENT
|
107 |
-
# find a smooth token limit to achieve even seperation
|
108 |
-
count = int(math.ceil(raw_token_num / TOKEN_LIMIT_PER_FRAGMENT))
|
109 |
-
token_limit_smooth = raw_token_num // count + count
|
110 |
-
return breakdown_txt_to_satisfy_token_limit_for_pdf(txt, get_token_fn=get_token_num, limit=token_limit_smooth)
|
111 |
-
|
112 |
-
for section in article_dict.get('sections'):
|
113 |
-
if len(section['text']) == 0: continue
|
114 |
-
section_frags = break_down(section['text'])
|
115 |
-
for i, fragment in enumerate(section_frags):
|
116 |
-
heading = section['heading']
|
117 |
-
if len(section_frags) > 1: heading += f' Part-{i+1}'
|
118 |
-
inputs_array.append(
|
119 |
-
f"你需要翻译{heading}章节,内容如下: \n\n{fragment}"
|
120 |
-
)
|
121 |
-
inputs_show_user_array.append(
|
122 |
-
f"# {heading}\n\n{fragment}"
|
123 |
-
)
|
124 |
-
|
125 |
-
gpt_response_collection = yield from request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
126 |
-
inputs_array=inputs_array,
|
127 |
-
inputs_show_user_array=inputs_show_user_array,
|
128 |
-
llm_kwargs=llm_kwargs,
|
129 |
-
chatbot=chatbot,
|
130 |
-
history_array=[meta for _ in inputs_array],
|
131 |
-
sys_prompt_array=[
|
132 |
-
"请你作为一个学术翻译,负责把学术论文准确翻���成中文。注意文章中的每一句话都要翻译。" for _ in inputs_array],
|
133 |
-
)
|
134 |
-
res_path = write_history_to_file(meta + ["# Meta Translation" , paper_meta_info] + gpt_response_collection, file_basename=None, file_fullname=None)
|
135 |
-
promote_file_to_downloadzone(res_path, rename_file=os.path.basename(fp)+'.md', chatbot=chatbot)
|
136 |
-
generated_conclusion_files.append(res_path)
|
137 |
-
|
138 |
-
ch = construct_html()
|
139 |
-
orig = ""
|
140 |
-
trans = ""
|
141 |
-
gpt_response_collection_html = copy.deepcopy(gpt_response_collection)
|
142 |
-
for i,k in enumerate(gpt_response_collection_html):
|
143 |
-
if i%2==0:
|
144 |
-
gpt_response_collection_html[i] = inputs_show_user_array[i//2]
|
145 |
-
else:
|
146 |
-
gpt_response_collection_html[i] = gpt_response_collection_html[i]
|
147 |
-
|
148 |
-
final = ["", "", "一、论文概况", "", "Abstract", paper_meta_info, "二、论文翻译", ""]
|
149 |
-
final.extend(gpt_response_collection_html)
|
150 |
-
for i, k in enumerate(final):
|
151 |
-
if i%2==0:
|
152 |
-
orig = k
|
153 |
-
if i%2==1:
|
154 |
-
trans = k
|
155 |
-
ch.add_row(a=orig, b=trans)
|
156 |
-
create_report_file_name = f"{os.path.basename(fp)}.trans.html"
|
157 |
-
html_file = ch.save_file(create_report_file_name)
|
158 |
-
generated_html_files.append(html_file)
|
159 |
-
promote_file_to_downloadzone(html_file, rename_file=os.path.basename(html_file), chatbot=chatbot)
|
160 |
-
|
161 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))
|
162 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
163 |
|
164 |
|
165 |
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
|
|
|
|
|
|
166 |
import copy
|
167 |
-
TOKEN_LIMIT_PER_FRAGMENT =
|
168 |
generated_conclusion_files = []
|
169 |
generated_html_files = []
|
170 |
from crazy_functions.crazy_utils import construct_html
|
|
|
1 |
+
from toolbox import CatchException, report_execption, get_log_folder, gen_time_str
|
2 |
from toolbox import update_ui, promote_file_to_downloadzone, update_ui_lastest_msg, disable_auto_promotion
|
3 |
from toolbox import write_history_to_file, promote_file_to_downloadzone
|
4 |
from .crazy_utils import request_gpt_model_in_new_thread_with_ui_alive
|
5 |
from .crazy_utils import request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency
|
6 |
from .crazy_utils import read_and_clean_pdf_text
|
7 |
+
from .pdf_fns.parse_pdf import parse_pdf, get_avail_grobid_url, translate_pdf
|
8 |
from colorful import *
|
9 |
+
import copy
|
10 |
import os
|
11 |
import math
|
12 |
|
|
|
58 |
|
59 |
|
60 |
def 解析PDF_基于GROBID(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, grobid_url):
|
61 |
+
import copy, json
|
62 |
+
TOKEN_LIMIT_PER_FRAGMENT = 1024
|
63 |
generated_conclusion_files = []
|
64 |
generated_html_files = []
|
65 |
DST_LANG = "中文"
|
|
|
67 |
for index, fp in enumerate(file_manifest):
|
68 |
chatbot.append(["当前进度:", f"正在连接GROBID服务,请稍候: {grobid_url}\n如果等待时间过长,请修改config中的GROBID_URL,可修改成本地GROBID服务。"]); yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
69 |
article_dict = parse_pdf(fp, grobid_url)
|
70 |
+
grobid_json_res = os.path.join(get_log_folder(), gen_time_str() + "grobid.json")
|
71 |
+
with open(grobid_json_res, 'w+', encoding='utf8') as f:
|
72 |
+
f.write(json.dumps(article_dict, indent=4, ensure_ascii=False))
|
73 |
+
promote_file_to_downloadzone(grobid_json_res, chatbot=chatbot)
|
74 |
+
|
75 |
if article_dict is None: raise RuntimeError("解析PDF失败,请检查PDF是否损坏。")
|
76 |
+
yield from translate_pdf(article_dict, llm_kwargs, chatbot, fp, generated_conclusion_files, TOKEN_LIMIT_PER_FRAGMENT, DST_LANG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
chatbot.append(("给出输出文件清单", str(generated_conclusion_files + generated_html_files)))
|
78 |
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
79 |
|
80 |
|
81 |
def 解析PDF(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
|
82 |
+
"""
|
83 |
+
此函数已经弃用
|
84 |
+
"""
|
85 |
import copy
|
86 |
+
TOKEN_LIMIT_PER_FRAGMENT = 1024
|
87 |
generated_conclusion_files = []
|
88 |
generated_html_files = []
|
89 |
from crazy_functions.crazy_utils import construct_html
|
crazy_functions/解析项目源代码.py
CHANGED
@@ -136,6 +136,23 @@ def 解析一个Python项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, s
|
|
136 |
return
|
137 |
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
@CatchException
|
141 |
def 解析一个C项目的头文件(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
|
|
136 |
return
|
137 |
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
138 |
|
139 |
+
@CatchException
|
140 |
+
def 解析一个Matlab项目(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
141 |
+
history = [] # 清空历史,以免输入溢出
|
142 |
+
import glob, os
|
143 |
+
if os.path.exists(txt):
|
144 |
+
project_folder = txt
|
145 |
+
else:
|
146 |
+
if txt == "": txt = '空空如也的输入栏'
|
147 |
+
report_execption(chatbot, history, a = f"解析Matlab项目: {txt}", b = f"找不到本地项目或无权访问: {txt}")
|
148 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
149 |
+
return
|
150 |
+
file_manifest = [f for f in glob.glob(f'{project_folder}/**/*.m', recursive=True)]
|
151 |
+
if len(file_manifest) == 0:
|
152 |
+
report_execption(chatbot, history, a = f"解析Matlab项目: {txt}", b = f"找不到任何`.m`源文件: {txt}")
|
153 |
+
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面
|
154 |
+
return
|
155 |
+
yield from 解析源代码新(file_manifest, project_folder, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt)
|
156 |
|
157 |
@CatchException
|
158 |
def 解析一个C项目的头文件(txt, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt, web_port):
|
docker-compose.yml
CHANGED
@@ -1,4 +1,81 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
## ===================================================
|
4 |
## 【方案一】 如果不需要运行本地模型(仅 chatgpt, azure, 星火, 千帆, claude 等在线大模型服务)
|
|
|
1 |
+
## ===================================================
|
2 |
+
# docker-compose.yml
|
3 |
+
## ===================================================
|
4 |
+
# 1. 请在以下方案中选择任意一种,然后删除其他的方案
|
5 |
+
# 2. 修改你选择的方案中的environment环境变量,详情请见github wiki或者config.py
|
6 |
+
# 3. 选择一种暴露服务端口的方法,并对相应的配置做出修改:
|
7 |
+
# 【方法1: 适用于Linux,很方便,可惜windows不支持】与宿主的网络融合为一体,这个是默认配置
|
8 |
+
# network_mode: "host"
|
9 |
+
# 【方法2: 适用于所有系统包括Windows和MacOS】端口映射,把容器的端口映射到宿主的端口(注意您需要先删除network_mode: "host",再追加以下内容)
|
10 |
+
# ports:
|
11 |
+
# - "12345:12345" # 注意!12345必须与WEB_PORT环境变量相互对应
|
12 |
+
# 4. 最后`docker-compose up`运行
|
13 |
+
# 5. 如果希望使用显卡,请关注 LOCAL_MODEL_DEVICE 和 英伟达显卡运行时 选项
|
14 |
+
## ===================================================
|
15 |
+
# 1. Please choose one of the following options and delete the others.
|
16 |
+
# 2. Modify the environment variables in the selected option, see GitHub wiki or config.py for more details.
|
17 |
+
# 3. Choose a method to expose the server port and make the corresponding configuration changes:
|
18 |
+
# [Method 1: Suitable for Linux, convenient, but not supported for Windows] Fusion with the host network, this is the default configuration
|
19 |
+
# network_mode: "host"
|
20 |
+
# [Method 2: Suitable for all systems including Windows and MacOS] Port mapping, mapping the container port to the host port (note that you need to delete network_mode: "host" first, and then add the following content)
|
21 |
+
# ports:
|
22 |
+
# - "12345: 12345" # Note! 12345 must correspond to the WEB_PORT environment variable.
|
23 |
+
# 4. Finally, run `docker-compose up`.
|
24 |
+
# 5. If you want to use a graphics card, pay attention to the LOCAL_MODEL_DEVICE and Nvidia GPU runtime options.
|
25 |
+
## ===================================================
|
26 |
+
|
27 |
+
## ===================================================
|
28 |
+
## 【方案零】 部署项目的全部能力(这个是包含cuda和latex的大型镜像。如果您网速慢、硬盘小或没有显卡,则不推荐使用这个)
|
29 |
+
## ===================================================
|
30 |
+
version: '3'
|
31 |
+
services:
|
32 |
+
gpt_academic_full_capability:
|
33 |
+
image: ghcr.io/binary-husky/gpt_academic_with_all_capacity:master
|
34 |
+
environment:
|
35 |
+
# 请查阅 `config.py`或者 github wiki 以查看所有的配置信息
|
36 |
+
API_KEY: ' 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 '
|
37 |
+
# USE_PROXY: ' True '
|
38 |
+
# proxies: ' { "http": "http://localhost:10881", "https": "http://localhost:10881", } '
|
39 |
+
LLM_MODEL: ' gpt-3.5-turbo '
|
40 |
+
AVAIL_LLM_MODELS: ' ["gpt-3.5-turbo", "gpt-4", "qianfan", "sparkv2", "spark", "chatglm"] '
|
41 |
+
BAIDU_CLOUD_API_KEY : ' bTUtwEAveBrQipEowUvDwYWq '
|
42 |
+
BAIDU_CLOUD_SECRET_KEY : ' jqXtLvXiVw6UNdjliATTS61rllG8Iuni '
|
43 |
+
XFYUN_APPID: ' 53a8d816 '
|
44 |
+
XFYUN_API_SECRET: ' MjMxNDQ4NDE4MzM0OSNlNjQ2NTlhMTkx '
|
45 |
+
XFYUN_API_KEY: ' 95ccdec285364869d17b33e75ee96447 '
|
46 |
+
ENABLE_AUDIO: ' False '
|
47 |
+
DEFAULT_WORKER_NUM: ' 20 '
|
48 |
+
WEB_PORT: ' 12345 '
|
49 |
+
ADD_WAIFU: ' False '
|
50 |
+
ALIYUN_APPKEY: ' RxPlZrM88DnAFkZK '
|
51 |
+
THEME: ' Chuanhu-Small-and-Beautiful '
|
52 |
+
ALIYUN_ACCESSKEY: ' LTAI5t6BrFUzxRXVGUWnekh1 '
|
53 |
+
ALIYUN_SECRET: ' eHmI20SVWIwQZxCiTD2bGQVspP9i68 '
|
54 |
+
# LOCAL_MODEL_DEVICE: ' cuda '
|
55 |
+
|
56 |
+
# 加载英伟达显卡运行时
|
57 |
+
# runtime: nvidia
|
58 |
+
# deploy:
|
59 |
+
# resources:
|
60 |
+
# reservations:
|
61 |
+
# devices:
|
62 |
+
# - driver: nvidia
|
63 |
+
# count: 1
|
64 |
+
# capabilities: [gpu]
|
65 |
+
|
66 |
+
# 【WEB_PORT暴露方法1: 适用于Linux】与宿主的网络融合
|
67 |
+
network_mode: "host"
|
68 |
+
|
69 |
+
# 【WEB_PORT暴露方法2: 适用于所有系统】端口映射
|
70 |
+
# ports:
|
71 |
+
# - "12345:12345" # 12345必须与WEB_PORT相互对应
|
72 |
+
|
73 |
+
# 启动容器后,运行main.py主程序
|
74 |
+
command: >
|
75 |
+
bash -c "python3 -u main.py"
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
|
80 |
## ===================================================
|
81 |
## 【方案一】 如果不需要运行本地模型(仅 chatgpt, azure, 星火, 千帆, claude 等在线大模型服务)
|
docs/GithubAction+AllCapacity
CHANGED
@@ -13,21 +13,20 @@ RUN python3 -m pip install openai numpy arxiv rich
|
|
13 |
RUN python3 -m pip install colorama Markdown pygments pymupdf
|
14 |
RUN python3 -m pip install python-docx moviepy pdfminer
|
15 |
RUN python3 -m pip install zh_langchain==0.2.1 pypinyin
|
16 |
-
RUN python3 -m pip install nougat-ocr
|
17 |
RUN python3 -m pip install rarfile py7zr
|
18 |
RUN python3 -m pip install aliyun-python-sdk-core==2.13.3 pyOpenSSL scipy git+https://github.com/aliyun/alibabacloud-nls-python-sdk.git
|
19 |
# 下载分支
|
20 |
WORKDIR /gpt
|
21 |
RUN git clone --depth=1 https://github.com/binary-husky/gpt_academic.git
|
22 |
WORKDIR /gpt/gpt_academic
|
23 |
-
RUN git clone https://github.com/OpenLMLab/MOSS.git request_llm/moss
|
24 |
|
25 |
RUN python3 -m pip install -r requirements.txt
|
26 |
RUN python3 -m pip install -r request_llm/requirements_moss.txt
|
27 |
RUN python3 -m pip install -r request_llm/requirements_qwen.txt
|
28 |
RUN python3 -m pip install -r request_llm/requirements_chatglm.txt
|
29 |
RUN python3 -m pip install -r request_llm/requirements_newbing.txt
|
30 |
-
|
31 |
|
32 |
|
33 |
# 预热Tiktoken模块
|
|
|
13 |
RUN python3 -m pip install colorama Markdown pygments pymupdf
|
14 |
RUN python3 -m pip install python-docx moviepy pdfminer
|
15 |
RUN python3 -m pip install zh_langchain==0.2.1 pypinyin
|
|
|
16 |
RUN python3 -m pip install rarfile py7zr
|
17 |
RUN python3 -m pip install aliyun-python-sdk-core==2.13.3 pyOpenSSL scipy git+https://github.com/aliyun/alibabacloud-nls-python-sdk.git
|
18 |
# 下载分支
|
19 |
WORKDIR /gpt
|
20 |
RUN git clone --depth=1 https://github.com/binary-husky/gpt_academic.git
|
21 |
WORKDIR /gpt/gpt_academic
|
22 |
+
RUN git clone --depth=1 https://github.com/OpenLMLab/MOSS.git request_llm/moss
|
23 |
|
24 |
RUN python3 -m pip install -r requirements.txt
|
25 |
RUN python3 -m pip install -r request_llm/requirements_moss.txt
|
26 |
RUN python3 -m pip install -r request_llm/requirements_qwen.txt
|
27 |
RUN python3 -m pip install -r request_llm/requirements_chatglm.txt
|
28 |
RUN python3 -m pip install -r request_llm/requirements_newbing.txt
|
29 |
+
RUN python3 -m pip install nougat-ocr
|
30 |
|
31 |
|
32 |
# 预热Tiktoken模块
|
docs/GithubAction+NoLocal+Latex
CHANGED
@@ -5,6 +5,9 @@
|
|
5 |
|
6 |
FROM fuqingxu/python311_texlive_ctex:latest
|
7 |
|
|
|
|
|
|
|
8 |
# 指定路径
|
9 |
WORKDIR /gpt
|
10 |
|
|
|
5 |
|
6 |
FROM fuqingxu/python311_texlive_ctex:latest
|
7 |
|
8 |
+
# 删除文档文件以节约空间
|
9 |
+
RUN rm -rf /usr/local/texlive/2023/texmf-dist/doc
|
10 |
+
|
11 |
# 指定路径
|
12 |
WORKDIR /gpt
|
13 |
|
docs/translate_english.json
CHANGED
@@ -322,7 +322,7 @@
|
|
322 |
"任何文件": "Any file",
|
323 |
"但推荐上传压缩文件": "But it is recommended to upload compressed files",
|
324 |
"更换模型 & SysPrompt & 交互界面布局": "Change model & SysPrompt & interactive interface layout",
|
325 |
-
"
|
326 |
"输入清除键": "Input clear key",
|
327 |
"插件参数区": "Plugin parameter area",
|
328 |
"显示/隐藏功能区": "Show/hide function area",
|
@@ -2513,5 +2513,141 @@
|
|
2513 |
"此处待注入的知识库名称id": "The knowledge base name ID to be injected here",
|
2514 |
"您需要构建知识库后再运行此插件": "You need to build the knowledge base before running this plugin",
|
2515 |
"判定是否为公式 | 测试1 写出洛伦兹定律": "Determine whether it is a formula | Test 1 write out the Lorentz law",
|
2516 |
-
"构建知识库后": "After building the knowledge base"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2517 |
}
|
|
|
322 |
"任何文件": "Any file",
|
323 |
"但推荐上传压缩文件": "But it is recommended to upload compressed files",
|
324 |
"更换模型 & SysPrompt & 交互界面布局": "Change model & SysPrompt & interactive interface layout",
|
325 |
+
"浮动输入区": "Floating input area",
|
326 |
"输入清除键": "Input clear key",
|
327 |
"插件参数区": "Plugin parameter area",
|
328 |
"显示/隐藏功能区": "Show/hide function area",
|
|
|
2513 |
"此处待注入的知识库名称id": "The knowledge base name ID to be injected here",
|
2514 |
"您需要构建知识库后再运行此插件": "You need to build the knowledge base before running this plugin",
|
2515 |
"判定是否为公式 | 测试1 写出洛伦兹定律": "Determine whether it is a formula | Test 1 write out the Lorentz law",
|
2516 |
+
"构建知识库后": "After building the knowledge base",
|
2517 |
+
"找不到本地项目或无法处理": "Unable to find local project or unable to process",
|
2518 |
+
"再做一个小修改": "Make another small modification",
|
2519 |
+
"解析整个Matlab项目": "Parse the entire Matlab project",
|
2520 |
+
"需要用GPT提取参数": "Need to extract parameters using GPT",
|
2521 |
+
"文件路径": "File path",
|
2522 |
+
"正在排队": "In queue",
|
2523 |
+
"-=-=-=-=-=-=-=-= 写出第1个文件": "-=-=-=-=-=-=-=-= Write the first file",
|
2524 |
+
"仅翻译后的文本 -=-=-=-=-=-=-=-=": "Translated text only -=-=-=-=-=-=-=-=",
|
2525 |
+
"对话通道": "Conversation channel",
|
2526 |
+
"找不到任何": "Unable to find any",
|
2527 |
+
"正在启动": "Starting",
|
2528 |
+
"开始创建新进程并执行代码! 时间限制": "Start creating a new process and executing the code! Time limit",
|
2529 |
+
"解析Matlab项目": "Parse Matlab project",
|
2530 |
+
"更换UI主题": "Change UI theme",
|
2531 |
+
"⭐ 开始啦 !": "⭐ Let's start!",
|
2532 |
+
"先提取当前英文标题": "First extract the current English title",
|
2533 |
+
"睡一会防止触发google反爬虫": "Sleep for a while to prevent triggering Google anti-crawler",
|
2534 |
+
"测试": "Test",
|
2535 |
+
"-=-=-=-=-=-=-=-= 写出Markdown文件 -=-=-=-=-=-=-=-=": "-=-=-=-=-=-=-=-= Write out Markdown file",
|
2536 |
+
"如果index是1的话": "If the index is 1",
|
2537 |
+
"VoidTerminal已经实现了类似的代码": "VoidTerminal has already implemented similar code",
|
2538 |
+
"等待线程锁": "Waiting for thread lock",
|
2539 |
+
"那么我们默认代理生效": "Then we default to proxy",
|
2540 |
+
"结果是一个有效文件": "The result is a valid file",
|
2541 |
+
"⭐ 检查模块": "⭐ Check module",
|
2542 |
+
"备份一份History作为记录": "Backup a copy of History as a record",
|
2543 |
+
"作者Binary-Husky": "Author Binary-Husky",
|
2544 |
+
"将csv文件转excel表格": "Convert CSV file to Excel table",
|
2545 |
+
"获取文章摘要": "Get article summary",
|
2546 |
+
"次代码生成尝试": "Attempt to generate code",
|
2547 |
+
"如果参数是空的": "If the parameter is empty",
|
2548 |
+
"请配置讯飞星火大模型的XFYUN_APPID": "Please configure XFYUN_APPID for the Xunfei Starfire model",
|
2549 |
+
"-=-=-=-=-=-=-=-= 写出第2个文件": "Write the second file",
|
2550 |
+
"代码生成阶段结束": "Code generation phase completed",
|
2551 |
+
"则进行提醒": "Then remind",
|
2552 |
+
"处理异常": "Handle exception",
|
2553 |
+
"可能触发了google反爬虫机制": "May have triggered Google anti-crawler mechanism",
|
2554 |
+
"AnalyzeAMatlabProject的所有源文件": "All source files of AnalyzeAMatlabProject",
|
2555 |
+
"写入": "Write",
|
2556 |
+
"我们5秒后再试一次...": "Let's try again in 5 seconds...",
|
2557 |
+
"判断一下用户是否错误地通过对话通道进入": "Check if the user entered through the dialogue channel by mistake",
|
2558 |
+
"结果": "Result",
|
2559 |
+
"2. 如果没有文件": "2. If there is no file",
|
2560 |
+
"由 test_on_sentence_end": "By test_on_sentence_end",
|
2561 |
+
"则直接使用first section name": "Then directly use the first section name",
|
2562 |
+
"太懒了": "Too lazy",
|
2563 |
+
"记录当前的大章节标题": "Record the current chapter title",
|
2564 |
+
"然后再次点击该插件! 至于您的文件": "Then click the plugin again! As for your file",
|
2565 |
+
"此次我们的错误追踪是": "This time our error tracking is",
|
2566 |
+
"首先在arxiv上搜索": "First search on arxiv",
|
2567 |
+
"被新插件取代": "Replaced by a new plugin",
|
2568 |
+
"正在处理文件": "Processing file",
|
2569 |
+
"除了连接OpenAI之外": "In addition to connecting OpenAI",
|
2570 |
+
"我们检查一下": "Let's check",
|
2571 |
+
"进度": "Progress",
|
2572 |
+
"处理少数情况下的特殊插件的锁定状态": "Handle the locked state of special plugins in a few cases",
|
2573 |
+
"⭐ 开始执行": "⭐ Start execution",
|
2574 |
+
"正常情况": "Normal situation",
|
2575 |
+
"下个句子中已经说完的部分": "The part that has already been said in the next sentence",
|
2576 |
+
"首次运行需要花费较长时间下载NOUGAT参数": "The first run takes a long time to download NOUGAT parameters",
|
2577 |
+
"使用tex格式公式 测试2 给出柯西不等式": "Use the tex format formula to test 2 and give the Cauchy inequality",
|
2578 |
+
"无法从bing获取信息!": "Unable to retrieve information from Bing!",
|
2579 |
+
"秒. 请等待任务完成": "Wait for the task to complete",
|
2580 |
+
"开始干正事": "Start doing real work",
|
2581 |
+
"需要花费较长时间下载NOUGAT参数": "It takes a long time to download NOUGAT parameters",
|
2582 |
+
"然后再次点击该插件": "Then click the plugin again",
|
2583 |
+
"受到bing限制": "Restricted by Bing",
|
2584 |
+
"检索文章的历史版本的题目": "Retrieve the titles of historical versions of the article",
|
2585 |
+
"收尾": "Wrap up",
|
2586 |
+
"给定了task": "Given a task",
|
2587 |
+
"某段话的整个句子": "The whole sentence of a paragraph",
|
2588 |
+
"-=-=-=-=-=-=-=-= 写出HTML文件 -=-=-=-=-=-=-=-=": "-=-=-=-=-=-=-=-= Write out HTML file -=-=-=-=-=-=-=-=",
|
2589 |
+
"当前文件": "Current file",
|
2590 |
+
"请在输入框内填写需求": "Please fill in the requirements in the input box",
|
2591 |
+
"结果是一个字符串": "The result is a string",
|
2592 |
+
"用插件实现」": "Implemented with a plugin",
|
2593 |
+
"⭐ 到最后一步了": "⭐ Reached the final step",
|
2594 |
+
"重新修改当前part的标题": "Modify the title of the current part again",
|
2595 |
+
"请勿点击“提交”按钮或者“基础功能区”按钮": "Do not click the 'Submit' button or the 'Basic Function Area' button",
|
2596 |
+
"正在执行命令": "Executing command",
|
2597 |
+
"检测到**滞留的缓存文档**": "Detected **stuck cache document**",
|
2598 |
+
"第三步": "Step three",
|
2599 |
+
"失败了~ 别担心": "Failed~ Don't worry",
|
2600 |
+
"动态代码解释器": "Dynamic code interpreter",
|
2601 |
+
"开始执行": "Start executing",
|
2602 |
+
"不给定task": "No task given",
|
2603 |
+
"正在加载NOUGAT...": "Loading NOUGAT...",
|
2604 |
+
"精准翻译PDF文档": "Accurate translation of PDF documents",
|
2605 |
+
"时间限制TIME_LIMIT": "Time limit TIME_LIMIT",
|
2606 |
+
"翻译前后混合 -=-=-=-=-=-=-=-=": "Mixed translation before and after -=-=-=-=-=-=-=-=",
|
2607 |
+
"搞定代码生成": "Code generation is done",
|
2608 |
+
"插件通道": "Plugin channel",
|
2609 |
+
"智能体": "Intelligent agent",
|
2610 |
+
"切换界面明暗 ☀": "Switch interface brightness ☀",
|
2611 |
+
"交换图像的蓝色通道和红色通道": "Swap blue channel and red channel of the image",
|
2612 |
+
"作为函数参数": "As a function parameter",
|
2613 |
+
"先挑选偶数序列号": "First select even serial numbers",
|
2614 |
+
"仅供测试": "For testing only",
|
2615 |
+
"执行成功了": "Execution succeeded",
|
2616 |
+
"开始逐个文件进行处理": "Start processing files one by one",
|
2617 |
+
"当前文件处理列表": "Current file processing list",
|
2618 |
+
"执行失败了": "Execution failed",
|
2619 |
+
"请及时处理": "Please handle it in time",
|
2620 |
+
"源文件": "Source file",
|
2621 |
+
"裁剪图像": "Crop image",
|
2622 |
+
"插件动态生成插件": "Dynamic generation of plugins",
|
2623 |
+
"正在验证上述代码的有效性": "Validating the above code",
|
2624 |
+
"⭐ = 关键步骤": "⭐ = Key step",
|
2625 |
+
"!= 0 代表“提交”键对话通道": "!= 0 represents the 'Submit' key dialogue channel",
|
2626 |
+
"解析python源代码项目": "Parsing Python source code project",
|
2627 |
+
"请检查PDF是否损坏": "Please check if the PDF is damaged",
|
2628 |
+
"插件动态生成": "Dynamic generation of plugins",
|
2629 |
+
"⭐ 分离代码块": "⭐ Separating code blocks",
|
2630 |
+
"已经被记忆": "Already memorized",
|
2631 |
+
"默认用英文的": "Default to English",
|
2632 |
+
"错误追踪": "Error tracking",
|
2633 |
+
"对话|编程|学术|智能体": "Dialogue|Programming|Academic|Intelligent agent",
|
2634 |
+
"请检查": "Please check",
|
2635 |
+
"检测到被滞留的缓存文档": "Detected cached documents being left behind",
|
2636 |
+
"还有哪些场合允许使用代理": "What other occasions allow the use of proxies",
|
2637 |
+
"1. 如果有文件": "1. If there is a file",
|
2638 |
+
"执行开始": "Execution starts",
|
2639 |
+
"代码生成结束": "Code generation ends",
|
2640 |
+
"请及时点击“**保存当前对话**”获取所有滞留文档": "Please click '**Save Current Dialogue**' in time to obtain all cached documents",
|
2641 |
+
"需点击“**函数插件区**”按钮进行处理": "Click the '**Function Plugin Area**' button for processing",
|
2642 |
+
"此函数已经弃用": "This function has been deprecated",
|
2643 |
+
"以后再写": "Write it later",
|
2644 |
+
"返回给定的url解析出的arxiv_id": "Return the arxiv_id parsed from the given URL",
|
2645 |
+
"⭐ 文件上传区是否有东西": "⭐ Is there anything in the file upload area",
|
2646 |
+
"Nougat解析论文失败": "Nougat failed to parse the paper",
|
2647 |
+
"本源代码中": "In this source code",
|
2648 |
+
"或者基础功能通道": "Or the basic function channel",
|
2649 |
+
"使用zip压缩格式": "Using zip compression format",
|
2650 |
+
"受到google限制": "Restricted by Google",
|
2651 |
+
"如果是": "If it is",
|
2652 |
+
"不用担心": "don't worry"
|
2653 |
}
|
docs/translate_japanese.json
CHANGED
@@ -1007,7 +1007,6 @@
|
|
1007 |
"第一部分": "第1部分",
|
1008 |
"的分析如下": "の分析は以下の通りです",
|
1009 |
"解决一个mdx_math的bug": "mdx_mathのバグを解決する",
|
1010 |
-
"底部输入区": "下部の入力エリア",
|
1011 |
"函数插件输入输出接驳区": "関数プラグインの入出力接続エリア",
|
1012 |
"打开浏览器": "ブラウザを開く",
|
1013 |
"免费用户填3": "無料ユーザーは3を入力してください",
|
|
|
1007 |
"第一部分": "第1部分",
|
1008 |
"的分析如下": "の分析は以下の通りです",
|
1009 |
"解决一个mdx_math的bug": "mdx_mathのバグを解決する",
|
|
|
1010 |
"函数插件输入输出接驳区": "関数プラグインの入出力接続エリア",
|
1011 |
"打开浏览器": "ブラウザを開く",
|
1012 |
"免费用户填3": "無料ユーザーは3を入力してください",
|
docs/translate_std.json
CHANGED
@@ -90,5 +90,7 @@
|
|
90 |
"解析PDF_基于GROBID": "ParsePDF_BasedOnGROBID",
|
91 |
"虚空终端主路由": "VoidTerminalMainRoute",
|
92 |
"批量翻译PDF文档_NOUGAT": "BatchTranslatePDFDocuments_NOUGAT",
|
93 |
-
"解析PDF_基于NOUGAT": "ParsePDF_NOUGAT"
|
|
|
|
|
94 |
}
|
|
|
90 |
"解析PDF_基于GROBID": "ParsePDF_BasedOnGROBID",
|
91 |
"虚空终端主路由": "VoidTerminalMainRoute",
|
92 |
"批量翻译PDF文档_NOUGAT": "BatchTranslatePDFDocuments_NOUGAT",
|
93 |
+
"解析PDF_基于NOUGAT": "ParsePDF_NOUGAT",
|
94 |
+
"解析一个Matlab项目": "AnalyzeAMatlabProject",
|
95 |
+
"函数动态生成": "DynamicFunctionGeneration"
|
96 |
}
|
docs/translate_traditionalchinese.json
CHANGED
@@ -346,7 +346,6 @@
|
|
346 |
"情况会好转": "情況會好轉",
|
347 |
"超过512个": "超過512個",
|
348 |
"多线": "多線",
|
349 |
-
"底部输入区": "底部輸入區",
|
350 |
"合并小写字母开头的段落块并替换为空格": "合併小寫字母開頭的段落塊並替換為空格",
|
351 |
"暗色主题": "暗色主題",
|
352 |
"提高限制请查询": "提高限制請查詢",
|
|
|
346 |
"情况会好转": "情況會好轉",
|
347 |
"超过512个": "超過512個",
|
348 |
"多线": "多線",
|
|
|
349 |
"合并小写字母开头的段落块并替换为空格": "合併小寫字母開頭的段落塊並替換為空格",
|
350 |
"暗色主题": "暗色主題",
|
351 |
"提高限制请查询": "提高限制請查詢",
|
docs/use_azure.md
CHANGED
@@ -107,6 +107,12 @@ AZURE_API_KEY = "填入azure openai api的密钥"
|
|
107 |
AZURE_API_VERSION = "2023-05-15" # 默认使用 2023-05-15 版本,无需修改
|
108 |
AZURE_ENGINE = "填入部署名" # 见上述图片
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
```
|
111 |
|
112 |
|
|
|
107 |
AZURE_API_VERSION = "2023-05-15" # 默认使用 2023-05-15 版本,无需修改
|
108 |
AZURE_ENGINE = "填入部署名" # 见上述图片
|
109 |
|
110 |
+
|
111 |
+
# 例如
|
112 |
+
API_KEY = '6424e9d19e674092815cea1cb35e67a5'
|
113 |
+
AZURE_ENDPOINT = 'https://rhtjjjjjj.openai.azure.com/'
|
114 |
+
AZURE_ENGINE = 'qqwe'
|
115 |
+
LLM_MODEL = "azure-gpt-3.5" # 可选 ↓↓↓
|
116 |
```
|
117 |
|
118 |
|
request_llm/bridge_all.py
CHANGED
@@ -52,6 +52,7 @@ API_URL_REDIRECT, AZURE_ENDPOINT, AZURE_ENGINE = get_conf("API_URL_REDIRECT", "A
|
|
52 |
openai_endpoint = "https://api.openai.com/v1/chat/completions"
|
53 |
api2d_endpoint = "https://openai.api2d.net/v1/chat/completions"
|
54 |
newbing_endpoint = "wss://sydney.bing.com/sydney/ChatHub"
|
|
|
55 |
azure_endpoint = AZURE_ENDPOINT + f'openai/deployments/{AZURE_ENGINE}/chat/completions?api-version=2023-05-15'
|
56 |
# 兼容旧版的配置
|
57 |
try:
|
@@ -125,6 +126,15 @@ model_info = {
|
|
125 |
"token_cnt": get_token_num_gpt4,
|
126 |
},
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
# azure openai
|
129 |
"azure-gpt-3.5":{
|
130 |
"fn_with_ui": chatgpt_ui,
|
@@ -135,6 +145,15 @@ model_info = {
|
|
135 |
"token_cnt": get_token_num_gpt35,
|
136 |
},
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
# api_2d
|
139 |
"api2d-gpt-3.5-turbo": {
|
140 |
"fn_with_ui": chatgpt_ui,
|
|
|
52 |
openai_endpoint = "https://api.openai.com/v1/chat/completions"
|
53 |
api2d_endpoint = "https://openai.api2d.net/v1/chat/completions"
|
54 |
newbing_endpoint = "wss://sydney.bing.com/sydney/ChatHub"
|
55 |
+
if not AZURE_ENDPOINT.endswith('/'): AZURE_ENDPOINT += '/'
|
56 |
azure_endpoint = AZURE_ENDPOINT + f'openai/deployments/{AZURE_ENGINE}/chat/completions?api-version=2023-05-15'
|
57 |
# 兼容旧版的配置
|
58 |
try:
|
|
|
126 |
"token_cnt": get_token_num_gpt4,
|
127 |
},
|
128 |
|
129 |
+
"gpt-4-32k": {
|
130 |
+
"fn_with_ui": chatgpt_ui,
|
131 |
+
"fn_without_ui": chatgpt_noui,
|
132 |
+
"endpoint": openai_endpoint,
|
133 |
+
"max_token": 32768,
|
134 |
+
"tokenizer": tokenizer_gpt4,
|
135 |
+
"token_cnt": get_token_num_gpt4,
|
136 |
+
},
|
137 |
+
|
138 |
# azure openai
|
139 |
"azure-gpt-3.5":{
|
140 |
"fn_with_ui": chatgpt_ui,
|
|
|
145 |
"token_cnt": get_token_num_gpt35,
|
146 |
},
|
147 |
|
148 |
+
"azure-gpt-4":{
|
149 |
+
"fn_with_ui": chatgpt_ui,
|
150 |
+
"fn_without_ui": chatgpt_noui,
|
151 |
+
"endpoint": azure_endpoint,
|
152 |
+
"max_token": 8192,
|
153 |
+
"tokenizer": tokenizer_gpt35,
|
154 |
+
"token_cnt": get_token_num_gpt35,
|
155 |
+
},
|
156 |
+
|
157 |
# api_2d
|
158 |
"api2d-gpt-3.5-turbo": {
|
159 |
"fn_with_ui": chatgpt_ui,
|
request_llm/bridge_chatglm.py
CHANGED
@@ -3,7 +3,7 @@ from transformers import AutoModel, AutoTokenizer
|
|
3 |
import time
|
4 |
import threading
|
5 |
import importlib
|
6 |
-
from toolbox import update_ui, get_conf
|
7 |
from multiprocessing import Process, Pipe
|
8 |
|
9 |
load_message = "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……"
|
@@ -48,16 +48,17 @@ class GetGLMHandle(Process):
|
|
48 |
|
49 |
while True:
|
50 |
try:
|
51 |
-
|
52 |
-
self.
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
-
|
57 |
-
self.chatglm_model = self.chatglm_model.eval()
|
58 |
-
break
|
59 |
-
else:
|
60 |
-
break
|
61 |
except:
|
62 |
retry += 1
|
63 |
if retry > 3:
|
|
|
3 |
import time
|
4 |
import threading
|
5 |
import importlib
|
6 |
+
from toolbox import update_ui, get_conf, ProxyNetworkActivate
|
7 |
from multiprocessing import Process, Pipe
|
8 |
|
9 |
load_message = "ChatGLM尚未加载,加载需要一段时间。注意,取决于`config.py`的配置,ChatGLM消耗大量的内存(CPU)或显存(GPU),也许会导致低配计算机卡死 ……"
|
|
|
48 |
|
49 |
while True:
|
50 |
try:
|
51 |
+
with ProxyNetworkActivate('Download_LLM'):
|
52 |
+
if self.chatglm_model is None:
|
53 |
+
self.chatglm_tokenizer = AutoTokenizer.from_pretrained(_model_name_, trust_remote_code=True)
|
54 |
+
if device=='cpu':
|
55 |
+
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).float()
|
56 |
+
else:
|
57 |
+
self.chatglm_model = AutoModel.from_pretrained(_model_name_, trust_remote_code=True).half().cuda()
|
58 |
+
self.chatglm_model = self.chatglm_model.eval()
|
59 |
+
break
|
60 |
else:
|
61 |
+
break
|
|
|
|
|
|
|
|
|
62 |
except:
|
63 |
retry += 1
|
64 |
if retry > 3:
|
request_llm/bridge_llama2.py
CHANGED
@@ -30,7 +30,7 @@ class GetONNXGLMHandle(LocalLLMHandle):
|
|
30 |
with open(os.path.expanduser('~/.cache/huggingface/token'), 'w') as f:
|
31 |
f.write(huggingface_token)
|
32 |
model_id = 'meta-llama/Llama-2-7b-chat-hf'
|
33 |
-
with ProxyNetworkActivate():
|
34 |
self._tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=huggingface_token)
|
35 |
# use fp16
|
36 |
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=huggingface_token).eval()
|
|
|
30 |
with open(os.path.expanduser('~/.cache/huggingface/token'), 'w') as f:
|
31 |
f.write(huggingface_token)
|
32 |
model_id = 'meta-llama/Llama-2-7b-chat-hf'
|
33 |
+
with ProxyNetworkActivate('Download_LLM'):
|
34 |
self._tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=huggingface_token)
|
35 |
# use fp16
|
36 |
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=huggingface_token).eval()
|
request_llm/requirements_chatglm.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
protobuf
|
2 |
-
transformers>=4.27.1
|
3 |
cpm_kernels
|
4 |
torch>=1.10
|
5 |
mdtex2html
|
|
|
1 |
protobuf
|
|
|
2 |
cpm_kernels
|
3 |
torch>=1.10
|
4 |
mdtex2html
|
request_llm/requirements_chatglm_onnx.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
protobuf
|
2 |
-
transformers>=4.27.1
|
3 |
cpm_kernels
|
4 |
torch>=1.10
|
5 |
mdtex2html
|
|
|
1 |
protobuf
|
|
|
2 |
cpm_kernels
|
3 |
torch>=1.10
|
4 |
mdtex2html
|
request_llm/requirements_jittorllms.txt
CHANGED
@@ -2,6 +2,5 @@ jittor >= 1.3.7.9
|
|
2 |
jtorch >= 0.1.3
|
3 |
torch
|
4 |
torchvision
|
5 |
-
transformers==4.26.1
|
6 |
pandas
|
7 |
jieba
|
|
|
2 |
jtorch >= 0.1.3
|
3 |
torch
|
4 |
torchvision
|
|
|
5 |
pandas
|
6 |
jieba
|
request_llm/requirements_moss.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
torch
|
2 |
-
transformers==4.25.1
|
3 |
sentencepiece
|
4 |
datasets
|
5 |
accelerate
|
|
|
1 |
torch
|
|
|
2 |
sentencepiece
|
3 |
datasets
|
4 |
accelerate
|
requirements.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
pydantic==1.10.11
|
2 |
tiktoken>=0.3.3
|
3 |
requests[socks]
|
4 |
-
transformers
|
5 |
python-markdown-math
|
6 |
beautifulsoup4
|
7 |
prompt_toolkit
|
|
|
1 |
pydantic==1.10.11
|
2 |
tiktoken>=0.3.3
|
3 |
requests[socks]
|
4 |
+
transformers>=4.27.1
|
5 |
python-markdown-math
|
6 |
beautifulsoup4
|
7 |
prompt_toolkit
|
tests/test_plugins.py
CHANGED
@@ -6,11 +6,14 @@
|
|
6 |
import os, sys
|
7 |
def validate_path(): dir_name = os.path.dirname(__file__); root_dir_assume = os.path.abspath(dir_name + '/..'); os.chdir(root_dir_assume); sys.path.append(root_dir_assume)
|
8 |
validate_path() # 返回项目根路径
|
9 |
-
from tests.test_utils import plugin_test
|
10 |
|
11 |
if __name__ == "__main__":
|
|
|
|
|
|
|
12 |
# plugin_test(plugin='crazy_functions.虚空终端->虚空终端', main_input='修改api-key为sk-jhoejriotherjep')
|
13 |
-
|
|
|
14 |
|
15 |
# plugin_test(plugin='crazy_functions.虚空终端->虚空终端', main_input='调用插件,对C:/Users/fuqingxu/Desktop/旧文件/gpt/chatgpt_academic/crazy_functions/latex_fns中的python文件进行解析')
|
16 |
|
|
|
6 |
import os, sys
|
7 |
def validate_path(): dir_name = os.path.dirname(__file__); root_dir_assume = os.path.abspath(dir_name + '/..'); os.chdir(root_dir_assume); sys.path.append(root_dir_assume)
|
8 |
validate_path() # 返回项目根路径
|
|
|
9 |
|
10 |
if __name__ == "__main__":
|
11 |
+
from tests.test_utils import plugin_test
|
12 |
+
plugin_test(plugin='crazy_functions.函数动态生成->函数动态生成', main_input='交换图像的蓝色通道和红色通道', advanced_arg={"file_path_arg": "./build/ants.jpg"})
|
13 |
+
|
14 |
# plugin_test(plugin='crazy_functions.虚空终端->虚空终端', main_input='修改api-key为sk-jhoejriotherjep')
|
15 |
+
|
16 |
+
# plugin_test(plugin='crazy_functions.批量翻译PDF文档_NOUGAT->批量翻译PDF文档', main_input='crazy_functions/test_project/pdf_and_word/aaai.pdf')
|
17 |
|
18 |
# plugin_test(plugin='crazy_functions.虚空终端->虚空终端', main_input='调用插件,对C:/Users/fuqingxu/Desktop/旧文件/gpt/chatgpt_academic/crazy_functions/latex_fns中的python文件进行解析')
|
19 |
|
tests/test_utils.py
CHANGED
@@ -74,7 +74,7 @@ def plugin_test(main_input, plugin, advanced_arg=None):
|
|
74 |
plugin_kwargs['plugin_kwargs'] = advanced_arg
|
75 |
my_working_plugin = silence_stdout(plugin)(**plugin_kwargs)
|
76 |
|
77 |
-
with Live(Markdown(""), auto_refresh=False) as live:
|
78 |
for cookies, chat, hist, msg in my_working_plugin:
|
79 |
md_str = vt.chat_to_markdown_str(chat)
|
80 |
md = Markdown(md_str)
|
|
|
74 |
plugin_kwargs['plugin_kwargs'] = advanced_arg
|
75 |
my_working_plugin = silence_stdout(plugin)(**plugin_kwargs)
|
76 |
|
77 |
+
with Live(Markdown(""), auto_refresh=False, vertical_overflow="visible") as live:
|
78 |
for cookies, chat, hist, msg in my_working_plugin:
|
79 |
md_str = vt.chat_to_markdown_str(chat)
|
80 |
md = Markdown(md_str)
|
themes/common.css
CHANGED
@@ -9,6 +9,11 @@
|
|
9 |
box-shadow: none;
|
10 |
}
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
/* hide selector label */
|
13 |
#input-plugin-group .svelte-1gfkn6j {
|
14 |
visibility: hidden;
|
@@ -19,3 +24,91 @@
|
|
19 |
.wrap.svelte-xwlu1w {
|
20 |
min-height: var(--size-32);
|
21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
box-shadow: none;
|
10 |
}
|
11 |
|
12 |
+
#input-plugin-group .secondary-wrap.svelte-aqlk7e.svelte-aqlk7e.svelte-aqlk7e {
|
13 |
+
border: none;
|
14 |
+
min-width: 0;
|
15 |
+
}
|
16 |
+
|
17 |
/* hide selector label */
|
18 |
#input-plugin-group .svelte-1gfkn6j {
|
19 |
visibility: hidden;
|
|
|
24 |
.wrap.svelte-xwlu1w {
|
25 |
min-height: var(--size-32);
|
26 |
}
|
27 |
+
|
28 |
+
/* status bar height */
|
29 |
+
.min.svelte-1yrv54 {
|
30 |
+
min-height: var(--size-12);
|
31 |
+
}
|
32 |
+
|
33 |
+
/* copy btn */
|
34 |
+
.message-btn-row {
|
35 |
+
width: 19px;
|
36 |
+
height: 19px;
|
37 |
+
position: absolute;
|
38 |
+
left: calc(100% + 3px);
|
39 |
+
top: 0;
|
40 |
+
display: flex;
|
41 |
+
justify-content: space-between;
|
42 |
+
}
|
43 |
+
/* .message-btn-row-leading, .message-btn-row-trailing {
|
44 |
+
display: inline-flex;
|
45 |
+
gap: 4px;
|
46 |
+
} */
|
47 |
+
.message-btn-row button {
|
48 |
+
font-size: 18px;
|
49 |
+
align-self: center;
|
50 |
+
align-items: center;
|
51 |
+
flex-wrap: nowrap;
|
52 |
+
white-space: nowrap;
|
53 |
+
display: inline-flex;
|
54 |
+
flex-direction: row;
|
55 |
+
gap: 4px;
|
56 |
+
padding-block: 2px !important;
|
57 |
+
}
|
58 |
+
|
59 |
+
|
60 |
+
/* Scrollbar Width */
|
61 |
+
::-webkit-scrollbar {
|
62 |
+
width: 12px;
|
63 |
+
}
|
64 |
+
|
65 |
+
/* Scrollbar Track */
|
66 |
+
::-webkit-scrollbar-track {
|
67 |
+
background: #f1f1f1;
|
68 |
+
border-radius: 12px;
|
69 |
+
}
|
70 |
+
|
71 |
+
/* Scrollbar Handle */
|
72 |
+
::-webkit-scrollbar-thumb {
|
73 |
+
background: #888;
|
74 |
+
border-radius: 12px;
|
75 |
+
}
|
76 |
+
|
77 |
+
/* Scrollbar Handle on hover */
|
78 |
+
::-webkit-scrollbar-thumb:hover {
|
79 |
+
background: #555;
|
80 |
+
}
|
81 |
+
|
82 |
+
/* input btns: clear, reset, stop */
|
83 |
+
#input-panel button {
|
84 |
+
min-width: min(80px, 100%);
|
85 |
+
}
|
86 |
+
|
87 |
+
/* input btns: clear, reset, stop */
|
88 |
+
#input-panel2 button {
|
89 |
+
min-width: min(80px, 100%);
|
90 |
+
}
|
91 |
+
|
92 |
+
|
93 |
+
#cbs {
|
94 |
+
background-color: var(--block-background-fill) !important;
|
95 |
+
}
|
96 |
+
|
97 |
+
#interact-panel .form {
|
98 |
+
border: hidden
|
99 |
+
}
|
100 |
+
|
101 |
+
.drag-area {
|
102 |
+
border: solid;
|
103 |
+
border-width: thin;
|
104 |
+
user-select: none;
|
105 |
+
padding-left: 2%;
|
106 |
+
}
|
107 |
+
|
108 |
+
.floating-component #input-panel2 {
|
109 |
+
border-top-left-radius: 0px;
|
110 |
+
border-top-right-radius: 0px;
|
111 |
+
border: solid;
|
112 |
+
border-width: thin;
|
113 |
+
border-top-width: 0;
|
114 |
+
}
|
themes/common.js
CHANGED
@@ -1,4 +1,81 @@
|
|
1 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
function update_height(){
|
3 |
var { panel_height_target, chatbot_height, chatbot } = get_elements(true);
|
4 |
if (panel_height_target!=chatbot_height)
|
@@ -28,6 +105,15 @@ function ChatBotHeight() {
|
|
28 |
}, 50); // 每100毫秒执行一次
|
29 |
}
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
function get_elements(consider_state_panel=false) {
|
32 |
var chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq');
|
33 |
if (!chatbot) {
|
@@ -36,14 +122,14 @@ function get_elements(consider_state_panel=false) {
|
|
36 |
const panel1 = document.querySelector('#input-panel').getBoundingClientRect();
|
37 |
const panel2 = document.querySelector('#basic-panel').getBoundingClientRect()
|
38 |
const panel3 = document.querySelector('#plugin-panel').getBoundingClientRect();
|
39 |
-
const panel4 = document.querySelector('#interact-panel').getBoundingClientRect();
|
40 |
const panel5 = document.querySelector('#input-panel2').getBoundingClientRect();
|
41 |
const panel_active = document.querySelector('#state-panel').getBoundingClientRect();
|
42 |
if (consider_state_panel || panel_active.height < 25){
|
43 |
document.state_panel_height = panel_active.height;
|
44 |
}
|
45 |
// 25 是chatbot的label高度, 16 是右侧的gap
|
46 |
-
var panel_height_target = panel1.height + panel2.height + panel3.height +
|
47 |
// 禁止动态的state-panel高度影响
|
48 |
panel_height_target = panel_height_target + (document.state_panel_height-panel_active.height)
|
49 |
var panel_height_target = parseInt(panel_height_target);
|
|
|
1 |
+
function gradioApp() {
|
2 |
+
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
|
3 |
+
const elems = document.getElementsByTagName('gradio-app');
|
4 |
+
const elem = elems.length == 0 ? document : elems[0];
|
5 |
+
if (elem !== document) {
|
6 |
+
elem.getElementById = function(id) {
|
7 |
+
return document.getElementById(id);
|
8 |
+
};
|
9 |
+
}
|
10 |
+
return elem.shadowRoot ? elem.shadowRoot : elem;
|
11 |
+
}
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
function addCopyButton(botElement) {
|
17 |
+
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
|
18 |
+
// Copy bot button
|
19 |
+
const copiedIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><polyline points="20 6 9 17 4 12"></polyline></svg></span>';
|
20 |
+
const copyIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg></span>';
|
21 |
+
|
22 |
+
const messageBtnColumnElement = botElement.querySelector('.message-btn-row');
|
23 |
+
if (messageBtnColumnElement) {
|
24 |
+
// Do something if .message-btn-column exists, for example, remove it
|
25 |
+
// messageBtnColumnElement.remove();
|
26 |
+
return;
|
27 |
+
}
|
28 |
+
|
29 |
+
var copyButton = document.createElement('button');
|
30 |
+
copyButton.classList.add('copy-bot-btn');
|
31 |
+
copyButton.setAttribute('aria-label', 'Copy');
|
32 |
+
copyButton.innerHTML = copyIcon;
|
33 |
+
copyButton.addEventListener('click', async () => {
|
34 |
+
const textToCopy = botElement.innerText;
|
35 |
+
try {
|
36 |
+
if ("clipboard" in navigator) {
|
37 |
+
await navigator.clipboard.writeText(textToCopy);
|
38 |
+
copyButton.innerHTML = copiedIcon;
|
39 |
+
setTimeout(() => {
|
40 |
+
copyButton.innerHTML = copyIcon;
|
41 |
+
}, 1500);
|
42 |
+
} else {
|
43 |
+
const textArea = document.createElement("textarea");
|
44 |
+
textArea.value = textToCopy;
|
45 |
+
document.body.appendChild(textArea);
|
46 |
+
textArea.select();
|
47 |
+
try {
|
48 |
+
document.execCommand('copy');
|
49 |
+
copyButton.innerHTML = copiedIcon;
|
50 |
+
setTimeout(() => {
|
51 |
+
copyButton.innerHTML = copyIcon;
|
52 |
+
}, 1500);
|
53 |
+
} catch (error) {
|
54 |
+
console.error("Copy failed: ", error);
|
55 |
+
}
|
56 |
+
document.body.removeChild(textArea);
|
57 |
+
}
|
58 |
+
} catch (error) {
|
59 |
+
console.error("Copy failed: ", error);
|
60 |
+
}
|
61 |
+
});
|
62 |
+
var messageBtnColumn = document.createElement('div');
|
63 |
+
messageBtnColumn.classList.add('message-btn-row');
|
64 |
+
messageBtnColumn.appendChild(copyButton);
|
65 |
+
botElement.appendChild(messageBtnColumn);
|
66 |
+
}
|
67 |
+
|
68 |
+
function chatbotContentChanged(attempt = 1, force = false) {
|
69 |
+
// https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript
|
70 |
+
for (var i = 0; i < attempt; i++) {
|
71 |
+
setTimeout(() => {
|
72 |
+
gradioApp().querySelectorAll('#gpt-chatbot .message-wrap .message.bot').forEach(addCopyButton);
|
73 |
+
}, i === 0 ? 0 : 200);
|
74 |
+
}
|
75 |
+
}
|
76 |
+
|
77 |
+
function chatbotAutoHeight(){
|
78 |
+
// 自动调整高度
|
79 |
function update_height(){
|
80 |
var { panel_height_target, chatbot_height, chatbot } = get_elements(true);
|
81 |
if (panel_height_target!=chatbot_height)
|
|
|
105 |
}, 50); // 每100毫秒执行一次
|
106 |
}
|
107 |
|
108 |
+
function GptAcademicJavaScriptInit(LAYOUT = "LEFT-RIGHT") {
|
109 |
+
chatbotIndicator = gradioApp().querySelector('#gpt-chatbot > div.wrap');
|
110 |
+
var chatbotObserver = new MutationObserver(() => {
|
111 |
+
chatbotContentChanged(1);
|
112 |
+
});
|
113 |
+
chatbotObserver.observe(chatbotIndicator, { attributes: true, childList: true, subtree: true });
|
114 |
+
if (LAYOUT === "LEFT-RIGHT") {chatbotAutoHeight();}
|
115 |
+
}
|
116 |
+
|
117 |
function get_elements(consider_state_panel=false) {
|
118 |
var chatbot = document.querySelector('#gpt-chatbot > div.wrap.svelte-18telvq');
|
119 |
if (!chatbot) {
|
|
|
122 |
const panel1 = document.querySelector('#input-panel').getBoundingClientRect();
|
123 |
const panel2 = document.querySelector('#basic-panel').getBoundingClientRect()
|
124 |
const panel3 = document.querySelector('#plugin-panel').getBoundingClientRect();
|
125 |
+
// const panel4 = document.querySelector('#interact-panel').getBoundingClientRect();
|
126 |
const panel5 = document.querySelector('#input-panel2').getBoundingClientRect();
|
127 |
const panel_active = document.querySelector('#state-panel').getBoundingClientRect();
|
128 |
if (consider_state_panel || panel_active.height < 25){
|
129 |
document.state_panel_height = panel_active.height;
|
130 |
}
|
131 |
// 25 是chatbot的label高度, 16 是右侧的gap
|
132 |
+
var panel_height_target = panel1.height + panel2.height + panel3.height + 0 + 0 - 25 + 16*2;
|
133 |
// 禁止动态的state-panel高度影响
|
134 |
panel_height_target = panel_height_target + (document.state_panel_height-panel_active.height)
|
135 |
var panel_height_target = parseInt(panel_height_target);
|
themes/contrast.css
CHANGED
@@ -198,7 +198,7 @@
|
|
198 |
}
|
199 |
|
200 |
/* 小按钮 */
|
201 |
-
.sm
|
202 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
203 |
--button-small-text-weight: 600;
|
204 |
--button-small-text-size: 16px;
|
@@ -208,7 +208,7 @@
|
|
208 |
border-top-left-radius: 0px;
|
209 |
}
|
210 |
|
211 |
-
#plugin-panel .sm
|
212 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
213 |
--button-small-text-weight: 400;
|
214 |
--button-small-text-size: 14px;
|
|
|
198 |
}
|
199 |
|
200 |
/* 小按钮 */
|
201 |
+
.sm {
|
202 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
203 |
--button-small-text-weight: 600;
|
204 |
--button-small-text-size: 16px;
|
|
|
208 |
border-top-left-radius: 0px;
|
209 |
}
|
210 |
|
211 |
+
#plugin-panel .sm {
|
212 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
213 |
--button-small-text-weight: 400;
|
214 |
--button-small-text-size: 14px;
|
themes/contrast.py
CHANGED
@@ -57,12 +57,9 @@ def adjust_theme():
|
|
57 |
button_cancel_text_color_dark="white",
|
58 |
)
|
59 |
|
60 |
-
|
61 |
-
js = ""
|
62 |
-
|
63 |
-
with open('themes/common.js', 'r', encoding='utf8') as f:
|
64 |
-
js = f"<script>{f.read()}</script>"
|
65 |
-
|
66 |
# 添加一个萌萌的看板娘
|
67 |
if ADD_WAIFU:
|
68 |
js += """
|
|
|
57 |
button_cancel_text_color_dark="white",
|
58 |
)
|
59 |
|
60 |
+
with open('themes/common.js', 'r', encoding='utf8') as f:
|
61 |
+
js = f"<script>{f.read()}</script>"
|
62 |
+
|
|
|
|
|
|
|
63 |
# 添加一个萌萌的看板娘
|
64 |
if ADD_WAIFU:
|
65 |
js += """
|
themes/default.css
CHANGED
@@ -9,15 +9,15 @@
|
|
9 |
border-radius: 4px;
|
10 |
}
|
11 |
|
12 |
-
#plugin-panel .dropdown-arrow
|
13 |
-
width:
|
14 |
}
|
15 |
#plugin-panel input.svelte-aqlk7e.svelte-aqlk7e.svelte-aqlk7e {
|
16 |
padding-left: 5px;
|
17 |
}
|
18 |
|
19 |
/* 小按钮 */
|
20 |
-
.sm
|
21 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
22 |
--button-small-text-weight: 600;
|
23 |
--button-small-text-size: 16px;
|
@@ -27,7 +27,7 @@
|
|
27 |
border-top-left-radius: 6px;
|
28 |
}
|
29 |
|
30 |
-
#plugin-panel .sm
|
31 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
32 |
--button-small-text-weight: 400;
|
33 |
--button-small-text-size: 14px;
|
|
|
9 |
border-radius: 4px;
|
10 |
}
|
11 |
|
12 |
+
#plugin-panel .dropdown-arrow {
|
13 |
+
width: 25px;
|
14 |
}
|
15 |
#plugin-panel input.svelte-aqlk7e.svelte-aqlk7e.svelte-aqlk7e {
|
16 |
padding-left: 5px;
|
17 |
}
|
18 |
|
19 |
/* 小按钮 */
|
20 |
+
#basic-panel .sm {
|
21 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
22 |
--button-small-text-weight: 600;
|
23 |
--button-small-text-size: 16px;
|
|
|
27 |
border-top-left-radius: 6px;
|
28 |
}
|
29 |
|
30 |
+
#plugin-panel .sm {
|
31 |
font-family: "Microsoft YaHei UI", "Helvetica", "Microsoft YaHei", "ui-sans-serif", "sans-serif", "system-ui";
|
32 |
--button-small-text-weight: 400;
|
33 |
--button-small-text-size: 14px;
|
themes/default.py
CHANGED
@@ -57,11 +57,8 @@ def adjust_theme():
|
|
57 |
button_cancel_text_color_dark="white",
|
58 |
)
|
59 |
|
60 |
-
|
61 |
-
js = ""
|
62 |
-
else:
|
63 |
-
with open('themes/common.js', 'r', encoding='utf8') as f:
|
64 |
-
js = f"<script>{f.read()}</script>"
|
65 |
|
66 |
# 添加一个萌萌的看板娘
|
67 |
if ADD_WAIFU:
|
|
|
57 |
button_cancel_text_color_dark="white",
|
58 |
)
|
59 |
|
60 |
+
with open('themes/common.js', 'r', encoding='utf8') as f:
|
61 |
+
js = f"<script>{f.read()}</script>"
|
|
|
|
|
|
|
62 |
|
63 |
# 添加一个萌萌的看板娘
|
64 |
if ADD_WAIFU:
|
themes/gradios.py
CHANGED
@@ -3,23 +3,29 @@ import logging
|
|
3 |
from toolbox import get_conf, ProxyNetworkActivate
|
4 |
CODE_HIGHLIGHT, ADD_WAIFU, LAYOUT = get_conf('CODE_HIGHLIGHT', 'ADD_WAIFU', 'LAYOUT')
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def adjust_theme():
|
7 |
|
8 |
try:
|
9 |
set_theme = gr.themes.ThemeClass()
|
10 |
-
with ProxyNetworkActivate():
|
11 |
logging.info('正在下载Gradio主题,请稍等。')
|
12 |
THEME, = get_conf('THEME')
|
13 |
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')
|
14 |
if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-')
|
15 |
set_theme = set_theme.from_hub(THEME.lower())
|
16 |
|
17 |
-
|
18 |
-
js = ""
|
19 |
-
|
20 |
-
with open('themes/common.js', 'r', encoding='utf8') as f:
|
21 |
-
js = f"<script>{f.read()}</script>"
|
22 |
-
|
23 |
# 添加一个萌萌的看板娘
|
24 |
if ADD_WAIFU:
|
25 |
js += """
|
|
|
3 |
from toolbox import get_conf, ProxyNetworkActivate
|
4 |
CODE_HIGHLIGHT, ADD_WAIFU, LAYOUT = get_conf('CODE_HIGHLIGHT', 'ADD_WAIFU', 'LAYOUT')
|
5 |
|
6 |
+
def dynamic_set_theme(THEME):
|
7 |
+
set_theme = gr.themes.ThemeClass()
|
8 |
+
with ProxyNetworkActivate('Download_Gradio_Theme'):
|
9 |
+
logging.info('正在下载Gradio主题,请稍等。')
|
10 |
+
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')
|
11 |
+
if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-')
|
12 |
+
set_theme = set_theme.from_hub(THEME.lower())
|
13 |
+
return set_theme
|
14 |
+
|
15 |
def adjust_theme():
|
16 |
|
17 |
try:
|
18 |
set_theme = gr.themes.ThemeClass()
|
19 |
+
with ProxyNetworkActivate('Download_Gradio_Theme'):
|
20 |
logging.info('正在下载Gradio主题,请稍等。')
|
21 |
THEME, = get_conf('THEME')
|
22 |
if THEME.startswith('Huggingface-'): THEME = THEME.lstrip('Huggingface-')
|
23 |
if THEME.startswith('huggingface-'): THEME = THEME.lstrip('huggingface-')
|
24 |
set_theme = set_theme.from_hub(THEME.lower())
|
25 |
|
26 |
+
with open('themes/common.js', 'r', encoding='utf8') as f:
|
27 |
+
js = f"<script>{f.read()}</script>"
|
28 |
+
|
|
|
|
|
|
|
29 |
# 添加一个萌萌的看板娘
|
30 |
if ADD_WAIFU:
|
31 |
js += """
|
themes/green.py
CHANGED
@@ -73,12 +73,8 @@ def adjust_theme():
|
|
73 |
chatbot_code_background_color_dark="*neutral_950",
|
74 |
)
|
75 |
|
76 |
-
js =
|
77 |
-
|
78 |
-
js = ""
|
79 |
-
else:
|
80 |
-
with open('themes/common.js', 'r', encoding='utf8') as f:
|
81 |
-
js = f"<script>{f.read()}</script>"
|
82 |
|
83 |
# 添加一个萌萌的看板娘
|
84 |
if ADD_WAIFU:
|
|
|
73 |
chatbot_code_background_color_dark="*neutral_950",
|
74 |
)
|
75 |
|
76 |
+
with open('themes/common.js', 'r', encoding='utf8') as f:
|
77 |
+
js = f"<script>{f.read()}</script>"
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# 添加一个萌萌的看板娘
|
80 |
if ADD_WAIFU:
|
themes/theme.py
CHANGED
@@ -2,17 +2,22 @@ import gradio as gr
|
|
2 |
from toolbox import get_conf
|
3 |
THEME, = get_conf('THEME')
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
|
|
|
2 |
from toolbox import get_conf
|
3 |
THEME, = get_conf('THEME')
|
4 |
|
5 |
+
def load_dynamic_theme(THEME):
|
6 |
+
adjust_dynamic_theme = None
|
7 |
+
if THEME == 'Chuanhu-Small-and-Beautiful':
|
8 |
+
from .green import adjust_theme, advanced_css
|
9 |
+
theme_declaration = "<h2 align=\"center\" class=\"small\">[Chuanhu-Small-and-Beautiful主题]</h2>"
|
10 |
+
elif THEME == 'High-Contrast':
|
11 |
+
from .contrast import adjust_theme, advanced_css
|
12 |
+
theme_declaration = ""
|
13 |
+
elif '/' in THEME:
|
14 |
+
from .gradios import adjust_theme, advanced_css
|
15 |
+
from .gradios import dynamic_set_theme
|
16 |
+
adjust_dynamic_theme = dynamic_set_theme(THEME)
|
17 |
+
theme_declaration = ""
|
18 |
+
else:
|
19 |
+
from .default import adjust_theme, advanced_css
|
20 |
+
theme_declaration = ""
|
21 |
+
return adjust_theme, advanced_css, theme_declaration, adjust_dynamic_theme
|
22 |
|
23 |
+
adjust_theme, advanced_css, theme_declaration, _ = load_dynamic_theme(THEME)
|
toolbox.py
CHANGED
@@ -216,7 +216,7 @@ def get_reduce_token_percent(text):
|
|
216 |
return 0.5, '不详'
|
217 |
|
218 |
|
219 |
-
def write_history_to_file(history, file_basename=None, file_fullname=None):
|
220 |
"""
|
221 |
将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
|
222 |
"""
|
@@ -235,7 +235,7 @@ def write_history_to_file(history, file_basename=None, file_fullname=None):
|
|
235 |
if type(content) != str: content = str(content)
|
236 |
except:
|
237 |
continue
|
238 |
-
if i % 2 == 0:
|
239 |
f.write('## ')
|
240 |
try:
|
241 |
f.write(content)
|
@@ -472,7 +472,7 @@ def extract_archive(file_path, dest_dir):
|
|
472 |
print("Successfully extracted rar archive to {}".format(dest_dir))
|
473 |
except:
|
474 |
print("Rar format requires additional dependencies to install")
|
475 |
-
return '\n\n解压失败! 需要安装pip install rarfile来解压rar
|
476 |
|
477 |
# 第三方库,需要预先pip install py7zr
|
478 |
elif file_extension == '.7z':
|
@@ -523,10 +523,11 @@ def promote_file_to_downloadzone(file, rename_file=None, chatbot=None):
|
|
523 |
# 把文件复制过去
|
524 |
if not os.path.exists(new_path): shutil.copyfile(file, new_path)
|
525 |
# 将文件添加到chatbot cookie中,避免多用户干扰
|
526 |
-
if chatbot:
|
527 |
if 'files_to_promote' in chatbot._cookies: current = chatbot._cookies['files_to_promote']
|
528 |
else: current = []
|
529 |
chatbot._cookies.update({'files_to_promote': [new_path] + current})
|
|
|
530 |
|
531 |
def disable_auto_promotion(chatbot):
|
532 |
chatbot._cookies.update({'files_to_promote': []})
|
@@ -580,7 +581,7 @@ def on_file_uploaded(request: gradio.Request, files, chatbot, txt, txt2, checkbo
|
|
580 |
|
581 |
# 整理文件集合
|
582 |
moved_files = [fp for fp in glob.glob(f'{target_path_base}/**/*', recursive=True)]
|
583 |
-
if "
|
584 |
txt, txt2 = "", target_path_base
|
585 |
else:
|
586 |
txt, txt2 = target_path_base, ""
|
@@ -955,7 +956,19 @@ class ProxyNetworkActivate():
|
|
955 |
"""
|
956 |
这段代码定义了一个名为TempProxy的空上下文管理器, 用于给一小段代码上代理
|
957 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
958 |
def __enter__(self):
|
|
|
959 |
from toolbox import get_conf
|
960 |
proxies, = get_conf('proxies')
|
961 |
if 'no_proxy' in os.environ: os.environ.pop('no_proxy')
|
|
|
216 |
return 0.5, '不详'
|
217 |
|
218 |
|
219 |
+
def write_history_to_file(history, file_basename=None, file_fullname=None, auto_caption=True):
|
220 |
"""
|
221 |
将对话记录history以Markdown格式写入文件中。如果没有指定文件名,则使用当前时间生成文件名。
|
222 |
"""
|
|
|
235 |
if type(content) != str: content = str(content)
|
236 |
except:
|
237 |
continue
|
238 |
+
if i % 2 == 0 and auto_caption:
|
239 |
f.write('## ')
|
240 |
try:
|
241 |
f.write(content)
|
|
|
472 |
print("Successfully extracted rar archive to {}".format(dest_dir))
|
473 |
except:
|
474 |
print("Rar format requires additional dependencies to install")
|
475 |
+
return '\n\n解压失败! 需要安装pip install rarfile来解压rar文件。建议:使用zip压缩格式。'
|
476 |
|
477 |
# 第三方库,需要预先pip install py7zr
|
478 |
elif file_extension == '.7z':
|
|
|
523 |
# 把文件复制过去
|
524 |
if not os.path.exists(new_path): shutil.copyfile(file, new_path)
|
525 |
# 将文件添加到chatbot cookie中,避免多用户干扰
|
526 |
+
if chatbot is not None:
|
527 |
if 'files_to_promote' in chatbot._cookies: current = chatbot._cookies['files_to_promote']
|
528 |
else: current = []
|
529 |
chatbot._cookies.update({'files_to_promote': [new_path] + current})
|
530 |
+
return new_path
|
531 |
|
532 |
def disable_auto_promotion(chatbot):
|
533 |
chatbot._cookies.update({'files_to_promote': []})
|
|
|
581 |
|
582 |
# 整理文件集合
|
583 |
moved_files = [fp for fp in glob.glob(f'{target_path_base}/**/*', recursive=True)]
|
584 |
+
if "浮动输入区" in checkboxes:
|
585 |
txt, txt2 = "", target_path_base
|
586 |
else:
|
587 |
txt, txt2 = target_path_base, ""
|
|
|
956 |
"""
|
957 |
这段代码定义了一个名为TempProxy的空上下文管理器, 用于给一小段代码上代理
|
958 |
"""
|
959 |
+
def __init__(self, task=None) -> None:
|
960 |
+
self.task = task
|
961 |
+
if not task:
|
962 |
+
# 不给定task, 那么我们默认代理生效
|
963 |
+
self.valid = True
|
964 |
+
else:
|
965 |
+
# 给定了task, 我们检查一下
|
966 |
+
from toolbox import get_conf
|
967 |
+
WHEN_TO_USE_PROXY, = get_conf('WHEN_TO_USE_PROXY')
|
968 |
+
self.valid = (task in WHEN_TO_USE_PROXY)
|
969 |
+
|
970 |
def __enter__(self):
|
971 |
+
if not self.valid: return self
|
972 |
from toolbox import get_conf
|
973 |
proxies, = get_conf('proxies')
|
974 |
if 'no_proxy' in os.environ: os.environ.pop('no_proxy')
|
version
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
{
|
2 |
-
"version": 3.
|
3 |
"show_feature": true,
|
4 |
-
"new_feature": "提高稳定性&解决多用户冲突问题 <-> 支持插件分类和更多UI皮肤外观 <-> 支持用户使用自然语言调度各个插件(虚空终端) ! <-> 改进UI,设计新主题 <-> 支持借助GROBID实现PDF高精度翻译 <-> 接入百度千帆平台和文心一言 <-> 接入阿里通义千问、讯飞星火、上海AI-Lab书生 <-> 优化一键升级 <-> 提高arxiv翻译速度和成功率"
|
5 |
}
|
|
|
1 |
{
|
2 |
+
"version": 3.55,
|
3 |
"show_feature": true,
|
4 |
+
"new_feature": "重新编译Gradio优化使用体验 <-> 新增动态代码解释器(CodeInterpreter) <-> 增加文本回答复制按钮 <-> 细分代理场合 <-> 支持动态选择不同界面主题 <-> 提高稳定性&解决多用户冲突问题 <-> 支持插件分类和更多UI皮肤外观 <-> 支持用户使用自然语言调度各个插件(虚空终端) ! <-> 改进UI,设计新主题 <-> 支持借助GROBID实现PDF高精度翻译 <-> 接入百度千帆平台和文心一言 <-> 接入阿里通义千问、讯飞星火、上海AI-Lab书生 <-> 优化一键升级 <-> 提高arxiv翻译速度和成功率"
|
5 |
}
|