Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
42 |
-
if
|
43 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
147 |
|
148 |
Instructions:
|
149 |
-
- Extract each
|
150 |
-
- Use proper LaTeX syntax for
|
|
|
|
|
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 |
-
|
160 |
-
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
250 |
-
with intelligent filename suggestions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
**Features:**
|
253 |
-
- Extracts
|
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
|
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
|
297 |
- Multiple equations in one image are supported
|
298 |
-
-
|
|
|
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 |
)
|