Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pyttsx3
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Initialize Text-to-Speech engine (
|
| 6 |
engine = pyttsx3.init()
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def speak_response(text):
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
# Function to
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Function to handle menu
|
| 20 |
def handle_order(item):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
elif item.lower() == "burger":
|
| 24 |
-
response = "You have selected Burger. It contains beef patty, lettuce, and cheese."
|
| 25 |
-
else:
|
| 26 |
-
response = "Sorry, I don't have information about that item."
|
| 27 |
speak_response(response)
|
| 28 |
return response
|
| 29 |
|
| 30 |
-
# Gradio
|
| 31 |
def chatbot_interface():
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
-
# Display
|
| 34 |
gr.Image("assets/menu_logo.png", label="Click to speak", elem_id="voice_logo", interactive=True)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak your order")
|
| 38 |
-
|
| 39 |
-
# Text output for displaying response
|
| 40 |
output_text = gr.Textbox(label="Assistant Response", interactive=False)
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
| 43 |
def on_audio_input(audio):
|
| 44 |
-
user_input =
|
| 45 |
-
response = handle_order(user_input)
|
| 46 |
-
output_text.update(value=response)
|
| 47 |
return response
|
| 48 |
|
| 49 |
-
# Attach microphone action
|
| 50 |
audio_input.change(on_audio_input, inputs=[audio_input], outputs=[output_text])
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pyttsx3
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Initialize Text-to-Speech engine (pyttsx3 for offline or gTTS for better quality)
|
| 8 |
engine = pyttsx3.init()
|
| 9 |
|
| 10 |
+
# Initialize Speech Recognition engine
|
| 11 |
+
recognizer = sr.Recognizer()
|
| 12 |
+
|
| 13 |
+
# Define Menu items for the restaurant
|
| 14 |
+
menu = {
|
| 15 |
+
"pizza": "Delicious pepperoni pizza with cheese and sauce.",
|
| 16 |
+
"burger": "Beef burger with lettuce, tomato, and cheese.",
|
| 17 |
+
"pasta": "Creamy pasta with garlic and parmesan.",
|
| 18 |
+
"salad": "A healthy vegetable salad with olive oil dressing."
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Function to convert text to speech using gTTS
|
| 22 |
def speak_response(text):
|
| 23 |
+
tts = gTTS(text=text, lang='en')
|
| 24 |
+
tts.save("response.mp3")
|
| 25 |
+
os.system("start response.mp3") # Windows; on Linux, use 'mpg321 response.mp3'
|
| 26 |
|
| 27 |
+
# Function to listen to user speech and convert it to text
|
| 28 |
+
def listen_input():
|
| 29 |
+
with sr.Microphone() as source:
|
| 30 |
+
print("Listening for order...")
|
| 31 |
+
audio = recognizer.listen(source)
|
| 32 |
+
try:
|
| 33 |
+
command = recognizer.recognize_google(audio)
|
| 34 |
+
print(f"You said: {command}")
|
| 35 |
+
return command
|
| 36 |
+
except sr.UnknownValueError:
|
| 37 |
+
return "Sorry, I didn't understand that. Could you please repeat?"
|
| 38 |
+
except sr.RequestError:
|
| 39 |
+
return "Sorry, there was an issue with the speech service."
|
| 40 |
|
| 41 |
+
# Function to handle menu interaction and confirm orders
|
| 42 |
def handle_order(item):
|
| 43 |
+
item = item.lower()
|
| 44 |
+
response = menu.get(item, "Sorry, we don't have that item on the menu.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
speak_response(response)
|
| 46 |
return response
|
| 47 |
|
| 48 |
+
# Gradio interface setup
|
| 49 |
def chatbot_interface():
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
+
# Display menu logo image (optional)
|
| 52 |
gr.Image("assets/menu_logo.png", label="Click to speak", elem_id="voice_logo", interactive=True)
|
| 53 |
|
| 54 |
+
# Textbox for displaying responses
|
|
|
|
|
|
|
|
|
|
| 55 |
output_text = gr.Textbox(label="Assistant Response", interactive=False)
|
| 56 |
|
| 57 |
+
# Microphone button to listen to user input
|
| 58 |
+
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak your order")
|
| 59 |
+
|
| 60 |
+
# Handle audio input and provide the appropriate response
|
| 61 |
def on_audio_input(audio):
|
| 62 |
+
user_input = listen_input() # Convert speech to text
|
| 63 |
+
response = handle_order(user_input) # Handle order
|
| 64 |
+
output_text.update(value=response) # Display assistant's response
|
| 65 |
return response
|
| 66 |
|
| 67 |
+
# Attach microphone action to trigger audio input
|
| 68 |
audio_input.change(on_audio_input, inputs=[audio_input], outputs=[output_text])
|
| 69 |
|
| 70 |
demo.launch()
|