luminoussg commited on
Commit
8190eb3
Β·
verified Β·
1 Parent(s): 0f9606a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -44
app.py CHANGED
@@ -18,7 +18,7 @@ MODEL_ENDPOINTS = {
18
  }
19
 
20
  def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
21
- """Query a single model with the chat history."""
22
  endpoint = MODEL_ENDPOINTS[model_name]
23
  headers = {
24
  "Authorization": f"Bearer {HF_API_KEY}",
@@ -28,7 +28,7 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
28
  # Build full conversation history for context
29
  conversation = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
30
 
31
- # Model-specific prompt formatting
32
  model_prompts = {
33
  "Qwen2.5-72B-Instruct": (
34
  f"<|im_start|>system\nCollaborate with other experts. Previous discussion:\n{conversation}<|im_end|>\n"
@@ -66,15 +66,16 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
66
  response = requests.post(endpoint, json=payload, headers=headers)
67
  response.raise_for_status()
68
  result = response.json()[0]['generated_text']
69
- # Basic cleanup
70
- result = result.split('<|')[0] # Remove special tokens
71
- result = result.replace('**', '').replace('##', '')
72
- return result.strip()
 
73
  except Exception as e:
74
  return f"{model_name} error: {str(e)}"
75
 
76
  def respond(message: str, history: List[List[str]], session_id: str) -> Generator[str, None, None]:
77
- """Handle sequential model responses with context preservation."""
78
  # Load or initialize session
79
  session = session_manager.load_session(session_id)
80
  if not isinstance(session, dict) or "history" not in session:
@@ -131,51 +132,31 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
131
  })
132
  messages.append({"role": "assistant", "content": f"Llama3.3-70B-Instruct: {response3}"})
133
 
134
- # Save session
135
  session_manager.save_session(session_id, session)
136
 
137
  # Return final combined response
138
- yield (
139
- f"πŸ”΅ **Qwen2.5-Coder-32B-Instruct**\n{response1}\n\n"
140
- f"🟣 **Qwen2.5-72B-Instruct**\n{response2}\n\n"
141
- f"🟑 **Llama3.3-70B-Instruct**\n{response3}"
142
- )
143
 
 
144
  with gr.Blocks() as demo:
145
- # -- Include KaTeX for LaTeX rendering --
146
- gr.HTML("""
147
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" />
148
- <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
149
- <script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>
150
- <script>
151
- // Re-render math whenever new content is added
152
- document.addEventListener("DOMContentLoaded", function() {
153
- const observer = new MutationObserver(function(mutations) {
154
- for (const mutation of mutations) {
155
- if (mutation.type === 'childList') {
156
- renderMathInElement(document.body, {
157
- delimiters: [
158
- {left: "$$", right: "$$", display: true},
159
- {left: "$", right: "$", display: false},
160
- ]
161
- });
162
- }
163
- }
164
- });
165
- observer.observe(document.body, { subtree: true, childList: true });
166
- });
167
- </script>
168
- """)
169
-
170
- gr.Markdown("## Multi-LLM Collaboration Chat (with LaTeX support)")
171
-
172
  with gr.Row():
173
  session_id = gr.State(session_manager.create_session)
174
  new_session = gr.Button("πŸ”„ New Session")
175
 
176
- chatbot = gr.Chatbot(height=600)
177
- msg = gr.Textbox(label="Message (Use $...$ or $$...$$ for LaTeX)")
178
-
 
 
 
 
 
 
 
 
179
  def on_new_session():
180
  new_id = session_manager.create_session()
181
  return new_id, []
@@ -189,7 +170,7 @@ with gr.Blocks() as demo:
189
  for response in respond(message, history[:-1], session_id):
190
  history[-1][1] = response
191
  yield history
192
-
193
  msg.submit(user, [msg, chatbot, session_id], [msg, chatbot]).then(
194
  bot, [chatbot, session_id], [chatbot]
195
  )
 
18
  }
19
 
20
  def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
21
+ """Query a single model with the chat history"""
22
  endpoint = MODEL_ENDPOINTS[model_name]
23
  headers = {
24
  "Authorization": f"Bearer {HF_API_KEY}",
 
28
  # Build full conversation history for context
29
  conversation = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
30
 
31
+ # Model-specific prompt formatting with full history
32
  model_prompts = {
33
  "Qwen2.5-72B-Instruct": (
34
  f"<|im_start|>system\nCollaborate with other experts. Previous discussion:\n{conversation}<|im_end|>\n"
 
66
  response = requests.post(endpoint, json=payload, headers=headers)
67
  response.raise_for_status()
68
  result = response.json()[0]['generated_text']
69
+ # Clean up response formatting
70
+ result = result.split('<|')[0] # Remove any remaining special tokens
71
+ result = result.replace('**', '').replace('##', '') # Remove markdown emphasis
72
+ result = result.strip() # Remove leading/trailing whitespace
73
+ return result
74
  except Exception as e:
75
  return f"{model_name} error: {str(e)}"
76
 
77
  def respond(message: str, history: List[List[str]], session_id: str) -> Generator[str, None, None]:
78
+ """Handle sequential model responses with context preservation"""
79
  # Load or initialize session
80
  session = session_manager.load_session(session_id)
81
  if not isinstance(session, dict) or "history" not in session:
 
132
  })
133
  messages.append({"role": "assistant", "content": f"Llama3.3-70B-Instruct: {response3}"})
134
 
135
+ # Save final session state
136
  session_manager.save_session(session_id, session)
137
 
138
  # Return final combined response
139
+ yield f"πŸ”΅ **Qwen2.5-Coder-32B-Instruct**\n{response1}\n\n🟣 **Qwen2.5-72B-Instruct**\n{response2}\n\n🟑 **Llama3.3-70B-Instruct**\n{response3}"
 
 
 
 
140
 
141
+ # Create the Gradio interface
142
  with gr.Blocks() as demo:
143
+ gr.Markdown("## Multi-LLM Collaboration Chat")
144
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  with gr.Row():
146
  session_id = gr.State(session_manager.create_session)
147
  new_session = gr.Button("πŸ”„ New Session")
148
 
149
+ # Add latex_delimiters to enable LaTeX rendering
150
+ chatbot = gr.Chatbot(
151
+ height=600,
152
+ latex_delimiters=[
153
+ {"left": "$", "right": "$", "display": False}, # inline math
154
+ {"left": "$$", "right": "$$", "display": True} # display math
155
+ ]
156
+ )
157
+
158
+ msg = gr.Textbox(label="Message")
159
+
160
  def on_new_session():
161
  new_id = session_manager.create_session()
162
  return new_id, []
 
170
  for response in respond(message, history[:-1], session_id):
171
  history[-1][1] = response
172
  yield history
173
+
174
  msg.submit(user, [msg, chatbot, session_id], [msg, chatbot]).then(
175
  bot, [chatbot, session_id], [chatbot]
176
  )