Spaces:
Runtime error
Runtime error
Commit
·
d217b74
1
Parent(s):
8152075
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import logging
|
2 |
import json
|
3 |
import os
|
4 |
-
import pathlib
|
5 |
import csv
|
6 |
import requests
|
7 |
import re
|
@@ -14,7 +13,6 @@ from markdown import markdown
|
|
14 |
from pygments import highlight
|
15 |
from pygments.lexers import get_lexer_by_name
|
16 |
from pygments.formatters import HtmlFormatter
|
17 |
-
from gradio.themes.utils import ThemeAsset
|
18 |
|
19 |
|
20 |
use_websearch_checkbox=False
|
@@ -199,7 +197,6 @@ def save_file(filename, system, history, chatbot):
|
|
199 |
md_s += f"\n{data['role']}: \n- {data['content']} \n"
|
200 |
with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:
|
201 |
f.write(md_s)
|
202 |
-
# logging.info("保存对话历史完毕")
|
203 |
return os.path.join(HISTORY_DIR, filename)
|
204 |
|
205 |
|
@@ -220,7 +217,6 @@ def export_markdown(filename, system, history, chatbot):
|
|
220 |
|
221 |
|
222 |
def load_chat_history(filename, system, history, chatbot):
|
223 |
-
# logging.info("加载对话历史中……")
|
224 |
if type(filename) != str:
|
225 |
filename = filename.name
|
226 |
try:
|
@@ -228,7 +224,6 @@ def load_chat_history(filename, system, history, chatbot):
|
|
228 |
json_s = json.load(f)
|
229 |
try:
|
230 |
if type(json_s["history"][0]) == str:
|
231 |
-
# logging.info("历史记录格式为旧版,正在转换……")
|
232 |
new_history = []
|
233 |
for index, item in enumerate(json_s["history"]):
|
234 |
if index % 2 == 0:
|
@@ -238,20 +233,15 @@ def load_chat_history(filename, system, history, chatbot):
|
|
238 |
json_s["history"] = new_history
|
239 |
logging.info(new_history)
|
240 |
except:
|
241 |
-
# 没有对话历史
|
242 |
pass
|
243 |
-
# logging.info("加载对话历史完毕")
|
244 |
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
245 |
except FileNotFoundError:
|
246 |
-
# logging.info("没有找到对话历史文件,不执行任何操作")
|
247 |
return filename, system, history, chatbot
|
248 |
|
249 |
|
250 |
def load_template(filename, mode=0):
|
251 |
-
# logging.info(f"加载模板文件{filename},模式为{mode}(0为返回字典和下拉菜单,1为返回下拉菜单,2为返回字典)")
|
252 |
lines = []
|
253 |
logging.info("Loading template...")
|
254 |
-
# filename='中文Prompts.json'
|
255 |
if filename.endswith(".json"):
|
256 |
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
|
257 |
lines = json.load(f)
|
@@ -306,57 +296,3 @@ def submit_key(key):
|
|
306 |
msg = f"API-Key: {hide_middle_chars(key)}"
|
307 |
logging.info(msg)
|
308 |
return key, msg
|
309 |
-
|
310 |
-
def create_theme_dropdown():
|
311 |
-
import gradio as gr
|
312 |
-
|
313 |
-
asset_path = pathlib.Path(__file__).parent / "themes"
|
314 |
-
themes = []
|
315 |
-
for theme_asset in os.listdir(str(asset_path)):
|
316 |
-
themes.append(
|
317 |
-
(ThemeAsset(theme_asset), gr.Theme.load(str(asset_path / theme_asset)))
|
318 |
-
)
|
319 |
-
|
320 |
-
def make_else_if(theme_asset):
|
321 |
-
return f"""
|
322 |
-
else if (theme == '{str(theme_asset[0].version)}') {{
|
323 |
-
var theme_css = `{theme_asset[1]._get_theme_css()}`
|
324 |
-
}}"""
|
325 |
-
|
326 |
-
head, tail = themes[0], themes[1:]
|
327 |
-
if_statement = f"""
|
328 |
-
if (theme == "{str(head[0].version)}") {{
|
329 |
-
var theme_css = `{head[1]._get_theme_css()}`
|
330 |
-
}} {" ".join(make_else_if(t) for t in tail)}
|
331 |
-
"""
|
332 |
-
|
333 |
-
latest_to_oldest = sorted([t[0] for t in themes], key=lambda asset: asset.version)[
|
334 |
-
::-1
|
335 |
-
]
|
336 |
-
latest_to_oldest = [str(t.version) for t in latest_to_oldest]
|
337 |
-
|
338 |
-
component = gr.Dropdown(
|
339 |
-
choices=latest_to_oldest,
|
340 |
-
value=latest_to_oldest[0],
|
341 |
-
render=False,
|
342 |
-
label="Select Version",
|
343 |
-
).style(container=False)
|
344 |
-
|
345 |
-
return (
|
346 |
-
component,
|
347 |
-
f"""
|
348 |
-
(theme) => {{
|
349 |
-
if (!document.querySelector('.theme-css')) {{
|
350 |
-
var theme_elem = document.createElement('style');
|
351 |
-
theme_elem.classList.add('theme-css');
|
352 |
-
document.head.appendChild(theme_elem);
|
353 |
-
}} else {{
|
354 |
-
var theme_elem = document.querySelector('.theme-css');
|
355 |
-
}}
|
356 |
-
{if_statement}
|
357 |
-
theme_elem.innerHTML = theme_css;
|
358 |
-
}}
|
359 |
-
""",
|
360 |
-
)
|
361 |
-
|
362 |
-
|
|
|
1 |
import logging
|
2 |
import json
|
3 |
import os
|
|
|
4 |
import csv
|
5 |
import requests
|
6 |
import re
|
|
|
13 |
from pygments import highlight
|
14 |
from pygments.lexers import get_lexer_by_name
|
15 |
from pygments.formatters import HtmlFormatter
|
|
|
16 |
|
17 |
|
18 |
use_websearch_checkbox=False
|
|
|
197 |
md_s += f"\n{data['role']}: \n- {data['content']} \n"
|
198 |
with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:
|
199 |
f.write(md_s)
|
|
|
200 |
return os.path.join(HISTORY_DIR, filename)
|
201 |
|
202 |
|
|
|
217 |
|
218 |
|
219 |
def load_chat_history(filename, system, history, chatbot):
|
|
|
220 |
if type(filename) != str:
|
221 |
filename = filename.name
|
222 |
try:
|
|
|
224 |
json_s = json.load(f)
|
225 |
try:
|
226 |
if type(json_s["history"][0]) == str:
|
|
|
227 |
new_history = []
|
228 |
for index, item in enumerate(json_s["history"]):
|
229 |
if index % 2 == 0:
|
|
|
233 |
json_s["history"] = new_history
|
234 |
logging.info(new_history)
|
235 |
except:
|
|
|
236 |
pass
|
|
|
237 |
return filename, json_s["system"], json_s["history"], json_s["chatbot"]
|
238 |
except FileNotFoundError:
|
|
|
239 |
return filename, system, history, chatbot
|
240 |
|
241 |
|
242 |
def load_template(filename, mode=0):
|
|
|
243 |
lines = []
|
244 |
logging.info("Loading template...")
|
|
|
245 |
if filename.endswith(".json"):
|
246 |
with open(os.path.join(TEMPLATES_DIR, filename), "r", encoding="utf8") as f:
|
247 |
lines = json.load(f)
|
|
|
296 |
msg = f"API-Key: {hide_middle_chars(key)}"
|
297 |
logging.info(msg)
|
298 |
return key, msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|