AIencoder commited on
Commit
636da78
·
verified ·
1 Parent(s): c3c3dca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -177,13 +177,9 @@ def export_chat_history(history):
177
  export = {
178
  "exported_at": datetime.now().isoformat(),
179
  "tool": "Axon v6 Chat",
180
- "messages": []
181
  }
182
 
183
- for user_msg, assistant_msg in history:
184
- export["messages"].append({"role": "user", "content": user_msg})
185
- export["messages"].append({"role": "assistant", "content": assistant_msg})
186
-
187
  filename = f"/tmp/axon_chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
188
  with open(filename, "w") as f:
189
  json.dump(export, f, indent=2)
@@ -209,41 +205,62 @@ def export_code(code, language):
209
 
210
  return filename, f"✅ Exported as .{ext}!"
211
 
212
- # ===== STREAMING =====
213
 
214
  def chat_stream(message, history, model_name, temperature, max_tokens):
 
 
 
215
  valid, error = validate_input(message, "Message")
216
  if not valid:
217
- yield history + [[message, error]]
 
 
 
218
  return
219
 
220
  llm = load_model(model_name)
221
  if not llm:
222
- yield history + [[message, " Model not available."]]
 
 
223
  return
224
 
 
225
  if "deepseek" in model_name.lower():
226
  conv = "### Instruction:\nYou are an expert coding assistant. Use markdown code blocks.\n\n"
227
- for u, a in history:
228
- conv += f"User: {u}\nAssistant: {a}\n\n"
 
 
 
229
  conv += f"User: {message}\n\n### Response:\n"
230
  stop_tokens = ["### Instruction:", "User:"]
231
  else:
232
  conv = "<|im_start|>system\nYou are an expert coding assistant. Use markdown code blocks.<|im_end|>\n"
233
- for u, a in history:
234
- conv += f"<|im_start|>user\n{u}<|im_end|>\n"
235
- if a:
236
- conv += f"<|im_start|>assistant\n{a}<|im_end|>\n"
237
  conv += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
238
  stop_tokens = ["<|im_end|>", "<|im_start|>"]
239
 
 
 
 
 
 
240
  try:
241
  full = ""
242
  for chunk in llm(conv, max_tokens=max_tokens, temperature=temperature, top_p=0.9, stop=stop_tokens, stream=True):
243
- full += chunk["choices"][0]["text"]
244
- yield history + [[message, full]]
 
 
 
245
  except Exception as e:
246
- yield history + [[message, f"❌ {str(e)[:50]}"]]
 
247
 
248
  def generate_stream(prompt, language, model_name, temperature, max_tokens):
249
  valid, error = validate_input(prompt, "Description")
@@ -573,7 +590,7 @@ with gr.Blocks(title="Axon v6", theme=dark_theme) as demo:
573
 
574
  # ===== CHAT =====
575
  with gr.TabItem("💬 Chat"):
576
- chatbot = gr.Chatbot(height=400, type="tuples")
577
  with gr.Row():
578
  msg = gr.Textbox(placeholder="Ask anything...", show_label=False, scale=8)
579
  send = gr.Button("Send", variant="primary", scale=1)
@@ -812,7 +829,7 @@ with gr.Blocks(title="Axon v6", theme=dark_theme) as demo:
812
  # ===== EVENTS =====
813
 
814
  def respond(message, history, model, temp, tokens):
815
- history = history or []
816
  for updated in chat_stream(message, history, model, temp, tokens):
817
  yield updated, ""
818
 
 
177
  export = {
178
  "exported_at": datetime.now().isoformat(),
179
  "tool": "Axon v6 Chat",
180
+ "messages": history # Direct dump for Gradio 5 format
181
  }
182
 
 
 
 
 
183
  filename = f"/tmp/axon_chat_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
184
  with open(filename, "w") as f:
185
  json.dump(export, f, indent=2)
 
205
 
206
  return filename, f"✅ Exported as .{ext}!"
207
 
208
+ # ===== STREAMING (UPDATED FOR GRADIO 5) =====
209
 
210
  def chat_stream(message, history, model_name, temperature, max_tokens):
211
+ # Initialize history if None (Gradio 5 sometimes sends None on first load)
212
+ history = history or []
213
+
214
  valid, error = validate_input(message, "Message")
215
  if not valid:
216
+ # Append error as assistant message
217
+ history.append({"role": "user", "content": message})
218
+ history.append({"role": "assistant", "content": error})
219
+ yield history
220
  return
221
 
222
  llm = load_model(model_name)
223
  if not llm:
224
+ history.append({"role": "user", "content": message})
225
+ history.append({"role": "assistant", "content": "❌ Model not available."})
226
+ yield history
227
  return
228
 
229
+ # Build conversation string from Dict history
230
  if "deepseek" in model_name.lower():
231
  conv = "### Instruction:\nYou are an expert coding assistant. Use markdown code blocks.\n\n"
232
+ for msg in history:
233
+ if msg['role'] == 'user':
234
+ conv += f"User: {msg['content']}\n"
235
+ else:
236
+ conv += f"Assistant: {msg['content']}\n\n"
237
  conv += f"User: {message}\n\n### Response:\n"
238
  stop_tokens = ["### Instruction:", "User:"]
239
  else:
240
  conv = "<|im_start|>system\nYou are an expert coding assistant. Use markdown code blocks.<|im_end|>\n"
241
+ for msg in history:
242
+ role = msg['role']
243
+ content = msg['content']
244
+ conv += f"<|im_start|>{role}\n{content}<|im_end|>\n"
245
  conv += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
246
  stop_tokens = ["<|im_end|>", "<|im_start|>"]
247
 
248
+ # Add the new user message to history
249
+ history.append({"role": "user", "content": message})
250
+ # Add a placeholder for the assistant response
251
+ history.append({"role": "assistant", "content": ""})
252
+
253
  try:
254
  full = ""
255
  for chunk in llm(conv, max_tokens=max_tokens, temperature=temperature, top_p=0.9, stop=stop_tokens, stream=True):
256
+ text_chunk = chunk["choices"][0]["text"]
257
+ full += text_chunk
258
+ # Update the last message (assistant's response)
259
+ history[-1]['content'] = full
260
+ yield history
261
  except Exception as e:
262
+ history[-1]['content'] = f"❌ Error: {str(e)[:100]}"
263
+ yield history
264
 
265
  def generate_stream(prompt, language, model_name, temperature, max_tokens):
266
  valid, error = validate_input(prompt, "Description")
 
590
 
591
  # ===== CHAT =====
592
  with gr.TabItem("💬 Chat"):
593
+ chatbot = gr.Chatbot(height=400, type="messages")
594
  with gr.Row():
595
  msg = gr.Textbox(placeholder="Ask anything...", show_label=False, scale=8)
596
  send = gr.Button("Send", variant="primary", scale=1)
 
829
  # ===== EVENTS =====
830
 
831
  def respond(message, history, model, temp, tokens):
832
+ # Gradio 5 automatically handles history state
833
  for updated in chat_stream(message, history, model, temp, tokens):
834
  yield updated, ""
835