Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ from typing import Generator, Dict, Any
|
|
11 |
import logging
|
12 |
import time
|
13 |
|
14 |
-
|
15 |
# MODEL NAME
|
16 |
model = os.getenv("MODEL_NAME")
|
17 |
# 代理服务器配置
|
@@ -21,7 +21,7 @@ MAX_RETRIES = int(os.getenv("MAX_RETRIES", 5))
|
|
21 |
# 保存历史
|
22 |
save_history = True
|
23 |
|
24 |
-
#
|
25 |
|
26 |
# 配置日志
|
27 |
logging.basicConfig(level=logging.INFO)
|
@@ -155,7 +155,7 @@ def chat_with_retry(history_messages, max_retries=MAX_RETRIES):
|
|
155 |
model=model,
|
156 |
messages=history_messages,
|
157 |
stream=True,
|
158 |
-
temperature
|
159 |
)
|
160 |
|
161 |
return response
|
@@ -243,7 +243,6 @@ DEFAULT_THEME = {
|
|
243 |
}
|
244 |
}
|
245 |
|
246 |
-
|
247 |
def format_history(history):
|
248 |
messages = [{
|
249 |
"role": "system",
|
@@ -256,6 +255,27 @@ def format_history(history):
|
|
256 |
messages.append({"role": "assistant", "content": item["content"]})
|
257 |
return messages
|
258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
259 |
|
260 |
class Gradio_Events:
|
261 |
|
@@ -508,7 +528,8 @@ class Gradio_Events:
|
|
508 |
return gr.skip()
|
509 |
state_value["conversation_id"] = ""
|
510 |
return gr.update(active_key=state_value["conversation_id"]), gr.update(
|
511 |
-
items=DEFAULT_CONVERSATIONS_HISTORY), gr.update(
|
|
|
512 |
|
513 |
@staticmethod
|
514 |
def select_conversation(state_value, e: gr.EventData):
|
@@ -565,7 +586,7 @@ class Gradio_Events:
|
|
565 |
|
566 |
@staticmethod
|
567 |
def update_browser_state(state_value):
|
568 |
-
|
569 |
return gr.update(value=dict(
|
570 |
conversations=state_value["conversations"],
|
571 |
conversations_history=state_value["conversations_history"]))
|
@@ -625,7 +646,6 @@ css = """
|
|
625 |
}
|
626 |
"""
|
627 |
|
628 |
-
|
629 |
def logo():
|
630 |
with antd.Typography.Title(level=1,
|
631 |
elem_style=dict(fontSize=24,
|
@@ -639,8 +659,15 @@ def logo():
|
|
639 |
height=24)
|
640 |
ms.Span("dots.llm1.inst")
|
641 |
|
642 |
-
|
643 |
with gr.Blocks(css=css, fill_width=True) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
644 |
state = gr.State({
|
645 |
"conversations_history": {},
|
646 |
"conversations": [],
|
@@ -974,13 +1001,9 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
974 |
maxRows=6),
|
975 |
elem_style=dict(width="100%"))
|
976 |
# Events Handler
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
demo.load(fn=Gradio_Events.apply_browser_state,
|
982 |
-
inputs=[browser_state, state],
|
983 |
-
outputs=[conversations, state])
|
984 |
|
985 |
add_conversation_btn.click(fn=Gradio_Events.new_chat,
|
986 |
inputs=[state],
|
@@ -1035,5 +1058,10 @@ with gr.Blocks(css=css, fill_width=True) as demo:
|
|
1035 |
conversations, add_conversation_btn, chatbot, state
|
1036 |
])
|
1037 |
|
|
|
|
|
|
|
|
|
|
|
1038 |
if __name__ == "__main__":
|
1039 |
demo.queue(default_concurrency_limit=200).launch(ssr_mode=False, max_threads=200)
|
|
|
11 |
import logging
|
12 |
import time
|
13 |
|
14 |
+
# ========== Configuration
|
15 |
# MODEL NAME
|
16 |
model = os.getenv("MODEL_NAME")
|
17 |
# 代理服务器配置
|
|
|
21 |
# 保存历史
|
22 |
save_history = True
|
23 |
|
24 |
+
# ========== Configuration
|
25 |
|
26 |
# 配置日志
|
27 |
logging.basicConfig(level=logging.INFO)
|
|
|
155 |
model=model,
|
156 |
messages=history_messages,
|
157 |
stream=True,
|
158 |
+
temperature=0.7, top_p=0.8
|
159 |
)
|
160 |
|
161 |
return response
|
|
|
243 |
}
|
244 |
}
|
245 |
|
|
|
246 |
def format_history(history):
|
247 |
messages = [{
|
248 |
"role": "system",
|
|
|
255 |
messages.append({"role": "assistant", "content": item["content"]})
|
256 |
return messages
|
257 |
|
258 |
+
def load_history():
|
259 |
+
"""加载历史记录"""
|
260 |
+
try:
|
261 |
+
with open('history.json', 'r', encoding='utf-8') as f:
|
262 |
+
return json.load(f)
|
263 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
264 |
+
return {
|
265 |
+
"conversations": [],
|
266 |
+
"conversations_history": {}
|
267 |
+
}
|
268 |
+
|
269 |
+
def save_history(state_value):
|
270 |
+
"""保存历史记录"""
|
271 |
+
try:
|
272 |
+
with open('history.json', 'w', encoding='utf-8') as f:
|
273 |
+
json.dump({
|
274 |
+
"conversations": state_value["conversations"],
|
275 |
+
"conversations_history": state_value["conversations_history"]
|
276 |
+
}, f, ensure_ascii=False, indent=4)
|
277 |
+
except Exception as e:
|
278 |
+
logger.error(f"Failed to save history: {e}")
|
279 |
|
280 |
class Gradio_Events:
|
281 |
|
|
|
528 |
return gr.skip()
|
529 |
state_value["conversation_id"] = ""
|
530 |
return gr.update(active_key=state_value["conversation_id"]), gr.update(
|
531 |
+
items=DEFAULT_CONVERSATIONS_HISTORY), gr.update(
|
532 |
+
value=state_value)
|
533 |
|
534 |
@staticmethod
|
535 |
def select_conversation(state_value, e: gr.EventData):
|
|
|
586 |
|
587 |
@staticmethod
|
588 |
def update_browser_state(state_value):
|
589 |
+
save_history(state_value)
|
590 |
return gr.update(value=dict(
|
591 |
conversations=state_value["conversations"],
|
592 |
conversations_history=state_value["conversations_history"]))
|
|
|
646 |
}
|
647 |
"""
|
648 |
|
|
|
649 |
def logo():
|
650 |
with antd.Typography.Title(level=1,
|
651 |
elem_style=dict(fontSize=24,
|
|
|
659 |
height=24)
|
660 |
ms.Span("dots.llm1.inst")
|
661 |
|
|
|
662 |
with gr.Blocks(css=css, fill_width=True) as demo:
|
663 |
+
if save_history:
|
664 |
+
browser_state = gr.State(load_history())
|
665 |
+
else:
|
666 |
+
browser_state = gr.State({
|
667 |
+
"conversations": [],
|
668 |
+
"conversations_history": {}
|
669 |
+
})
|
670 |
+
|
671 |
state = gr.State({
|
672 |
"conversations_history": {},
|
673 |
"conversations": [],
|
|
|
1001 |
maxRows=6),
|
1002 |
elem_style=dict(width="100%"))
|
1003 |
# Events Handler
|
1004 |
+
demo.load(fn=Gradio_Events.apply_browser_state,
|
1005 |
+
inputs=[browser_state, state],
|
1006 |
+
outputs=[conversations, state])
|
|
|
|
|
|
|
|
|
1007 |
|
1008 |
add_conversation_btn.click(fn=Gradio_Events.new_chat,
|
1009 |
inputs=[state],
|
|
|
1058 |
conversations, add_conversation_btn, chatbot, state
|
1059 |
])
|
1060 |
|
1061 |
+
# 添加状态更新时的保存逻辑
|
1062 |
+
demo.post(fn=Gradio_Events.update_browser_state,
|
1063 |
+
inputs=state,
|
1064 |
+
outputs=None)
|
1065 |
+
|
1066 |
if __name__ == "__main__":
|
1067 |
demo.queue(default_concurrency_limit=200).launch(ssr_mode=False, max_threads=200)
|