Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ from gtts import gTTS
|
|
3 |
import os
|
4 |
import speech_recognition as sr
|
5 |
from tempfile import NamedTemporaryFile
|
|
|
|
|
6 |
|
7 |
# Initialize the recognizer
|
8 |
recognizer = sr.Recognizer()
|
@@ -15,6 +17,7 @@ menu_items = {
|
|
15 |
}
|
16 |
cart = []
|
17 |
|
|
|
18 |
def text_to_speech(text):
|
19 |
"""Convert text to speech and provide an audio file."""
|
20 |
tts = gTTS(text=text, lang='en')
|
@@ -30,6 +33,7 @@ def read_menu():
|
|
30 |
menu_text += "Please tell me the items you want to add to your cart."
|
31 |
return menu_text
|
32 |
|
|
|
33 |
def process_audio_command(audio_path):
|
34 |
"""Process the user's audio command."""
|
35 |
global cart
|
@@ -65,23 +69,41 @@ def process_audio_command(audio_path):
|
|
65 |
|
66 |
return "Sorry, I didn't understand that. Please try again."
|
67 |
|
68 |
-
#
|
69 |
-
def
|
70 |
-
"""
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
|
|
74 |
with gr.Blocks() as app:
|
75 |
gr.Markdown("# Voice-Activated Restaurant Menu System")
|
76 |
gr.Markdown("Speak your command to interact with the menu dynamically.")
|
77 |
|
78 |
with gr.Row():
|
79 |
audio_input = gr.Audio(label="Speak Your Command", type="filepath")
|
80 |
-
|
81 |
-
|
82 |
-
audio_output = gr.Audio(label="Assistant Response")
|
83 |
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
|
86 |
if __name__ == "__main__":
|
|
|
87 |
app.launch()
|
|
|
3 |
import os
|
4 |
import speech_recognition as sr
|
5 |
from tempfile import NamedTemporaryFile
|
6 |
+
import threading
|
7 |
+
import time
|
8 |
|
9 |
# Initialize the recognizer
|
10 |
recognizer = sr.Recognizer()
|
|
|
17 |
}
|
18 |
cart = []
|
19 |
|
20 |
+
# Helper Functions
|
21 |
def text_to_speech(text):
|
22 |
"""Convert text to speech and provide an audio file."""
|
23 |
tts = gTTS(text=text, lang='en')
|
|
|
33 |
menu_text += "Please tell me the items you want to add to your cart."
|
34 |
return menu_text
|
35 |
|
36 |
+
# Command Processing
|
37 |
def process_audio_command(audio_path):
|
38 |
"""Process the user's audio command."""
|
39 |
global cart
|
|
|
69 |
|
70 |
return "Sorry, I didn't understand that. Please try again."
|
71 |
|
72 |
+
# Continuous Conversation Loop
|
73 |
+
def continuous_conversation():
|
74 |
+
"""Continuously listen to and respond to user commands."""
|
75 |
+
while True:
|
76 |
+
with NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
77 |
+
try:
|
78 |
+
print("Listening...")
|
79 |
+
with sr.Microphone() as source:
|
80 |
+
recognizer.adjust_for_ambient_noise(source)
|
81 |
+
audio = recognizer.listen(source)
|
82 |
+
with open(temp_audio.name, "wb") as f:
|
83 |
+
f.write(audio.get_wav_data())
|
84 |
+
response = process_audio_command(temp_audio.name)
|
85 |
+
print(response)
|
86 |
+
audio_response = text_to_speech(response)
|
87 |
+
os.system(f"mpg123 {audio_response}")
|
88 |
+
except Exception as e:
|
89 |
+
print("Error processing audio input.")
|
90 |
|
91 |
+
# Gradio Interface
|
92 |
with gr.Blocks() as app:
|
93 |
gr.Markdown("# Voice-Activated Restaurant Menu System")
|
94 |
gr.Markdown("Speak your command to interact with the menu dynamically.")
|
95 |
|
96 |
with gr.Row():
|
97 |
audio_input = gr.Audio(label="Speak Your Command", type="filepath")
|
98 |
+
response_text = gr.Textbox(label="Assistant Response")
|
99 |
+
audio_output = gr.Audio(label="Assistant Voice Response")
|
|
|
100 |
|
101 |
+
def handle_conversation(audio_path):
|
102 |
+
response = process_audio_command(audio_path)
|
103 |
+
return response, text_to_speech(response)
|
104 |
+
|
105 |
+
audio_input.change(handle_conversation, inputs=audio_input, outputs=[response_text, audio_output])
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
+
threading.Thread(target=continuous_conversation, daemon=True).start()
|
109 |
app.launch()
|