Update app.py
Browse files
app.py
CHANGED
|
@@ -37,7 +37,6 @@ Always format your response exactly like this:
|
|
| 37 |
|
| 38 |
|
| 39 |
def ocr_image(pil_image):
|
| 40 |
-
"""Extract text from a worksheet image using Tesseract OCR."""
|
| 41 |
try:
|
| 42 |
w, h = pil_image.size
|
| 43 |
scale = max(1, 2000 // max(w, h))
|
|
@@ -45,10 +44,7 @@ def ocr_image(pil_image):
|
|
| 45 |
img = img.convert("L")
|
| 46 |
img = ImageEnhance.Contrast(img).enhance(2.0)
|
| 47 |
img = img.filter(ImageFilter.SHARPEN)
|
| 48 |
-
text = pytesseract.image_to_string(
|
| 49 |
-
img,
|
| 50 |
-
config="--psm 6 --oem 3"
|
| 51 |
-
).strip()
|
| 52 |
return text if len(text) > 10 else None
|
| 53 |
except Exception:
|
| 54 |
return None
|
|
@@ -68,7 +64,7 @@ def solve_math(image_input, text_input, grade_level, user_key):
|
|
| 68 |
return
|
| 69 |
|
| 70 |
has_image = image_input is not None
|
| 71 |
-
has_text
|
| 72 |
|
| 73 |
if not has_image and not has_text:
|
| 74 |
yield "⚠️ Please upload a worksheet image **or** type a math problem."
|
|
@@ -86,8 +82,7 @@ def solve_math(image_input, text_input, grade_level, user_key):
|
|
| 86 |
if not has_text:
|
| 87 |
yield (
|
| 88 |
"⚠️ **Could not read text from the image clearly.**\n\n"
|
| 89 |
-
"Please type the problem in the text box below and try again.
|
| 90 |
-
"Make sure the image is well-lit and the text is clear."
|
| 91 |
)
|
| 92 |
return
|
| 93 |
yield "⚡ Solving step by step..."
|
|
@@ -115,7 +110,7 @@ def solve_math(image_input, text_input, grade_level, user_key):
|
|
| 115 |
"model": MODEL,
|
| 116 |
"messages": [
|
| 117 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 118 |
-
{"role": "user",
|
| 119 |
],
|
| 120 |
"max_tokens": 1200,
|
| 121 |
"temperature": 0,
|
|
@@ -279,4 +274,4 @@ with gr.Blocks(css=CSS, title="Interactive Math Tutor") as demo:
|
|
| 279 |
)
|
| 280 |
|
| 281 |
if __name__ == "__main__":
|
| 282 |
-
demo.queue().launch(
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def ocr_image(pil_image):
|
|
|
|
| 40 |
try:
|
| 41 |
w, h = pil_image.size
|
| 42 |
scale = max(1, 2000 // max(w, h))
|
|
|
|
| 44 |
img = img.convert("L")
|
| 45 |
img = ImageEnhance.Contrast(img).enhance(2.0)
|
| 46 |
img = img.filter(ImageFilter.SHARPEN)
|
| 47 |
+
text = pytesseract.image_to_string(img, config="--psm 6 --oem 3").strip()
|
|
|
|
|
|
|
|
|
|
| 48 |
return text if len(text) > 10 else None
|
| 49 |
except Exception:
|
| 50 |
return None
|
|
|
|
| 64 |
return
|
| 65 |
|
| 66 |
has_image = image_input is not None
|
| 67 |
+
has_text = text_input and text_input.strip()
|
| 68 |
|
| 69 |
if not has_image and not has_text:
|
| 70 |
yield "⚠️ Please upload a worksheet image **or** type a math problem."
|
|
|
|
| 82 |
if not has_text:
|
| 83 |
yield (
|
| 84 |
"⚠️ **Could not read text from the image clearly.**\n\n"
|
| 85 |
+
"Please type the problem in the text box below and try again."
|
|
|
|
| 86 |
)
|
| 87 |
return
|
| 88 |
yield "⚡ Solving step by step..."
|
|
|
|
| 110 |
"model": MODEL,
|
| 111 |
"messages": [
|
| 112 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 113 |
+
{"role": "user", "content": user_msg},
|
| 114 |
],
|
| 115 |
"max_tokens": 1200,
|
| 116 |
"temperature": 0,
|
|
|
|
| 274 |
)
|
| 275 |
|
| 276 |
if __name__ == "__main__":
|
| 277 |
+
demo.queue().launch()
|