Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,111 @@
|
|
1 |
-
import
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
-
import mathparse # Using mathparse for text-to-math conversion
|
6 |
|
7 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def process_math(query):
|
9 |
try:
|
10 |
# Clean up the input by removing unnecessary spaces
|
11 |
-
query = query.replace(" ", "") # Removing extra spaces
|
12 |
|
13 |
# Try to process the query using sympy
|
14 |
-
|
15 |
-
|
|
|
16 |
# Convert the result to speech and save as mp3
|
17 |
result_text = f"The result is: {result}"
|
18 |
tts = gTTS(result_text, lang="en")
|
19 |
tts.save("response.mp3")
|
20 |
|
21 |
-
# Return the result text
|
22 |
return result_text, "response.mp3"
|
23 |
except Exception as e:
|
24 |
return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
|
25 |
|
26 |
-
#
|
27 |
-
def voice_to_text(audio):
|
28 |
-
recognizer = sr.Recognizer()
|
29 |
-
try:
|
30 |
-
with sr.AudioFile(audio) as source:
|
31 |
-
audio_data = recognizer.record(source)
|
32 |
-
text = recognizer.recognize_google(audio_data)
|
33 |
-
return text
|
34 |
-
except Exception as e:
|
35 |
-
return f"Error in voice recognition: {e}"
|
36 |
-
|
37 |
-
# Function to convert natural language expressions into valid math expressions using mathparser
|
38 |
-
def convert_speech_to_math(text):
|
39 |
-
try:
|
40 |
-
# Use mathparse to convert the text into a valid math expression
|
41 |
-
expression = mathparse.parse(text)
|
42 |
-
return expression
|
43 |
-
except Exception as e:
|
44 |
-
return f"Error in math conversion: {e}"
|
45 |
-
|
46 |
-
# Main function to handle input and process it for math calculation (text or voice)
|
47 |
def calculator(audio=None, text_input=None):
|
48 |
if audio:
|
49 |
-
|
50 |
-
|
51 |
-
query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
elif text_input:
|
|
|
53 |
query = text_input
|
54 |
-
print(f"Text
|
55 |
else:
|
56 |
return "No valid input provided.", None
|
57 |
-
|
58 |
-
# Process the math
|
59 |
return process_math(query)
|
60 |
|
61 |
-
#
|
62 |
-
|
63 |
-
#
|
64 |
-
|
65 |
-
fn=calculator,
|
66 |
-
inputs=[
|
67 |
-
gr.Audio(type="filepath", label="Speak a Math Expression (e.g., factorial(5), differentiate x squared, integrate sin(x), matrix([[1, 2], [3, 4]]) )"), # Voice input (required)
|
68 |
-
gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...") # Optional text input
|
69 |
-
],
|
70 |
-
outputs=[
|
71 |
-
"text", # Show the text result
|
72 |
-
gr.Audio(label="Listen to the result") # Provide the audio result
|
73 |
-
],
|
74 |
-
title="Advanced Math Solver",
|
75 |
-
description="Solve advanced math problems including factorials, permutations, combinations, logarithms, differentiation, integration, matrices, trigonometric functions, and more. Use speech or text input for accessibility.",
|
76 |
-
theme="huggingface", # Attractive theme for Gradio interface
|
77 |
-
live=True
|
78 |
-
)
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
if
|
83 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import sympy as sp
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
|
|
5 |
|
6 |
+
# Function to convert speech to text using CMU Sphinx
|
7 |
+
def voice_to_text():
|
8 |
+
recognizer = sr.Recognizer()
|
9 |
+
|
10 |
+
# Using the default microphone as the audio source
|
11 |
+
with sr.Microphone() as source:
|
12 |
+
print("Please speak a math expression (e.g., '2 plus 2', 'integrate x squared')...")
|
13 |
+
recognizer.adjust_for_ambient_noise(source)
|
14 |
+
audio = recognizer.listen(source)
|
15 |
+
|
16 |
+
try:
|
17 |
+
# Using CMU Sphinx for offline recognition
|
18 |
+
text = recognizer.recognize_sphinx(audio)
|
19 |
+
print(f"Recognized text: {text}")
|
20 |
+
return text
|
21 |
+
except sr.UnknownValueError:
|
22 |
+
return "Sorry, I could not understand the audio."
|
23 |
+
except sr.RequestError:
|
24 |
+
return "Could not request results from Sphinx service."
|
25 |
+
|
26 |
+
# Function to map speech to mathematical symbols
|
27 |
+
def convert_speech_to_math(text):
|
28 |
+
# Mapping words to math operators
|
29 |
+
mapping = {
|
30 |
+
"plus": "+",
|
31 |
+
"minus": "-",
|
32 |
+
"times": "*",
|
33 |
+
"divided by": "/",
|
34 |
+
"squared": "**2",
|
35 |
+
"cube": "**3",
|
36 |
+
"square root of": "sqrt",
|
37 |
+
"integrate": "integrate",
|
38 |
+
"derivative of": "diff"
|
39 |
+
}
|
40 |
+
|
41 |
+
# Replace speech terms with math symbols
|
42 |
+
for word, symbol in mapping.items():
|
43 |
+
text = text.replace(word, symbol)
|
44 |
+
|
45 |
+
# Return the updated expression
|
46 |
+
return text
|
47 |
+
|
48 |
+
# Function to process the math expression using SymPy
|
49 |
def process_math(query):
|
50 |
try:
|
51 |
# Clean up the input by removing unnecessary spaces
|
52 |
+
query = query.replace(" ", "") # Removing extra spaces
|
53 |
|
54 |
# Try to process the query using sympy
|
55 |
+
expr = sp.sympify(query)
|
56 |
+
result = expr
|
57 |
+
|
58 |
# Convert the result to speech and save as mp3
|
59 |
result_text = f"The result is: {result}"
|
60 |
tts = gTTS(result_text, lang="en")
|
61 |
tts.save("response.mp3")
|
62 |
|
63 |
+
# Return the result text and audio file path
|
64 |
return result_text, "response.mp3"
|
65 |
except Exception as e:
|
66 |
return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
|
67 |
|
68 |
+
# Main function for handling the math solver (either from voice or text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
def calculator(audio=None, text_input=None):
|
70 |
if audio:
|
71 |
+
# Convert speech to text using CMU Sphinx
|
72 |
+
query = voice_to_text()
|
73 |
+
if query.lower() == "sorry, i could not understand the audio.":
|
74 |
+
return query, None
|
75 |
+
|
76 |
+
print(f"Original Query (Voice): {query}")
|
77 |
+
|
78 |
+
# Convert speech to math expression
|
79 |
+
query = convert_speech_to_math(query)
|
80 |
+
print(f"Converted Math Expression: {query}")
|
81 |
+
|
82 |
elif text_input:
|
83 |
+
# If text input is provided directly
|
84 |
query = text_input
|
85 |
+
print(f"Original Query (Text): {query}")
|
86 |
else:
|
87 |
return "No valid input provided.", None
|
88 |
+
|
89 |
+
# Process the math query
|
90 |
return process_math(query)
|
91 |
|
92 |
+
# Example usage
|
93 |
+
if __name__ == "__main__":
|
94 |
+
# Get either voice or text input
|
95 |
+
choice = input("Do you want to use voice input or text input? (voice/text): ").strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
if choice == "voice":
|
98 |
+
result_text, audio_path = calculator(audio=True)
|
99 |
+
elif choice == "text":
|
100 |
+
text_input = input("Enter a math expression (e.g., '2 + 2', 'integrate sin(x)'): ").strip()
|
101 |
+
result_text, audio_path = calculator(text_input=text_input)
|
102 |
+
else:
|
103 |
+
print("Invalid choice, please enter 'voice' or 'text'.")
|
104 |
+
exit()
|
105 |
|
106 |
+
if result_text:
|
107 |
+
print(result_text)
|
108 |
+
# Optionally, you can play the audio result here (if needed)
|
109 |
+
os.system(f"start {audio_path}") # For Windows, replace 'start' with 'open' for macOS
|
110 |
+
else:
|
111 |
+
print("Error: Could not process the math expression.")
|