Update app.py
Browse files
app.py
CHANGED
@@ -16,26 +16,14 @@ client = OpenAI(
|
|
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 |
-
|
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 = {}
|
@@ -109,28 +97,6 @@ def generate_response(user_message,
|
|
109 |
|
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 |
-
|
117 |
-
buffered = BytesIO()
|
118 |
-
image.save(buffered, format="PNG")
|
119 |
-
buffered.seek(0)
|
120 |
-
|
121 |
-
api_url = "https://api-inference.huggingface.co/models/harishkannaram/latex-ocr"
|
122 |
-
hf_token = os.getenv(HF_API)
|
123 |
-
headers = {"Authorization": f"Bearer {hf_token}"}
|
124 |
-
|
125 |
-
response = requests.post(api_url, headers=headers, files={"inputs": buffered})
|
126 |
-
|
127 |
-
if response.status_code == 200:
|
128 |
-
text = response.json().get("generated_text", "")
|
129 |
-
return gr.update(value=text)
|
130 |
-
else:
|
131 |
-
return gr.update(value="⚠️ OCR failed. Please try again.")
|
132 |
-
'''
|
133 |
-
|
134 |
|
135 |
example_messages = {
|
136 |
"JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
|
@@ -198,17 +164,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
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:**")
|
214 |
with gr.Row():
|
|
|
16 |
api_key="hf_XXXXX"
|
17 |
)
|
18 |
|
|
|
19 |
from PIL import Image
|
20 |
+
from pix2tex import cli as pix2tex
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def image_to_latex(image_path):
|
23 |
+
model = pix2tex.LatexOCR()
|
24 |
image = Image.open(image_path).convert("RGB")
|
25 |
+
latex_code = model(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
return latex_code
|
|
|
27 |
|
28 |
# Global dictionary to store all conversations: {id: {"title": str, "messages": list}}
|
29 |
conversations = {}
|
|
|
97 |
|
98 |
yield new_history, new_history
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
example_messages = {
|
102 |
"JEE Main 2025 Combinatorics": "From all the English alphabets, five letters are chosen and are arranged in alphabetical order. The total number of ways, in which the middle letter is 'M', is?",
|
|
|
164 |
with gr.Column():
|
165 |
submit_button = gr.Button("Send", variant="primary", scale=1)
|
166 |
clear_button = gr.Button("Clear", scale=1)
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
with gr.Row():
|
169 |
image_input = gr.Image(type="filepath", label="Upload Image")
|
170 |
output_text = gr.Textbox(label="LaTeX Output")
|
171 |
image_input.change(fn=process_image, inputs=image_input, outputs=output_text)
|
172 |
+
|
173 |
|
174 |
gr.Markdown("**Try these examples:**")
|
175 |
with gr.Row():
|