shukdevdattaEX commited on
Commit
f0bc96c
Β·
verified Β·
1 Parent(s): 40a7c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -33
app.py CHANGED
@@ -177,6 +177,16 @@ def markdown_to_html(markdown_text, problem_text="", include_problem=True):
177
  .print-button:hover {{
178
  background-color: #0056b3;
179
  }}
 
 
 
 
 
 
 
 
 
 
180
  @media print {{
181
  body {{
182
  background-color: white;
@@ -188,6 +198,9 @@ def markdown_to_html(markdown_text, problem_text="", include_problem=True):
188
  .print-button {{
189
  display: none;
190
  }}
 
 
 
191
  }}
192
  </style>
193
  <script>
@@ -212,7 +225,7 @@ def markdown_to_html(markdown_text, problem_text="", include_problem=True):
212
  </div>
213
  ''' if include_problem and problem_text.strip() else ''}
214
 
215
- <div class="solution-content">
216
  <h2>πŸ” Solution</h2>
217
  {html_content}
218
  </div>
@@ -227,6 +240,22 @@ def markdown_to_html(markdown_text, problem_text="", include_problem=True):
227
 
228
  return styled_html
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  # Function to save HTML to file
231
  def save_html_to_file(html_content, filename_prefix="math_solution"):
232
  """Save HTML content to a temporary file and return the file path"""
@@ -299,12 +328,15 @@ def generate_math_solution_openrouter(api_key, problem_text, history=None):
299
  if history:
300
  for exchange in history:
301
  messages.append({"role": "user", "content": exchange[0]})
302
- if len(exchange) > 1 and exchange[1]: # Check if there's a response
303
  messages.append({"role": "assistant", "content": exchange[1]})
304
 
305
  # Add the current problem
306
  messages.append({"role": "user", "content": f"Solve this math problem step-by-step: {problem_text}"})
307
 
 
 
 
308
  # Create the completion
309
  completion = client.chat.completions.create(
310
  model="microsoft/phi-4-reasoning-plus:free",
@@ -331,11 +363,11 @@ def generate_math_solution_openrouter(api_key, problem_text, history=None):
331
  history = []
332
  history.append((problem_text, markdown_solution))
333
 
334
- return html_solution, html_file_path, pdf_file_path, history
335
 
336
  except Exception as e:
337
  error_message = f"Error: {str(e)}"
338
- return error_message, None, None, history
339
 
340
  # Enhanced function to generate math solution using Together AI with HTML output
341
  def generate_math_solution_together(api_key, problem_text, image_path=None, history=None):
@@ -376,7 +408,7 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
376
  if history:
377
  for exchange in history:
378
  messages.append({"role": "user", "content": exchange[0]})
379
- if len(exchange) > 1 and exchange[1]: # Check if there's a response
380
  messages.append({"role": "assistant", "content": exchange[1]})
381
 
382
  # Prepare the user message content
@@ -412,6 +444,9 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
412
  "content": user_message_content
413
  })
414
 
 
 
 
415
  # Create the completion
416
  response = client.chat.completions.create(
417
  model="meta-llama/Llama-Vision-Free",
@@ -431,16 +466,16 @@ def generate_math_solution_together(api_key, problem_text, image_path=None, hist
431
  # Convert to PDF
432
  pdf_file_path = html_to_pdf(html_solution, "together_solution")
433
 
434
- # Update history - for simplicity, just store the text problem
435
  if history is None:
436
  history = []
437
  history.append((problem_display, markdown_solution))
438
 
439
- return html_solution, html_file_path, pdf_file_path, history
440
 
441
  except Exception as e:
442
  error_message = f"Error: {str(e)}"
443
- return error_message, None, None, history
444
 
445
  # Function to convert image to base64
446
  def image_to_base64(image_path):
@@ -462,7 +497,7 @@ def create_demo():
462
  This application provides step-by-step solutions to math problems using advanced AI models.
463
  Solutions are generated in **HTML format** with download and print-to-PDF capabilities.
464
  Choose between OpenRouter's Phi-4-reasoning-plus for text-based problems or Together AI's
465
- Llama-Vision for problems with images.
466
  """)
467
 
468
  # Main tabs
@@ -513,17 +548,14 @@ def create_demo():
513
 
514
  # Button actions
515
  def handle_openrouter_submit(api_key, problem_text, history):
516
- html_solution, html_file, pdf_file, updated_history = generate_math_solution_openrouter(
517
- api_key, problem_text, history
518
- )
519
-
520
- # Return outputs including file updates
521
- return (
522
- html_solution,
523
- updated_history,
524
- gr.update(value=html_file, visible=html_file is not None),
525
- gr.update(value=pdf_file, visible=pdf_file is not None)
526
- )
527
 
528
  openrouter_submit_btn.click(
529
  fn=handle_openrouter_submit,
@@ -595,17 +627,14 @@ def create_demo():
595
 
596
  # Button actions
597
  def handle_together_submit(api_key, problem_text, image_path, history):
598
- html_solution, html_file, pdf_file, updated_history = generate_math_solution_together(
599
- api_key, problem_text, image_path, history
600
- )
601
-
602
- # Return outputs including file updates
603
- return (
604
- html_solution,
605
- updated_history,
606
- gr.update(value=html_file, visible=html_file is not None),
607
- gr.update(value=pdf_file, visible=pdf_file is not None)
608
- )
609
 
610
  together_submit_btn.click(
611
  fn=handle_together_submit,
@@ -643,7 +672,8 @@ def create_demo():
643
  ## How to Use the Advanced Math Tutor
644
 
645
  ### New Features πŸŽ‰
646
- - **HTML-formatted solutions**: All responses are now generated in beautiful HTML format
 
647
  - **Download HTML**: Download the complete solution as an HTML file
648
  - **Download PDF**: Convert and download solutions as PDF files
649
  - **Print functionality**: Use the "Print to PDF" button in the HTML output to print directly
@@ -665,7 +695,7 @@ def create_demo():
665
  ### Solving Math Problems
666
  - For text problems: Type or paste your math problem in the input field
667
  - For image problems: Upload a clear image of the math problem
668
- - Click "Solve Problem" to get a detailed step-by-step solution in HTML format
669
  - Use the download buttons to save HTML or PDF versions
670
  - Click "Print to PDF" within the solution to print directly from your browser
671
 
@@ -675,6 +705,7 @@ def create_demo():
675
  - **Step-by-step sections**: Clearly organized solution steps
676
  - **Print-friendly**: Optimized for printing and PDF conversion
677
  - **Timestamps**: Each solution includes generation timestamp
 
678
 
679
  ### Tips for Best Results
680
  - Be specific in your problem description
@@ -708,6 +739,7 @@ def create_demo():
708
  and Llama-Vision-Free via Together AI for image-based problems.
709
 
710
  **New Features:**
 
711
  - HTML-formatted responses with professional styling
712
  - Download solutions as HTML files
713
  - Convert and download solutions as PDF files
 
177
  .print-button:hover {{
178
  background-color: #0056b3;
179
  }}
180
+ .thinking {{
181
+ font-style: italic;
182
+ color: #007bff;
183
+ font-size: 1.2em;
184
+ text-align: center;
185
+ padding: 20px;
186
+ background-color: #e9ecef;
187
+ border-radius: 8px;
188
+ min-height: 100px;
189
+ }}
190
  @media print {{
191
  body {{
192
  background-color: white;
 
198
  .print-button {{
199
  display: none;
200
  }}
201
+ .thinking {{
202
+ display: none;
203
+ }}
204
  }}
205
  </style>
206
  <script>
 
225
  </div>
226
  ''' if include_problem and problem_text.strip() else ''}
227
 
228
+ <div class="solution-content" id="solution-content">
229
  <h2>πŸ” Solution</h2>
230
  {html_content}
231
  </div>
 
240
 
241
  return styled_html
242
 
243
+ # Function to generate thinking animation HTML
244
+ def thinking_animation_html():
245
+ return """
246
+ <div class="thinking" id="thinking-animation">
247
+ Thinking<span id="dots"></span>
248
+ <script>
249
+ let dots = 0;
250
+ const dotsElement = document.getElementById('dots');
251
+ const interval = setInterval(() => {
252
+ dots = (dots + 1) % 4;
253
+ dotsElement.textContent = '.'.repeat(dots);
254
+ }, 500);
255
+ </script>
256
+ </div>
257
+ """
258
+
259
  # Function to save HTML to file
260
  def save_html_to_file(html_content, filename_prefix="math_solution"):
261
  """Save HTML content to a temporary file and return the file path"""
 
328
  if history:
329
  for exchange in history:
330
  messages.append({"role": "user", "content": exchange[0]})
331
+ if len(exchange) > 1 and exchange[1]:
332
  messages.append({"role": "assistant", "content": exchange[1]})
333
 
334
  # Add the current problem
335
  messages.append({"role": "user", "content": f"Solve this math problem step-by-step: {problem_text}"})
336
 
337
+ # Show thinking animation
338
+ yield thinking_animation_html(), None, None, history
339
+
340
  # Create the completion
341
  completion = client.chat.completions.create(
342
  model="microsoft/phi-4-reasoning-plus:free",
 
363
  history = []
364
  history.append((problem_text, markdown_solution))
365
 
366
+ yield html_solution, html_file_path, pdf_file_path, history
367
 
368
  except Exception as e:
369
  error_message = f"Error: {str(e)}"
370
+ yield error_message, None, None, history
371
 
372
  # Enhanced function to generate math solution using Together AI with HTML output
373
  def generate_math_solution_together(api_key, problem_text, image_path=None, history=None):
 
408
  if history:
409
  for exchange in history:
410
  messages.append({"role": "user", "content": exchange[0]})
411
+ if len(exchange) > 1 and exchange[1]:
412
  messages.append({"role": "assistant", "content": exchange[1]})
413
 
414
  # Prepare the user message content
 
444
  "content": user_message_content
445
  })
