Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import base64
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from io import BytesIO
|
| 8 |
|
|
@@ -21,6 +22,7 @@ Rules you ALWAYS follow:
|
|
| 21 |
6. For any calculation, write out every arithmetic operation explicitly before evaluating it. Never skip steps or compute mentally.
|
| 22 |
7. For trigonometry, always state the formula, substitute values, then compute. Show sin/cos/tan values to 4 decimal places.
|
| 23 |
8. Never use LaTeX notation. Use plain text only. Write fractions as a/b, exponents as x^2, and put each equation on its own line.
|
|
|
|
| 24 |
Always format your response exactly like this:
|
| 25 |
📘 **Problem:** [restate the problem clearly]
|
| 26 |
---
|
|
@@ -38,6 +40,25 @@ Always format your response exactly like this:
|
|
| 38 |
💡 [One encouraging sentence]"""
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def image_to_base64(pil_image):
|
| 42 |
buffered = BytesIO()
|
| 43 |
pil_image.save(buffered, format="PNG")
|
|
@@ -70,7 +91,6 @@ def solve_math(image_input, text_input, grade_level, user_key):
|
|
| 70 |
|
| 71 |
try:
|
| 72 |
if has_image:
|
| 73 |
-
# Use vision model to read the image directly
|
| 74 |
img_b64 = image_to_base64(image_input)
|
| 75 |
text_note = f"\nStudent's additional note: {text_input.strip()}" if has_text else ""
|
| 76 |
user_content = [
|
|
@@ -133,7 +153,7 @@ def solve_math(image_input, text_input, grade_level, user_key):
|
|
| 133 |
token = json.loads(line)["choices"][0]["delta"].get("content", "")
|
| 134 |
if token:
|
| 135 |
output += token
|
| 136 |
-
yield output
|
| 137 |
except Exception:
|
| 138 |
continue
|
| 139 |
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import base64
|
| 6 |
+
import re
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
|
|
|
|
| 22 |
6. For any calculation, write out every arithmetic operation explicitly before evaluating it. Never skip steps or compute mentally.
|
| 23 |
7. For trigonometry, always state the formula, substitute values, then compute. Show sin/cos/tan values to 4 decimal places.
|
| 24 |
8. Never use LaTeX notation. Use plain text only. Write fractions as a/b, exponents as x^2, and put each equation on its own line.
|
| 25 |
+
|
| 26 |
Always format your response exactly like this:
|
| 27 |
📘 **Problem:** [restate the problem clearly]
|
| 28 |
---
|
|
|
|
| 40 |
💡 [One encouraging sentence]"""
|
| 41 |
|
| 42 |
|
| 43 |
+
def strip_latex(text):
|
| 44 |
+
text = re.sub(r'\$\$(.+?)\$\$', r'\1', text, flags=re.DOTALL)
|
| 45 |
+
text = re.sub(r'\$(.+?)\$', r'\1', text)
|
| 46 |
+
text = re.sub(r'\\frac\{(.+?)\}\{(.+?)\}', r'\1/\2', text)
|
| 47 |
+
text = re.sub(r'\\text\{(.+?)\}', r'\1', text)
|
| 48 |
+
text = text.replace(r'\cos', 'cos')
|
| 49 |
+
text = text.replace(r'\sin', 'sin')
|
| 50 |
+
text = text.replace(r'\tan', 'tan')
|
| 51 |
+
text = text.replace(r'\times', '×')
|
| 52 |
+
text = text.replace(r'\div', '÷')
|
| 53 |
+
text = text.replace(r'\approx', '≈')
|
| 54 |
+
text = text.replace(r'\cdot', '·')
|
| 55 |
+
text = text.replace(r'\left', '').replace(r'\right', '')
|
| 56 |
+
text = text.replace(r'\sqrt', 'sqrt')
|
| 57 |
+
text = text.replace(r'\pi', 'π')
|
| 58 |
+
text = re.sub(r'\{|\}', '', text)
|
| 59 |
+
return text
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def image_to_base64(pil_image):
|
| 63 |
buffered = BytesIO()
|
| 64 |
pil_image.save(buffered, format="PNG")
|
|
|
|
| 91 |
|
| 92 |
try:
|
| 93 |
if has_image:
|
|
|
|
| 94 |
img_b64 = image_to_base64(image_input)
|
| 95 |
text_note = f"\nStudent's additional note: {text_input.strip()}" if has_text else ""
|
| 96 |
user_content = [
|
|
|
|
| 153 |
token = json.loads(line)["choices"][0]["delta"].get("content", "")
|
| 154 |
if token:
|
| 155 |
output += token
|
| 156 |
+
yield strip_latex(output)
|
| 157 |
except Exception:
|
| 158 |
continue
|
| 159 |
|