FractalAIR commited on
Commit
b5b8744
·
verified ·
1 Parent(s): 160870f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -6,16 +6,24 @@ from threading import Thread
6
  import re
7
  import uuid
8
  from openai import OpenAI
 
 
 
 
9
 
10
  client = OpenAI(
11
  base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
12
  api_key="hf_XXXXX"
13
  )
14
 
15
- def format_math(text):
16
- text = re.sub(r"\[(.*?)\]", r"$$\1$$", text, flags=re.DOTALL)
17
- text = text.replace(r"\(", "$").replace(r"\)", "$")
18
- return text
 
 
 
 
19
 
20
  # Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
21
  conversations = {}
@@ -155,6 +163,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
155
  with gr.Column():
156
  submit_button = gr.Button("Send", variant="primary", scale=1)
157
  clear_button = gr.Button("Clear", scale=1)
 
 
 
 
 
 
 
158
  gr.Markdown("**Try these examples:**")
159
  with gr.Row():
160
  example1_button = gr.Button("JEE Main 2025\nCombinatorics")
@@ -162,6 +177,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
162
  example3_button = gr.Button("JEE Main 2025\nProbability & Statistics")
163
  example4_button = gr.Button("JEE Main 2025\nLaws of Motion")
164
 
 
 
165
  def update_conversation_list():
166
  return [conversations[cid]["title"] for cid in conversations]
167
 
@@ -221,6 +238,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
221
  example3_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Probability & Statistics"]), inputs=None, outputs=user_input)
222
  example4_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Laws of Motion"]), inputs=None, outputs=user_input)
223
 
 
 
 
 
 
 
224
  #demo.launch(share=True, ssr_mode=False)
225
 
226
  if __name__ == "__main__":
 
6
  import re
7
  import uuid
8
  from openai import OpenAI
9
+ from PIL import Image
10
+ from pix2tex import cli as latex_ocr
11
+ # Initialize LaTeX OCR model
12
+ latex_model = latex_ocr.LatexOCR()
13
 
14
  client = OpenAI(
15
  base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
16
  api_key="hf_XXXXX"
17
  )
18
 
19
+ def process_image(image_file):
20
+ try:
21
+ img = Image.open(image_file)
22
+ latex = latex_model(img)
23
+ return format_math(latex)
24
+ except Exception as e:
25
+ print(f"Image processing error: {e}")
26
+ return "⚠️ Could not process image. Please type your question manually."
27
 
28
  # Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
29
  conversations = {}
 
163
  with gr.Column():
164
  submit_button = gr.Button("Send", variant="primary", scale=1)
165
  clear_button = gr.Button("Clear", scale=1)
166
+ image_upload = gr.UploadButton(
167
+ "📷 Upload Math Image",
168
+ file_types=["image"],
169
+ file_count="single"
170
+ )
171
+
172
+
173
  gr.Markdown("**Try these examples:**")
174
  with gr.Row():
175
  example1_button = gr.Button("JEE Main 2025\nCombinatorics")
 
177
  example3_button = gr.Button("JEE Main 2025\nProbability & Statistics")
178
  example4_button = gr.Button("JEE Main 2025\nLaws of Motion")
179
 
180
+
181
+
182
  def update_conversation_list():
183
  return [conversations[cid]["title"] for cid in conversations]
184
 
 
238
  example3_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Probability & Statistics"]), inputs=None, outputs=user_input)
239
  example4_button.click(fn=lambda: gr.update(value=example_messages["JEE Main 2025 Laws of Motion"]), inputs=None, outputs=user_input)
240
 
241
+ image_upload.upload(
242
+ fn=process_image,
243
+ inputs=image_upload,
244
+ outputs=user_input
245
+ )
246
+
247
  #demo.launch(share=True, ssr_mode=False)
248
 
249
  if __name__ == "__main__":