Spaces:
Runtime error
Runtime error
DSatishchandra
commited on
Commit
•
67436ec
1
Parent(s):
49e2d43
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,12 @@ import gradio as gr
|
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
-
|
6 |
from transformers import pipeline
|
7 |
|
|
|
|
|
|
|
8 |
# Initialize recognizer for speech recognition
|
9 |
recognizer = sr.Recognizer()
|
10 |
|
@@ -30,7 +33,7 @@ def process_order(order):
|
|
30 |
def recognize_speech(audio):
|
31 |
try:
|
32 |
# If audio is from file, use SpeechRecognition to convert speech to text
|
33 |
-
if isinstance(audio, str): # Audio file input
|
34 |
with sr.AudioFile(audio) as source:
|
35 |
audio_data = recognizer.record(source)
|
36 |
text = recognizer.recognize_google(audio_data)
|
@@ -45,8 +48,9 @@ def recognize_speech(audio):
|
|
45 |
tts = gTTS(text=response, lang='en')
|
46 |
tts.save("response.mp3")
|
47 |
|
48 |
-
# Play the MP3 response using
|
49 |
-
|
|
|
50 |
|
51 |
return response
|
52 |
except Exception as e:
|
@@ -58,8 +62,8 @@ def create_gradio_interface():
|
|
58 |
with gr.Blocks() as demo:
|
59 |
gr.Markdown("## AI Voice Bot for Food Ordering")
|
60 |
|
61 |
-
# Audio Input: User speaks into microphone or uploads a file
|
62 |
-
audio_input = gr.Audio(type="
|
63 |
|
64 |
# Display the bot's response after recognition
|
65 |
output_text = gr.Textbox(label="Bot Response")
|
|
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
+
import pygame # Use pygame for playing audio
|
6 |
from transformers import pipeline
|
7 |
|
8 |
+
# Initialize pygame for audio playback
|
9 |
+
pygame.mixer.init()
|
10 |
+
|
11 |
# Initialize recognizer for speech recognition
|
12 |
recognizer = sr.Recognizer()
|
13 |
|
|
|
33 |
def recognize_speech(audio):
|
34 |
try:
|
35 |
# If audio is from file, use SpeechRecognition to convert speech to text
|
36 |
+
if isinstance(audio, str): # Audio file input (filepath)
|
37 |
with sr.AudioFile(audio) as source:
|
38 |
audio_data = recognizer.record(source)
|
39 |
text = recognizer.recognize_google(audio_data)
|
|
|
48 |
tts = gTTS(text=response, lang='en')
|
49 |
tts.save("response.mp3")
|
50 |
|
51 |
+
# Play the MP3 response using pygame
|
52 |
+
pygame.mixer.music.load("response.mp3")
|
53 |
+
pygame.mixer.music.play()
|
54 |
|
55 |
return response
|
56 |
except Exception as e:
|
|
|
62 |
with gr.Blocks() as demo:
|
63 |
gr.Markdown("## AI Voice Bot for Food Ordering")
|
64 |
|
65 |
+
# Audio Input: User speaks into microphone or uploads a file (filepath)
|
66 |
+
audio_input = gr.Audio(type="filepath", label="Speak to the bot (Upload or Record Audio)")
|
67 |
|
68 |
# Display the bot's response after recognition
|
69 |
output_text = gr.Textbox(label="Bot Response")
|