Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,24 +5,30 @@ import os
|
|
5 |
import gradio as gr
|
6 |
|
7 |
# Function to convert speech to text using CMU Sphinx
|
8 |
-
def
|
9 |
recognizer = sr.Recognizer()
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Function to map speech to mathematical symbols
|
28 |
def convert_speech_to_math(text):
|
@@ -74,10 +80,10 @@ def process_math(query):
|
|
74 |
return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
|
75 |
|
76 |
# Gradio interface function to handle the input
|
77 |
-
def calculator(choice, text_input=None):
|
78 |
if choice == "voice":
|
79 |
-
# Convert speech to text using CMU Sphinx
|
80 |
-
query =
|
81 |
if query.lower() == "sorry, i could not understand the audio.":
|
82 |
return query, None
|
83 |
|
@@ -103,7 +109,8 @@ def start_interface():
|
|
103 |
fn=calculator,
|
104 |
inputs=[
|
105 |
gr.Radio(["voice", "text"], label="Choose Input Type"), # Option to select between voice and text input
|
106 |
-
gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...", visible=False) # Hidden text input when voice is selected
|
|
|
107 |
],
|
108 |
outputs=[
|
109 |
"text", # Display the result text
|
@@ -115,7 +122,7 @@ def start_interface():
|
|
115 |
live=True # Allow live updating as the user interacts
|
116 |
)
|
117 |
|
118 |
-
interface.launch()
|
119 |
|
120 |
if __name__ == "__main__":
|
121 |
start_interface()
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
# Function to convert speech to text using CMU Sphinx
|
8 |
+
def voice_to_text_from_microphone(audio=None):
|
9 |
recognizer = sr.Recognizer()
|
10 |
|
11 |
+
# If audio file is provided, use it. Otherwise, record from the microphone.
|
12 |
+
if audio:
|
13 |
+
# Using uploaded audio file
|
14 |
+
with sr.AudioFile(audio) as source:
|
15 |
+
audio_data = recognizer.record(source)
|
16 |
+
else:
|
17 |
+
# Using the default microphone as the audio source
|
18 |
+
with sr.Microphone() as source:
|
19 |
+
print("Please speak a math expression...")
|
20 |
+
recognizer.adjust_for_ambient_noise(source)
|
21 |
+
audio_data = recognizer.listen(source)
|
22 |
+
|
23 |
+
try:
|
24 |
+
# Using CMU Sphinx for offline recognition
|
25 |
+
text = recognizer.recognize_sphinx(audio_data)
|
26 |
+
print(f"Recognized text: {text}")
|
27 |
+
return text
|
28 |
+
except sr.UnknownValueError:
|
29 |
+
return "Sorry, I could not understand the audio."
|
30 |
+
except sr.RequestError:
|
31 |
+
return "Could not request results from Sphinx service."
|
32 |
|
33 |
# Function to map speech to mathematical symbols
|
34 |
def convert_speech_to_math(text):
|
|
|
80 |
return f"Error: Unable to process the query. Please check the syntax. Error: {e}", None
|
81 |
|
82 |
# Gradio interface function to handle the input
|
83 |
+
def calculator(choice, text_input=None, audio=None):
|
84 |
if choice == "voice":
|
85 |
+
# Convert speech to text using CMU Sphinx from the microphone or uploaded audio
|
86 |
+
query = voice_to_text_from_microphone(audio)
|
87 |
if query.lower() == "sorry, i could not understand the audio.":
|
88 |
return query, None
|
89 |
|
|
|
109 |
fn=calculator,
|
110 |
inputs=[
|
111 |
gr.Radio(["voice", "text"], label="Choose Input Type"), # Option to select between voice and text input
|
112 |
+
gr.Textbox(label="Or Type a Math Expression (Optional)", placeholder="Type your math expression here...", visible=False), # Hidden text input when voice is selected
|
113 |
+
gr.Audio(label="Upload an Audio File (Optional)", type="filepath", visible=False) # Option to upload an audio file
|
114 |
],
|
115 |
outputs=[
|
116 |
"text", # Display the result text
|
|
|
122 |
live=True # Allow live updating as the user interacts
|
123 |
)
|
124 |
|
125 |
+
interface.launch()(share=true)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
start_interface()
|