luminoussg commited on
Commit
0d6849e
·
verified ·
1 Parent(s): a66103f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -10
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import os
3
  import requests
4
  import threading
 
5
  from typing import List, Dict, Any
6
 
7
  # Get the Hugging Face API key from Spaces secrets
@@ -71,10 +72,19 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
71
  except Exception as e:
72
  return f"{model_name} error: {str(e)}"
73
 
74
- def respond(message: str, history: List[List[str]]) -> str:
75
- """Handle sequential model responses with individual updates"""
 
 
76
  messages = [{"role": "user", "content": message}]
77
 
 
 
 
 
 
 
 
78
  # Get first model's response
79
  response1 = query_model("Qwen2.5-Coder-32B-Instruct", messages)
80
  yield f"**Qwen2.5-Coder-32B-Instruct**:\n{response1}"
@@ -99,14 +109,32 @@ def respond(message: str, history: List[List[str]]) -> str:
99
  response3 = query_model("Llama3.3-70B-Instruct", messages)
100
  yield f"**Llama3.3-70B-Instruct**:\n{response3}"
101
 
102
- # Create the Gradio interface
103
- chat_interface = gr.ChatInterface(
104
- respond,
105
- title="Multi-LLM Collaboration Chat",
106
- description="A group chat with Qwen2.5-72B, Llama3.3-70B, and Qwen2.5-Coder-32B",
107
- examples=["How can I optimize Python code?", "Explain quantum computing basics"],
108
- theme="soft"
109
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  if __name__ == "__main__":
112
  chat_interface.launch(share=True)
 
2
  import os
3
  import requests
4
  import threading
5
+ from datetime import datetime
6
  from typing import List, Dict, Any
7
 
8
  # Get the Hugging Face API key from Spaces secrets
 
72
  except Exception as e:
73
  return f"{model_name} error: {str(e)}"
74
 
75
+ def respond(message: str, history: List[List[str]], session_id: str) -> str:
76
+ """Handle sequential model responses with session tracking"""
77
+ # Load session history
78
+ session = session_manager.load_session(session_id)
79
  messages = [{"role": "user", "content": message}]
80
 
81
+ # Store user message in session
82
+ session["history"].append({
83
+ "timestamp": datetime.now().isoformat(),
84
+ "type": "user",
85
+ "content": message
86
+ })
87
+
88
  # Get first model's response
89
  response1 = query_model("Qwen2.5-Coder-32B-Instruct", messages)
90
  yield f"**Qwen2.5-Coder-32B-Instruct**:\n{response1}"
 
109
  response3 = query_model("Llama3.3-70B-Instruct", messages)
110
  yield f"**Llama3.3-70B-Instruct**:\n{response3}"
111
 
112
+ # Create the Gradio interface with session management
113
+ with gr.Blocks(title="Multi-LLM Collaboration Chat") as demo:
114
+ session_id = gr.State(session_manager.create_session)
115
+
116
+ with gr.Row():
117
+ gr.Markdown("## Multi-LLM Collaboration Chat")
118
+ new_session_btn = gr.Button("🆕 New Session", variant="secondary")
119
+
120
+ with gr.Row():
121
+ gr.Markdown("A group chat with Qwen2.5-72B, Llama3.3-70B, and Qwen2.5-Coder-32B")
122
+
123
+ chat_interface = gr.ChatInterface(
124
+ respond,
125
+ examples=["How can I optimize Python code?", "Explain quantum computing basics"],
126
+ additional_inputs=[session_id]
127
+ )
128
+
129
+ def create_new_session():
130
+ new_id = session_manager.create_session()
131
+ return new_id, None
132
+
133
+ new_session_btn.click(
134
+ fn=create_new_session,
135
+ outputs=[session_id, chat_interface.chatbot],
136
+ show_progress=False
137
+ )
138
 
139
  if __name__ == "__main__":
140
  chat_interface.launch(share=True)