luminoussg commited on
Commit
0f9606a
Β·
verified Β·
1 Parent(s): 149cacc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -26
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import gradio as gr
3
  import os
4
  import requests
@@ -19,7 +18,7 @@ MODEL_ENDPOINTS = {
19
  }
20
 
21
  def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
22
- """Query a single model with the chat history"""
23
  endpoint = MODEL_ENDPOINTS[model_name]
24
  headers = {
25
  "Authorization": f"Bearer {HF_API_KEY}",
@@ -29,7 +28,7 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
29
  # Build full conversation history for context
30
  conversation = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
31
 
32
- # Model-specific prompt formatting with full history
33
  model_prompts = {
34
  "Qwen2.5-72B-Instruct": (
35
  f"<|im_start|>system\nCollaborate with other experts. Previous discussion:\n{conversation}<|im_end|>\n"
@@ -67,14 +66,15 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
67
  response = requests.post(endpoint, json=payload, headers=headers)
68
  response.raise_for_status()
69
  result = response.json()[0]['generated_text']
70
- # Do NOT remove ** or ## so that Markdown (including LaTeX) is preserved
71
- result = result.split('<|')[0] # Remove any remaining special tokens
72
- return result.strip() # Return the cleaned response
 
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,34 +131,51 @@ 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 final session state
135
  session_manager.save_session(session_id, session)
136
 
137
  # Return final combined response
138
- 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}"
 
 
 
 
139
 
140
- # Create the Gradio interface
141
  with gr.Blocks() as demo:
142
- gr.Markdown("## Multi-LLM Collaboration Chat")
143
- gr.Markdown(
144
- """
145
- **LaTeX Support:** You can include inline math like `$E = mc^2$` or block math like:
146
- ```
147
- $$
148
- \\int_0^1 x^2 \\, dx
149
- $$
150
- ```
151
- and it will be rendered correctly below.
152
- """
153
- )
154
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  with gr.Row():
156
  session_id = gr.State(session_manager.create_session)
157
  new_session = gr.Button("πŸ”„ New Session")
158
 
159
  chatbot = gr.Chatbot(height=600)
160
- msg = gr.Textbox(label="Message")
161
-
162
  def on_new_session():
163
  new_id = session_manager.create_session()
164
  return new_id, []
@@ -172,7 +189,7 @@ with gr.Blocks() as demo:
172
  for response in respond(message, history[:-1], session_id):
173
  history[-1][1] = response
174
  yield history
175
-
176
  msg.submit(user, [msg, chatbot, session_id], [msg, chatbot]).then(
177
  bot, [chatbot, session_id], [chatbot]
178
  )
 
 
1
  import gradio as gr
2
  import os
3
  import requests
 
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
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
+ # 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
  })
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
  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
  )