surendranaga commited on
Commit
268b977
·
1 Parent(s): fd2ec0b

Initial commit for voice assistant

Browse files
Files changed (2) hide show
  1. app.py.txt +86 -0
  2. requirements.txt.txt +3 -0
app.py.txt ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import speech_recognition as sr
3
+ import pyttsx3
4
+
5
+ # Initialize text-to-speech engine
6
+ engine = pyttsx3.init()
7
+
8
+ # Store cart in a temporary storage
9
+ cart = []
10
+
11
+ # Define the menu items dynamically
12
+ menu_items = {
13
+ "Pizza": 10.99,
14
+ "Burger": 8.49,
15
+ "Pasta": 12.99,
16
+ "Salad": 7.99,
17
+ "Soda": 2.49
18
+ }
19
+
20
+ def speak_response(response):
21
+ """Generate voice output."""
22
+ engine.say(response)
23
+ engine.runAndWait()
24
+
25
+ def process_input(input_text):
26
+ global cart
27
+ response = ""
28
+
29
+ if "price of" in input_text:
30
+ matched_items = [item for item in menu_items if item.lower() in input_text]
31
+ if len(matched_items) == 1:
32
+ item = matched_items[0]
33
+ response = f"The price of {item} is ${menu_items[item]:.2f}."
34
+ elif len(matched_items) > 1:
35
+ response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please ask for the price of one item at a time."
36
+ else:
37
+ response = "I couldn't find that item on the menu. Please ask for an item available in the menu."
38
+ elif any(item.lower() in input_text for item in menu_items):
39
+ matched_items = [item for item in menu_items if item.lower() in input_text]
40
+ if len(matched_items) == 1:
41
+ item = matched_items[0]
42
+ cart.append(item)
43
+ response = f"{item} has been added to your cart. Your current cart includes:\n"
44
+ for cart_item in cart:
45
+ response += f"- {cart_item}\n"
46
+ response += "\nWould you like to add anything else?"
47
+ elif len(matched_items) > 1:
48
+ response = f"I detected multiple items in your input: {', '.join(matched_items)}. Please mention one item at a time."
49
+ elif "menu" in input_text:
50
+ response = "Here is our menu:\n"
51
+ for item in menu_items.keys():
52
+ response += f"{item}\n"
53
+ response += "\nWhat would you like to order?"
54
+ elif "final order" in input_text or "submit order" in input_text:
55
+ if cart:
56
+ total = calculate_total(cart)
57
+ response = "Your final order includes:\n"
58
+ for item in cart:
59
+ response += f"- {item}\n"
60
+ response += f"\nTotal amount: ${total:.2f}. Thank you for ordering!"
61
+ cart.clear()
62
+ else:
63
+ response = "Your cart is empty. Would you like to order something?"
64
+ else:
65
+ response = "I didn’t quite catch that. Please tell me what you’d like to order or ask about."
66
+
67
+ return response
68
+
69
+ def voice_assistant(audio_file):
70
+ recognizer = sr.Recognizer()
71
+ try:
72
+ with sr.AudioFile(audio_file) as source:
73
+ audio = recognizer.record(source)
74
+ input_text = recognizer.recognize_google(audio).lower()
75
+ response = process_input(input_text)
76
+ return response
77
+ except sr.UnknownValueError:
78
+ return "Sorry, I couldn't understand the audio. Please try again."
79
+
80
+ gr.Interface(
81
+ fn=voice_assistant,
82
+ inputs=gr.Audio(source="microphone", type="filepath"),
83
+ outputs="text",
84
+ live=True,
85
+ title="Voice Assistant"
86
+ ).launch()
requirements.txt.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ speechrecognition
3
+ pyttsx3