446
 
447
+ # Show thinking animation
448
+ yield thinking_animation_html(), None, None, history
449
+
450
  # Create the completion
451
  response = client.chat.completions.create(
452
  model="meta-llama/Llama-Vision-Free",
 
466
  # Convert to PDF
467
  pdf_file_path = html_to_pdf(html_solution, "together_solution")
468
 
469
+ # Update history
470
  if history is None:
471
  history = []
472
  history.append((problem_display, markdown_solution))
473
 
474
+ yield html_solution, html_file_path, pdf_file_path, history
475
 
476
  except Exception as e:
477
  error_message = f"Error: {str(e)}"
478
+ yield error_message, None, None, history
479
 
480
  # Function to convert image to base64
481
  def image_to_base64(image_path):
 
497
  This application provides step-by-step solutions to math problems using advanced AI models.
498
  Solutions are generated in **HTML format** with download and print-to-PDF capabilities.
499
  Choose between OpenRouter's Phi-4-reasoning-plus for text-based problems or Together AI's
500
+ Llama-Vision for problems with images. A thinking animation is shown while the solution is being generated.
501
  """)
502
 
503
  # Main tabs
 
548
 
549
  # Button actions
550
  def handle_openrouter_submit(api_key, problem_text, history):
551
+ for output in generate_math_solution_openrouter(api_key, problem_text, history):
552
+ html_solution, html_file, pdf_file, updated_history = output
553
+ yield (
554
+ html_solution,
555
+ updated_history,
556
+ gr.update(value=html_file, visible=html_file is not None),
557
+ gr.update(value=pdf_file, visible=pdf_file is not None)
558
+ )
 
 
 
559
 
560
  openrouter_submit_btn.click(
561
  fn=handle_openrouter_submit,
 
627
 
628
  # Button actions
629
  def handle_together_submit(api_key, problem_text, image_path, history):
630
+ for output in generate_math_solution_together(api_key, problem_text, image_path, history):
631
+ html_solution, html_file, pdf_file, updated_history = output
632
+ yield (
633
+ html_solution,
634
+ updated_history,
635
+ gr.update(value=html_file, visible=html_file is not None),
636
+ gr.update(value=pdf_file, visible=pdf_file is not None)
637
+ )
 
 
 
638
 
639
  together_submit_btn.click(
640
  fn=handle_together_submit,
 
672
  ## How to Use the Advanced Math Tutor
673
 
674
  ### New Features πŸŽ‰
675
+ - **Thinking Animation**: Displays a dynamic "Thinking..." animation while the solution is being generated
676
+ - **HTML-formatted solutions**: All responses are generated in beautiful HTML format
677
  - **Download HTML**: Download the complete solution as an HTML file
678
  - **Download PDF**: Convert and download solutions as PDF files
679
  - **Print functionality**: Use the "Print to PDF" button in the HTML output to print directly
 
695
  ### Solving Math Problems
696
  - For text problems: Type or paste your math problem in the input field
697
  - For image problems: Upload a clear image of the math problem
698
+ - Click "Solve Problem" to see a thinking animation followed by a detailed step-by-step solution in HTML format
699
  - Use the download buttons to save HTML or PDF versions
700
  - Click "Print to PDF" within the solution to print directly from your browser
701
 
 
705
  - **Step-by-step sections**: Clearly organized solution steps
706
  - **Print-friendly**: Optimized for printing and PDF conversion
707
  - **Timestamps**: Each solution includes generation timestamp
708
+ - **Thinking Animation**: Dynamic dots animation while processing
709
 
710
  ### Tips for Best Results
711
  - Be specific in your problem description
 
739
  and Llama-Vision-Free via Together AI for image-based problems.
740
 
741
  **New Features:**
742
+ - Thinking animation during solution generation
743
  - HTML-formatted responses with professional styling
744
  - Download solutions as HTML files
745
  - Convert and download solutions as PDF files