Spaces:
Runtime error
Runtime error
DSatishchandra
commited on
Commit
•
a992697
1
Parent(s):
455c585
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
import pyttsx3
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Initialize recognizer for speech recognition
|
7 |
+
recognizer = sr.Recognizer()
|
8 |
+
|
9 |
+
# Initialize text-to-speech engine
|
10 |
+
engine = pyttsx3.init()
|
11 |
+
|
12 |
+
# Initialize Hugging Face NLP pipeline for intent recognition
|
13 |
+
nlp = pipeline("zero-shot-classification")
|
14 |
+
|
15 |
+
# Define the food menu
|
16 |
+
menu = {
|
17 |
+
'Pizza': ['Cheese', 'Pepperoni', 'Vegetarian'],
|
18 |
+
'Beverages': ['Coke', 'Pepsi', 'Water']
|
19 |
+
}
|
20 |
+
|
21 |
+
# Function to process the order
|
22 |
+
def process_order(order):
|
23 |
+
if 'pizza' in order.lower():
|
24 |
+
return "What type of pizza would you like? Cheese, Pepperoni, or Vegetarian?"
|
25 |
+
elif 'coke' in order.lower():
|
26 |
+
return "One Coke added to your order."
|
27 |
+
else:
|
28 |
+
return "Sorry, we didn't catch that. Please try again."
|
29 |
+
|
30 |
+
# Function to handle speech recognition
|
31 |
+
def recognize_speech(audio):
|
32 |
+
try:
|
33 |
+
text = recognizer.recognize_google(audio)
|
34 |
+
response = process_order(text)
|
35 |
+
engine.say(response) # TTS response
|
36 |
+
engine.runAndWait()
|
37 |
+
return response
|
38 |
+
except Exception as e:
|
39 |
+
return "Sorry, I could not understand."
|
40 |
+
|
41 |
+
# Gradio Interface for the app
|
42 |
+
def create_gradio_interface():
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("## AI Voice Bot for Food Ordering")
|
45 |
+
|
46 |
+
# Speech-to-Text: User speaks into microphone
|
47 |
+
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak to the bot")
|
48 |
+
|
49 |
+
# Display the bot's response after recognition
|
50 |
+
output_text = gr.Textbox(label="Bot Response")
|
51 |
+
|
52 |
+
# Define the button to process the audio input
|
53 |
+
audio_input.change(fn=recognize_speech, inputs=audio_input, outputs=output_text)
|
54 |
+
|
55 |
+
return demo
|
56 |
+
|
57 |
+
# Create and launch the Gradio app
|
58 |
+
if __name__ == "__main__":
|
59 |
+
app = create_gradio_interface()
|
60 |
+
app.launch(share=True)
|