FractalAIR commited on
Commit
ef5ddd1
·
verified ·
1 Parent(s): d2c905d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -8,17 +8,34 @@ import uuid
8
  from openai import OpenAI
9
  import requests
10
  from io import BytesIO
11
- from PIL import Image
 
12
 
13
  client = OpenAI(
14
  base_url="https://a7g1ajqixo23revq.us-east-1.aws.endpoints.huggingface.cloud/v1/",
15
  api_key="hf_XXXXX"
16
  )
17
 
18
- def format_math(text):
19
- text = re.sub(r"\[(.*?)\]", r"$$\1$$", text, flags=re.DOTALL)
20
- text = text.replace(r"\(", "$").replace(r"\)", "$")
21
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
24
  conversations = {}
@@ -93,7 +110,7 @@ def generate_response(user_message,
93
  yield new_history, new_history
94
 
95
  # OCR function using HF hosted model
96
- def extract_latex_from_image(image):
97
  if image is None:
98
  return gr.update(value="")
99
 
@@ -112,7 +129,7 @@ def extract_latex_from_image(image):
112
  return gr.update(value=text)
113
  else:
114
  return gr.update(value="⚠️ OCR failed. Please try again.")
115
-
116
 
117
 
118
  example_messages = {
@@ -175,16 +192,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
175
  with gr.Column(scale=4):
176
  #chatbot = gr.Chatbot(label="Chat", type="messages")
177
  chatbot = gr.Chatbot(label="Chat", type="messages", height=520)
 
178
  with gr.Row():
179
  user_input = gr.Textbox(label="User Input", placeholder="Type your question here...", lines=3, scale=8)
180
  with gr.Column():
181
  submit_button = gr.Button("Send", variant="primary", scale=1)
182
  clear_button = gr.Button("Clear", scale=1)
183
 
184
- gr.Markdown("### Upload Image of a Math Question")
185
  image_input = gr.Image(type="pil", label="Upload Image (PNG/JPG)", height=120)
186
  ocr_button = gr.Button("Extract LaTeX from Image 🧠")
187
- ocr_button.click(fn=extract_latex_from_image, inputs=image_input, outputs=user_input)
 
 
 
 
 
188
 
189
 
190
  gr.Markdown("**Try these examples:**")
 
8
  from openai import OpenAI
9
  import requests
10
  from io import BytesIO
11
+ import os
12
+ #from PIL import Image
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
+ from transformers import VisionEncoderDecoderModel, TrOCRProcessor
20
+ from PIL import Image
21
+
22
+ # Load the model and processor
23
+ processor = TrOCRProcessor.from_pretrained("breezedeus/pix2text-mfr")
24
+ model = VisionEncoderDecoderModel.from_pretrained("breezedeus/pix2text-mfr")
25
+ model.eval()
26
+
27
+
28
+ def image_to_latex(image_path):
29
+ image = Image.open(image_path).convert("RGB")
30
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
31
+ with torch.no_grad():
32
+ generated_ids = model.generate(pixel_values)
33
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
34
+ return generated_text
35
+ def process_image(image):
36
+ latex_code = image_to_latex(image)
37
+ return latex_code
38
+
39
 
40
  # Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
41
  conversations = {}
 
110
  yield new_history, new_history
111
 
112
  # OCR function using HF hosted model
113
+ '''def extract_latex_from_image(image):
114
  if image is None:
115
  return gr.update(value="")
116
 
 
129
  return gr.update(value=text)
130
  else:
131
  return gr.update(value="⚠️ OCR failed. Please try again.")
132
+ '''
133
 
134
 
135
  example_messages = {
 
192
  with gr.Column(scale=4):
193
  #chatbot = gr.Chatbot(label="Chat", type="messages")
194
  chatbot = gr.Chatbot(label="Chat", type="messages", height=520)
195
+
196
  with gr.Row():
197
  user_input = gr.Textbox(label="User Input", placeholder="Type your question here...", lines=3, scale=8)
198
  with gr.Column():
199
  submit_button = gr.Button("Send", variant="primary", scale=1)
200
  clear_button = gr.Button("Clear", scale=1)
201
 
202
+ '''gr.Markdown("### Upload Image of a Math Question")
203
  image_input = gr.Image(type="pil", label="Upload Image (PNG/JPG)", height=120)
204
  ocr_button = gr.Button("Extract LaTeX from Image 🧠")
205
+ ocr_button.click(fn=extract_latex_from_image, inputs=image_input, outputs=user_input)'''
206
+
207
+ with gr.Row():
208
+ image_input = gr.Image(type="filepath", label="Upload Image")
209
+ output_text = gr.Textbox(label="LaTeX Output")
210
+ image_input.change(fn=process_image, inputs=image_input, outputs=output_text)
211
 
212
 
213
  gr.Markdown("**Try these examples:**")