hsuwill000 commited on
Commit
5d23361
·
verified ·
1 Parent(s): 2fccafa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -17,8 +17,6 @@ def run_command(command, cwd=None):
17
  print(f"Command succeeded: {command}")
18
  print(result.stdout)
19
 
20
- run_command('pip install openai')
21
-
22
  # Model configuration
23
  MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
24
  QUANT = "Q5_K_M"
@@ -54,7 +52,7 @@ def start_llama_server(model_path):
54
  time.sleep(5)
55
  return process
56
 
57
- # GUI-specific utilities (unchanged from your original)
58
  def format_time(seconds_float):
59
  total_seconds = int(round(seconds_float))
60
  hours = total_seconds // 3600
@@ -86,11 +84,14 @@ details { border: 1px solid #e0e0e0 !important; border-radius: 8px !important; p
86
 
87
  client = OpenAI(base_url="http://localhost:8080/v1", api_key="no-key-required")
88
 
89
- # 修改 user() 確保 message 為字串
90
  def user(message, history):
91
  if not isinstance(message, str):
92
  message = str(message)
93
- return "", history + [[message, None]]
 
 
 
94
 
95
  class ParserState:
96
  __slots__ = ['answer', 'thought', 'in_think', 'start_time', 'last_pos', 'total_think_time']
@@ -148,13 +149,15 @@ def format_response(state, elapsed):
148
  )
149
  return collapsible, answer_part
150
 
 
151
  def generate_response(history, temperature, top_p, max_tokens, active_gen):
152
- messages = [
153
- {"role": "system", "content": "You are a helpful assistant."},
154
- *[{"role": "user" if i % 2 == 0 else "assistant", "content": msg or ""}
155
- for i, (user_msg, assistant_msg) in enumerate(history[:-1])],
156
- {"role": "user", "content": history[-1][0]}
157
- ]
 
158
  full_response = ""
159
  state = ParserState()
160
  try:
@@ -173,14 +176,24 @@ def generate_response(history, temperature, top_p, max_tokens, active_gen):
173
  full_response += chunk.choices[0].delta.content
174
  state, elapsed = parse_response(full_response, state)
175
  collapsible, answer_part = format_response(state, elapsed)
176
- history[-1][1] = "\n\n".join(collapsible + [answer_part])
 
 
 
 
177
  yield history
178
  state, elapsed = parse_response(full_response, state)
179
  collapsible, answer_part = format_response(state, elapsed)
180
- history[-1][1] = "\n\n".join(collapsible + [answer_part])
 
 
 
181
  yield history
182
  except Exception as e:
183
- history[-1][1] = f"Error: {str(e)}"
 
 
 
184
  yield history
185
  finally:
186
  active_gen[0] = False
@@ -195,8 +208,8 @@ with gr.Blocks(css=CSS) as demo:
195
  height=500,
196
  show_label=False,
197
  render_markdown=True,
198
- value=[], # 初始值為空列表
199
- type="messages" # 指定使用 messages 格式
200
  )
201
 
202
  with gr.Row():
@@ -259,7 +272,7 @@ if __name__ == "__main__":
259
  # Start llama-server
260
  server_process = start_llama_server(MODEL_PATH)
261
  try:
262
- # Launch GUI
263
  demo.launch(server_name="0.0.0.0", server_port=7860)
264
  finally:
265
  # Cleanup: terminate the server process when the GUI is closed
 
17
  print(f"Command succeeded: {command}")
18
  print(result.stdout)
19
 
 
 
20
  # Model configuration
21
  MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
22
  QUANT = "Q5_K_M"
 
52
  time.sleep(5)
53
  return process
54
 
55
+ # GUI-specific utilities
56
  def format_time(seconds_float):
57
  total_seconds = int(round(seconds_float))
58
  hours = total_seconds // 3600
 
84
 
85
  client = OpenAI(base_url="http://localhost:8080/v1", api_key="no-key-required")
86
 
87
+ # Update the user() function to use dictionary format
88
  def user(message, history):
89
  if not isinstance(message, str):
90
  message = str(message)
91
+ history = history if history is not None else []
92
+ # Append the user message as a dict
93
+ history.append({"role": "user", "content": message})
94
+ return "", history
95
 
96
  class ParserState:
97
  __slots__ = ['answer', 'thought', 'in_think', 'start_time', 'last_pos', 'total_think_time']
 
149
  )
150
  return collapsible, answer_part
151
 
152
+ # Modified generate_response() using dictionary-format history
153
  def generate_response(history, temperature, top_p, max_tokens, active_gen):
154
+ # Guard against empty history.
155
+ if not history:
156
+ yield []
157
+ return
158
+
159
+ # Build messages: system message + conversation history.
160
+ messages = [{"role": "system", "content": "You are a helpful assistant."}] + history
161
  full_response = ""
162
  state = ParserState()
163
  try:
 
176
  full_response += chunk.choices[0].delta.content
177
  state, elapsed = parse_response(full_response, state)
178
  collapsible, answer_part = format_response(state, elapsed)
179
+ # Update or add the assistant reply in history
180
+ if history and history[-1].get("role") == "assistant":
181
+ history[-1]["content"] = "\n\n".join(collapsible + [answer_part])
182
+ else:
183
+ history.append({"role": "assistant", "content": "\n\n".join(collapsible + [answer_part])})
184
  yield history
185
  state, elapsed = parse_response(full_response, state)
186
  collapsible, answer_part = format_response(state, elapsed)
187
+ if history and history[-1].get("role") == "assistant":
188
+ history[-1]["content"] = "\n\n".join(collapsible + [answer_part])
189
+ else:
190
+ history.append({"role": "assistant", "content": "\n\n".join(collapsible + [answer_part])})
191
  yield history
192
  except Exception as e:
193
+ if history and history[-1].get("role") == "assistant":
194
+ history[-1]["content"] = f"Error: {str(e)}"
195
+ else:
196
+ history.append({"role": "assistant", "content": f"Error: {str(e)}"})
197
  yield history
198
  finally:
199
  active_gen[0] = False
 
208
  height=500,
209
  show_label=False,
210
  render_markdown=True,
211
+ value=[], # initial value as an empty list
212
+ type="messages" # use messages format (dict with role and content)
213
  )
214
 
215
  with gr.Row():
 
272
  # Start llama-server
273
  server_process = start_llama_server(MODEL_PATH)
274
  try:
275
+ # Launch GUI (set share=True if you need a public link)
276
  demo.launch(server_name="0.0.0.0", server_port=7860)
277
  finally:
278
  # Cleanup: terminate the server process when the GUI is closed