Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,50 @@
|
|
1 |
import sympy as sp
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
|
7 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def process_math(query):
|
9 |
try:
|
10 |
-
#
|
11 |
-
query = query.replace("
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
expr = sp.sympify(query)
|
15 |
|
16 |
-
#
|
17 |
result = expr.evalf()
|
18 |
|
19 |
# Convert the result to speech and save as mp3
|
@@ -24,7 +55,9 @@ def process_math(query):
|
|
24 |
|
25 |
return result_text
|
26 |
except Exception as e:
|
27 |
-
|
|
|
|
|
28 |
|
29 |
# Function to handle voice input (speech-to-text)
|
30 |
def voice_to_text(audio):
|
|
|
1 |
import sympy as sp
|
2 |
import gradio as gr
|
3 |
+
import re
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
|
7 |
+
# Function to intelligently detect and fix input errors using simple AI-based rules
|
8 |
+
def intelligent_error_correction(query):
|
9 |
+
"""
|
10 |
+
A simple AI-based function to suggest corrections for common math expression mistakes.
|
11 |
+
"""
|
12 |
+
suggestions = []
|
13 |
+
|
14 |
+
# Detect missing parentheses for functions like factorial, permutation, etc.
|
15 |
+
if "factorial" in query and "(" not in query:
|
16 |
+
suggestions.append("Did you mean factorial(x)? Please make sure to enclose the number inside parentheses like factorial(5).")
|
17 |
+
if "permutation" in query and "(" not in query:
|
18 |
+
suggestions.append("Did you mean permutation(n, k)? Ensure both numbers are inside parentheses like permutation(5, 3).")
|
19 |
+
if "combinations" in query and "(" not in query:
|
20 |
+
suggestions.append("Did you mean combinations(n, k)? Ensure both numbers are inside parentheses like combinations(5, 3).")
|
21 |
+
|
22 |
+
# Check for spaces between function name and parentheses
|
23 |
+
if re.search(r"\w\s*\(", query):
|
24 |
+
suggestions.append("Make sure there are no spaces between the function name and parentheses (e.g., factorial(5) not factorial (5)).")
|
25 |
+
|
26 |
+
# Handle common syntax issues like missing operators or numbers
|
27 |
+
if re.search(r"[\+\-\*\/]\s*$", query): # Ends with an operator without a number after
|
28 |
+
suggestions.append("It seems you have an operator at the end of the expression, but no number to apply it to. Please fix it.")
|
29 |
+
|
30 |
+
if not suggestions:
|
31 |
+
suggestions.append("Your expression looks good, but if you're unsure, check for spaces or parentheses.")
|
32 |
+
|
33 |
+
return suggestions
|
34 |
+
|
35 |
+
# Function to process math queries, with AI to guide error resolution
|
36 |
def process_math(query):
|
37 |
try:
|
38 |
+
# Clean up the input by removing unnecessary spaces
|
39 |
+
query = query.replace(" ", "") # Removing extra spaces around operators or parentheses
|
40 |
+
|
41 |
+
# Use intelligent error correction
|
42 |
+
error_suggestions = intelligent_error_correction(query)
|
43 |
|
44 |
+
# Try to process the query using sympy
|
45 |
expr = sp.sympify(query)
|
46 |
|
47 |
+
# If the expression is valid, return the result
|
48 |
result = expr.evalf()
|
49 |
|
50 |
# Convert the result to speech and save as mp3
|
|
|
55 |
|
56 |
return result_text
|
57 |
except Exception as e:
|
58 |
+
# If there is an error, suggest corrections
|
59 |
+
suggestions = "\n".join(intelligent_error_correction(query))
|
60 |
+
return f"Error: Unable to process the query. Details: {e}. Suggestions to fix:\n{suggestions}"
|
61 |
|
62 |
# Function to handle voice input (speech-to-text)
|
63 |
def voice_to_text(audio):
|