HaveAI commited on
Commit
c35a0a6
·
verified ·
1 Parent(s): 215c859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -40
app.py CHANGED
@@ -11,7 +11,7 @@ from typing import Generator, Dict, Any
11
  import logging
12
  import time
13
 
14
- # ========== Configuration
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
- # ========== Configuration
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=0.7, top_p=0.8
159
  )
160
 
161
  return response
@@ -243,27 +243,19 @@ DEFAULT_THEME = {
243
  }
244
  }
245
 
246
- def load_history():
247
- """加载历史记录"""
248
- try:
249
- with open('history.json', 'r', encoding='utf-8') as f:
250
- return json.load(f)
251
- except (FileNotFoundError, json.JSONDecodeError):
252
- return {
253
- "conversations": [],
254
- "conversations_history": {}
255
- }
256
 
257
- def save_history(state_value):
258
- """保存历史记录"""
259
- try:
260
- with open('history.json', 'w', encoding='utf-8') as f:
261
- json.dump({
262
- "conversations": state_value["conversations"],
263
- "conversations_history": state_value["conversations_history"]
264
- }, f, ensure_ascii=False, indent=4)
265
- except Exception as e:
266
- logger.error(f"Failed to save history: {e}")
 
 
267
 
268
  class Gradio_Events:
269
 
@@ -516,8 +508,7 @@ class Gradio_Events:
516
  return gr.skip()
517
  state_value["conversation_id"] = ""
518
  return gr.update(active_key=state_value["conversation_id"]), gr.update(
519
- items=DEFAULT_CONVERSATIONS_HISTORY), gr.update(
520
- value=state_value)
521
 
522
  @staticmethod
523
  def select_conversation(state_value, e: gr.EventData):
@@ -574,7 +565,7 @@ class Gradio_Events:
574
 
575
  @staticmethod
576
  def update_browser_state(state_value):
577
- save_history(state_value)
578
  return gr.update(value=dict(
579
  conversations=state_value["conversations"],
580
  conversations_history=state_value["conversations_history"]))
@@ -634,6 +625,7 @@ css = """
634
  }
635
  """
636
 
 
637
  def logo():
638
  with antd.Typography.Title(level=1,
639
  elem_style=dict(fontSize=24,
@@ -647,15 +639,8 @@ def logo():
647
  height=24)
648
  ms.Span("dots.llm1.inst")
649
 
650
- with gr.Blocks(css=css, fill_width=True) as demo:
651
- if save_history:
652
- browser_state = gr.State(load_history())
653
- else:
654
- browser_state = gr.State({
655
- "conversations": [],
656
- "conversations_history": {}
657
- })
658
 
 
659
  state = gr.State({
660
  "conversations_history": {},
661
  "conversations": [],
@@ -989,9 +974,13 @@ with gr.Blocks(css=css, fill_width=True) as demo:
989
  maxRows=6),
990
  elem_style=dict(width="100%"))
991
  # Events Handler
992
- demo.load(fn=Gradio_Events.apply_browser_state,
993
- inputs=[browser_state, state],
994
- outputs=[conversations, state])
 
 
 
 
995
 
996
  add_conversation_btn.click(fn=Gradio_Events.new_chat,
997
  inputs=[state],
@@ -1046,8 +1035,5 @@ with gr.Blocks(css=css, fill_width=True) as demo:
1046
  conversations, add_conversation_btn, chatbot, state
1047
  ])
1048
 
1049
- # 添加状态更新时的保存逻辑 (исправленный вариант)
1050
- state.change(fn=Gradio_Events.update_browser_state, inputs=state, outputs=None)
1051
-
1052
  if __name__ == "__main__":
1053
  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
 
247
+ def format_history(history):
248
+ messages = [{
249
+ "role": "system",
250
+ "content": "You are a helpful assistant named Flare.",
251
+ }]
252
+ for item in history:
253
+ if item["role"] == "user":
254
+ messages.append({"role": "user", "content": item["content"]})
255
+ elif item["role"] == "assistant":
256
+ messages.append({"role": "assistant", "content": item["content"]})
257
+ return messages
258
+
259
 
260
  class Gradio_Events:
261
 
 
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(value=state_value)
 
512
 
513
  @staticmethod
514
  def select_conversation(state_value, e: gr.EventData):
 
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
  }
626
  """
627
 
628
+
629
  def logo():
630
  with antd.Typography.Title(level=1,
631
  elem_style=dict(fontSize=24,
 
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
  maxRows=6),
975
  elem_style=dict(width="100%"))
976
  # Events Handler
977
+ if save_history:
978
+ browser_state = gr.State({
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
  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)