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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -6,6 +6,9 @@ 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/",
@@ -35,7 +38,7 @@ def generate_response(user_message,
35
  if not user_message.strip():
36
  return history_state, history_state
37
 
38
- system_message = "Your role as an assistant..."
39
  messages = [{"role": "system", "content": system_message}]
40
  for m in history_state:
41
  messages.append({"role": m["role"], "content": m["content"]})
@@ -89,6 +92,28 @@ def generate_response(user_message,
89
 
90
  yield new_history, new_history
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  example_messages = {
94
  "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?",
@@ -155,6 +180,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")
 
6
  import re
7
  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/",
 
38
  if not user_message.strip():
39
  return history_state, history_state
40
 
41
+ system_message = "You are Ramanujan-Ganit, an intelligent AI assistant developed by Fractal AI Research. You are an expert at solving complex reasoning problems, specialising in the domains of math and science."
42
  messages = [{"role": "system", "content": system_message}]
43
  for m in history_state:
44
  messages.append({"role": m["role"], "content": m["content"]})
 
92
 
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
+
100
+ buffered = BytesIO()
101
+ image.save(buffered, format="PNG")
102
+ buffered.seek(0)
103
+
104
+ api_url = "https://api-inference.huggingface.co/models/harishkannaram/latex-ocr"
105
+ hf_token = os.getenv(HF_API)
106
+ headers = {"Authorization": f"Bearer {hf_token}"}
107
+
108
+ response = requests.post(api_url, headers=headers, files={"inputs": buffered})
109
+
110
+ if response.status_code == 200:
111
+ text = response.json().get("generated_text", "")
112
+ return gr.update(value=text)
113
+ else:
114
+ return gr.update(value="⚠️ OCR failed. Please try again.")
115
+
116
+
117
 
118
  example_messages = {
119
  "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?",
 
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:**")
191
  with gr.Row():
192
  example1_button = gr.Button("JEE Main 2025\nCombinatorics")