AkshitShubham commited on
Commit
3d6bf61
·
verified ·
1 Parent(s): 9c0fd54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -38,9 +38,18 @@ def extract_equations_and_generate_commands(api_response):
38
  if not line:
39
  continue
40
 
41
- # Check if line contains LaTeX math
42
- if '\\' in line and not line.startswith('#'):
43
- # Extract LaTeX content
 
 
 
 
 
 
 
 
 
44
  latex_content = line
45
 
46
  # Clean up common markdown artifacts
@@ -143,21 +152,25 @@ def image_to_latex(image):
143
  {
144
  "parts": [
145
  {
146
- "text": """Please analyze this image and convert any mathematical equations, formulas, or mathematical text you find into proper LaTeX format.
147
 
148
  Instructions:
149
- - Extract each mathematical equation or formula separately
150
- - Use proper LaTeX syntax for mathematical expressions
 
 
151
  - Output raw LaTeX without any $ delimiters
152
  - Put each equation on a separate line
153
  - Do not include any markdown formatting or explanatory text
154
  - Only output the raw LaTeX code for each equation
155
  - If there are multiple equations, list them one per line
 
156
 
157
  Example output format:
158
  \\frac{a}{b} = c
159
- x^2 + y^2 = z^2
160
- \\int_0^1 x dx = \\frac{1}{2}"""
 
161
  },
162
  {
163
  "inline_data": {
@@ -184,7 +197,17 @@ x^2 + y^2 = z^2
184
  equations = []
185
  for line in latex_output.split('\n'):
186
  line = line.strip()
187
- if line and '\\' in line:
 
 
 
 
 
 
 
 
 
 
188
  equations.append(line)
189
 
190
  if not equations:
@@ -246,21 +269,23 @@ def create_app():
246
  """
247
  # 📸 Image to maths2svg Command Generator
248
 
249
- Upload an image containing mathematical equations and get ready-to-use `maths2svg` commands
250
- with intelligent filename suggestions.
 
 
 
 
 
 
 
251
 
252
  **Features:**
253
- - Extracts multiple equations from images
254
  - Generates proper LaTeX syntax
255
  - Creates maths2svg CLI commands
256
  - Suggests meaningful filenames using AI
257
  - Falls back to serial numbering if needed
258
 
259
- **Supported content:**
260
- - Mathematical equations and formulas
261
- - Mathematical symbols and expressions
262
- - Multiple equations in one image
263
-
264
  **Note:** Make sure to set your `GEMINI_API_KEY` environment variable before running this app.
265
  """
266
  )
@@ -268,7 +293,7 @@ def create_app():
268
  with gr.Row():
269
  with gr.Column(scale=1):
270
  image_input = gr.Image(
271
- label="Upload Image with Math Equations",
272
  type="pil",
273
  height=400
274
  )
@@ -293,9 +318,10 @@ def create_app():
293
  gr.Markdown(
294
  """
295
  - Use clear, high-contrast images
296
- - Ensure mathematical expressions are clearly visible
297
  - Multiple equations in one image are supported
298
- - Handwritten or printed equations both work
 
299
  - The app will generate meaningful filenames automatically
300
  """
301
  )
 
38
  if not line:
39
  continue
40
 
41
+ # Check if line contains any equation-like content
42
+ if line and (
43
+ '\\' in line or # LaTeX commands
44
+ '=' in line or # Equations
45
+ '+' in line or # Mathematical operations
46
+ '->' in line or # Arrows
47
+ '→' in line or # Unicode arrow
48
+ '_' in line or # Subscripts
49
+ '^' in line or # Superscripts
50
+ any(char.isdigit() for char in line) # Contains numbers
51
+ ) and not line.startswith('#'):
52
+ # Extract content
53
  latex_content = line
54
 
55
  # Clean up common markdown artifacts
 
152
  {
153
  "parts": [
154
  {
155
+ "text": """Please analyze this image and convert any equations, formulas, or expressions you find into proper LaTeX format. This includes mathematical equations, chemical equations, physics formulas, or any other scientific notation.
156
 
157
  Instructions:
158
+ - Extract each equation or formula separately
159
+ - Use proper LaTeX syntax for all expressions
160
+ - For chemical equations: use subscripts {}, superscripts {}, and \\rightarrow for arrows
161
+ - For mathematical equations: use proper LaTeX math notation
162
  - Output raw LaTeX without any $ delimiters
163
  - Put each equation on a separate line
164
  - Do not include any markdown formatting or explanatory text
165
  - Only output the raw LaTeX code for each equation
166
  - If there are multiple equations, list them one per line
167
+ - Include ALL text content that looks like equations or formulas, even if handwritten
168
 
169
  Example output format:
170
  \\frac{a}{b} = c
171
+ C_6H_{12}O_6 + 6O_2 \\rightarrow 6CO_2 + 6H_2O
172
+ F = ma
173
+ x^2 + y^2 = z^2"""
174
  },
175
  {
176
  "inline_data": {
 
197
  equations = []
198
  for line in latex_output.split('\n'):
199
  line = line.strip()
200
+ # More flexible detection - look for common equation patterns
201
+ if line and (
202
+ '\\' in line or # LaTeX commands
203
+ '=' in line or # Equations
204
+ '+' in line or # Mathematical operations
205
+ '->' in line or # Arrows (could be chemical)
206
+ '→' in line or # Unicode arrow
207
+ '_' in line or # Subscripts
208
+ '^' in line or # Superscripts
209
+ any(char.isdigit() for char in line) # Contains numbers
210
+ ):
211
  equations.append(line)
212
 
213
  if not equations:
 
269
  """
270
  # 📸 Image to maths2svg Command Generator
271
 
272
+ Upload an image containing mathematical equations, chemical equations, physics formulas,
273
+ or any scientific expressions and get ready-to-use `maths2svg` commands with intelligent filename suggestions.
274
+
275
+ **Supported content:**
276
+ - Mathematical equations and formulas
277
+ - Chemical equations and reactions
278
+ - Physics formulas and expressions
279
+ - Handwritten or printed equations
280
+ - Multiple equations in one image
281
 
282
  **Features:**
283
+ - Extracts equations from images (including handwritten)
284
  - Generates proper LaTeX syntax
285
  - Creates maths2svg CLI commands
286
  - Suggests meaningful filenames using AI
287
  - Falls back to serial numbering if needed
288
 
 
 
 
 
 
289
  **Note:** Make sure to set your `GEMINI_API_KEY` environment variable before running this app.
290
  """
291
  )
 
293
  with gr.Row():
294
  with gr.Column(scale=1):
295
  image_input = gr.Image(
296
+ label="Upload Image with Equations/Formulas",
297
  type="pil",
298
  height=400
299
  )
 
318
  gr.Markdown(
319
  """
320
  - Use clear, high-contrast images
321
+ - Ensure equations are clearly visible and well-lit
322
  - Multiple equations in one image are supported
323
+ - Works with handwritten, printed, or digital equations
324
+ - Supports mathematical equations, chemical formulas, physics expressions
325
  - The app will generate meaningful filenames automatically
326
  """
327
  )