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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -5,7 +5,7 @@ import threading
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()
@@ -72,11 +72,13 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
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:
@@ -149,18 +151,19 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
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():
 
5
  from datetime import datetime
6
  from typing import List, Dict, Any, Generator
7
  from session_manager import SessionManager
8
+ from sympy import latex, sympify
9
 
10
  # Initialize session manager and get HF API key
11
  session_manager = SessionManager()
 
72
  result = result.replace('**', '').replace('##', '') # Remove markdown
73
 
74
  # Detect and format LaTeX content
75
+ try:
76
+ # Convert mathematical expressions to LaTeX
77
+ result = latex(sympify(result, evaluate=False))
78
+ result = f'<div class="math">{result}</div>'
79
+ except:
80
+ # If conversion fails, return the original text
81
+ pass
82
  result = result.strip() # Remove leading/trailing whitespace
83
  return result # Return complete response
84
  except Exception as e:
 
151
  # Create the Gradio interface with LaTeX support
152
  with gr.Blocks() as demo:
153
  gr.Markdown("## Multi-LLM Collaboration Chat")
154
+ # Add MathJax configuration for LaTeX rendering
155
  demo.css = """
156
+ .math { font-size: 1.1em; }
157
  """
158
  demo.js = """
159
+ MathJax = {
160
+ tex: {
161
+ inlineMath: [['$', '$'], ['\\(', '\\)']]
162
+ },
163
+ svg: {
164
+ fontCache: 'global'
165
+ }
166
+ };
167
  """
168
 
169
  with gr.Row():