Daemontatox commited on
Commit
69d91ca
·
verified ·
1 Parent(s): 237811d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -45
app.py CHANGED
@@ -88,7 +88,19 @@ example_messages = {
88
  "LaTeX example": "Solve the quadratic equation ax^2 + bx + c = 0 and explain the solution. Then calculate the roots of 2x^2 - 5x + 3 = 0."
89
  }
90
 
91
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
92
  gr.Markdown(
93
  """
94
  # Problem Solving with LaTeX Math Support
@@ -96,6 +108,41 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
96
  """
97
  )
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  history_state = gr.State([])
100
 
101
  with gr.Row():
@@ -136,13 +183,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
136
  )
137
 
138
  with gr.Column(scale=4):
139
- # Use the markdown flag to ensure proper rendering of LaTeX
140
  chatbot = gr.Chatbot(
141
  label="Chat",
142
  render_markdown=True,
 
143
  elem_id="chatbot",
144
  show_copy_button=True,
145
- bubble_full_width=False,
146
  avatar_images=(None, None)
147
  )
148
  with gr.Row():
@@ -160,48 +207,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
160
  example3_button = gr.Button("Physics problem")
161
  example4_button = gr.Button("LaTeX example")
162
 
163
- # Add custom JavaScript to ensure LaTeX rendering
164
- demo.load(None, None, None, _js="""
165
- function() {
166
- // Check if MathJax is available
167
- if (typeof window.MathJax === 'undefined') {
168
- // Load MathJax if not available
169
- const script = document.createElement('script');
170
- script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML';
171
- script.async = true;
172
- document.head.appendChild(script);
173
-
174
- // Configure MathJax
175
- window.MathJax = {
176
- tex2jax: {
177
- inlineMath: [['$', '$']],
178
- displayMath: [['$$', '$$']],
179
- processEscapes: true
180
- },
181
- showProcessingMessages: false,
182
- messageStyle: 'none'
183
- };
184
- }
185
-
186
- // Function to render LaTeX in the chatbot
187
- function renderMathInChatbot() {
188
- if (window.MathJax && window.MathJax.Hub) {
189
- window.MathJax.Hub.Queue(['Typeset', window.MathJax.Hub, document.getElementById('chatbot')]);
190
- }
191
- }
192
-
193
- // Set up a mutation observer to detect changes in the chatbot
194
- const observer = new MutationObserver(renderMathInChatbot);
195
- const chatbot = document.getElementById('chatbot');
196
- if (chatbot) {
197
- observer.observe(chatbot, { childList: true, subtree: true });
198
- }
199
-
200
- // Initial render
201
- renderMathInChatbot();
202
- }
203
- """)
204
-
205
  submit_button.click(
206
  fn=generate_response,
207
  inputs=[user_input, max_tokens_slider, temperature_slider, top_k_slider, top_p_slider, repetition_penalty_slider, history_state],
 
88
  "LaTeX example": "Solve the quadratic equation ax^2 + bx + c = 0 and explain the solution. Then calculate the roots of 2x^2 - 5x + 3 = 0."
89
  }
90
 
91
+ # Custom CSS for better LaTeX display
92
+ css = """
93
+ .markdown-body .katex {
94
+ font-size: 1.2em;
95
+ }
96
+ .markdown-body .katex-display {
97
+ margin: 1em 0;
98
+ overflow-x: auto;
99
+ overflow-y: hidden;
100
+ }
101
+ """
102
+
103
+ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
104
  gr.Markdown(
105
  """
106
  # Problem Solving with LaTeX Math Support
 
108
  """
109
  )
110
 
111
+ # Add JavaScript for MathJax loading
112
+ gr.HTML("""
113
+ <script>
114
+ // Check if MathJax is available
115
+ if (typeof window.MathJax === 'undefined') {
116
+ // Load MathJax if not available
117
+ const script = document.createElement('script');
118
+ script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML';
119
+ script.async = true;
120
+ document.head.appendChild(script);
121
+
122
+ // Configure MathJax
123
+ window.MathJax = {
124
+ tex2jax: {
125
+ inlineMath: [['$', '$']],
126
+ displayMath: [['$$', '$$']],
127
+ processEscapes: true
128
+ },
129
+ showProcessingMessages: false,
130
+ messageStyle: 'none'
131
+ };
132
+ }
133
+
134
+ // Set up a rerender function
135
+ function rerender() {
136
+ if (window.MathJax && window.MathJax.Hub) {
137
+ window.MathJax.Hub.Queue(['Typeset', window.MathJax.Hub]);
138
+ }
139
+ }
140
+
141
+ // Call rerender periodically
142
+ setInterval(rerender, 1000);
143
+ </script>
144
+ """)
145
+
146
  history_state = gr.State([])
147
 
148
  with gr.Row():
 
183
  )
184
 
185
  with gr.Column(scale=4):
186
+ # Use the markdown flag and type='messages' to ensure proper rendering of LaTeX
187
  chatbot = gr.Chatbot(
188
  label="Chat",
189
  render_markdown=True,
190
+ type="messages",
191
  elem_id="chatbot",
192
  show_copy_button=True,
 
193
  avatar_images=(None, None)
194
  )
195
  with gr.Row():
 
207
  example3_button = gr.Button("Physics problem")
208
  example4_button = gr.Button("LaTeX example")
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  submit_button.click(
211
  fn=generate_response,
212
  inputs=[user_input, max_tokens_slider, temperature_slider, top_k_slider, top_p_slider, repetition_penalty_slider, history_state],