DSatishchandra commited on
Commit
5710e4d
·
verified ·
1 Parent(s): c6016eb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pyttsx3
3
+ import speech_recognition as sr
4
+
5
+ # Initialize Text-to-Speech Engine
6
+ engine = pyttsx3.init()
7
+ def speak(text):
8
+ engine.say(text)
9
+ engine.runAndWait()
10
+
11
+ # Speech Recognition Function
12
+ def recognize_speech():
13
+ recognizer = sr.Recognizer()
14
+ with sr.Microphone() as source:
15
+ print("Listening...")
16
+ try:
17
+ audio = recognizer.listen(source, timeout=5)
18
+ text = recognizer.recognize_google(audio)
19
+ print(f"You said: {text}")
20
+ return text
21
+ except sr.UnknownValueError:
22
+ return "I didn't catch that. Could you please repeat?"
23
+ except sr.RequestError:
24
+ return "Sorry, I'm having trouble understanding you. Please try again later."
25
+
26
+ # Voice Assistant Logic
27
+ def voice_assistant(user_input):
28
+ menu = {
29
+ "Bread": ["Roti", "Naan", "Paratha"],
30
+ "Veg Curries": ["Paneer Curry", "Mix Veg", "Dal Tadka"],
31
+ "Non-Veg Curries": ["Chicken Curry", "Mutton Curry", "Fish Curry"],
32
+ "Biryanis": ["Veg Biryani", "Chicken Biryani", "Mutton Biryani"]
33
+ }
34
+
35
+ if "menu" in user_input.lower() or "help" in user_input.lower():
36
+ response = (
37
+ "We have Bread, Veg Curries, Non-Veg Curries, and Biryanis. "
38
+ "Please let me know which category you'd like to explore."
39
+ )
40
+ speak(response)
41
+ return response
42
+
43
+ for category, items in menu.items():
44
+ if category.lower() in user_input.lower():
45
+ response = f"In {category}, we have: {', '.join(items)}. What would you like to order?"
46
+ speak(response)
47
+ return response
48
+
49
+ if "order" in user_input.lower():
50
+ response = (
51
+ "You have selected your items. Would you like to add any customizations, "
52
+ "such as extra cheese, less spice, or something else?"
53
+ )
54
+ speak(response)
55
+ return response
56
+
57
+ if "confirm" in user_input.lower():
58
+ response = (
59
+ "Your order has been confirmed! Thank you for ordering with DynamicMenuApp. "
60
+ "Your food will be ready soon. Have a great day!"
61
+ )
62
+ speak(response)
63
+ return response
64
+
65
+ response = "I didn't quite catch that. Could you please repeat?"
66
+ speak(response)
67
+ return response
68
+
69
+ # Gradio UI
70
+ def interact_with_voice_assistant():
71
+ user_input = recognize_speech()
72
+ if user_input:
73
+ return voice_assistant(user_input)
74
+ else:
75
+ return "No input detected. Please try again."
76
+
77
+ iface = gr.Interface(
78
+ fn=interact_with_voice_assistant,
79
+ inputs=[],
80
+ outputs="text",
81
+ title="DynamicMenuApp Voice Assistant",
82
+ description=(
83
+ "Interact with the DynamicMenuApp Voice Assistant to navigate the menu, "
84
+ "customize your food, and place orders seamlessly."
85
+ ),
86
+ live=True
87
+ )
88
+
89
+ if __name__ == "__main__":
90
+ iface.launch()