luminoussg commited on
Commit
3340c69
·
verified ·
1 Parent(s): c4bae57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -2
app.py CHANGED
@@ -5,6 +5,7 @@ import threading
5
  from datetime import datetime
6
  from typing import List, Dict, Any, Generator
7
  from session_manager import SessionManager
 
8
 
9
  # Initialize session manager and get HF API key
10
  session_manager = SessionManager()
@@ -66,9 +67,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
- # Clean up response formatting
70
  result = result.split('<|')[0] # Remove any remaining special tokens
71
  result = result.replace('**', '').replace('##', '') # Remove markdown
 
 
 
 
 
 
 
72
  result = result.strip() # Remove leading/trailing whitespace
73
  return result # Return complete response
74
  except Exception as e:
@@ -138,9 +146,22 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
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)
 
5
  from datetime import datetime
6
  from typing import List, Dict, Any, Generator
7
  from session_manager import SessionManager
8
+ import katex
9
 
10
  # Initialize session manager and get HF API key
11
  session_manager = SessionManager()
 
67
  response = requests.post(endpoint, json=payload, headers=headers)
68
  response.raise_for_status()
69
  result = response.json()[0]['generated_text']
70
+ # Clean up response formatting and handle LaTeX
71
  result = result.split('<|')[0] # Remove any remaining special tokens
72
  result = result.replace('**', '').replace('##', '') # Remove markdown
73
+
74
+ # Detect and format LaTeX content
75
+ import re
76
+ # Handle inline LaTeX
77
+ result = re.sub(r'\\\((.*?)\\\)', r'<span class="katex">\1</span>', result)
78
+ # Handle display LaTeX
79
+ result = re.sub(r'\\\[(.*?)\\\]', r'<div class="katex">\1</div>', result)
80
  result = result.strip() # Remove leading/trailing whitespace
81
  return result # Return complete response
82
  except Exception as e:
 
146
  # Return final combined response
147
  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}"
148
 
149
+ # Create the Gradio interface with LaTeX support
150
  with gr.Blocks() as demo:
151
  gr.Markdown("## Multi-LLM Collaboration Chat")
152
+ # Add KaTeX CSS and JS
153
+ demo.css = """
154
+ .katex { font-size: 1.1em; }
155
+ """
156
+ demo.js = """
157
+ function renderLatex() {
158
+ document.querySelectorAll('.katex').forEach(el => {
159
+ katex.render(el.textContent, el, {
160
+ throwOnError: false
161
+ });
162
+ });
163
+ }
164
+ """
165
 
166
  with gr.Row():
167
  session_id = gr.State(session_manager.create_session)