Subbu1304 commited on
Commit
2d07494
·
verified ·
1 Parent(s): 567f69d

Create Ai.py

Browse files
Files changed (1) hide show
  1. Ai.py +89 -0
Ai.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pyttsx3
2
+ import speech_recognition as sr
3
+
4
+ # Initialize Text-to-Speech engine
5
+ engine = pyttsx3.init()
6
+
7
+ def speak(text):
8
+ """Convert text to speech."""
9
+ engine.say(text)
10
+ engine.runAndWait()
11
+
12
+ def listen():
13
+ """Capture voice input and return as text."""
14
+ recognizer = sr.Recognizer()
15
+ with sr.Microphone() as source:
16
+ print("Listening...")
17
+ speak("I am listening. Please speak now.")
18
+ recognizer.adjust_for_ambient_noise(source)
19
+ try:
20
+ audio = recognizer.listen(source, timeout=5)
21
+ command = recognizer.recognize_google(audio)
22
+ return command.lower()
23
+ except sr.UnknownValueError:
24
+ speak("Sorry, I could not understand. Could you repeat?")
25
+ return None
26
+ except sr.RequestError:
27
+ speak("There seems to be an issue with the voice recognition service.")
28
+ return None
29
+ except sr.WaitTimeoutError:
30
+ speak("I did not hear anything. Please try again.")
31
+ return None
32
+
33
+ def read_menu():
34
+ """Read the menu from a text file."""
35
+ try:
36
+ with open("menu.txt", "r") as file:
37
+ menu = file.readlines()
38
+ return menu
39
+ except FileNotFoundError:
40
+ speak("Menu file not found.")
41
+ return []
42
+
43
+ def process_order(order, menu):
44
+ """Process the voice command to identify menu items."""
45
+ selected_items = []
46
+ for item in menu:
47
+ if any(word in order for word in item.lower().split(" - ")[0].split()): # Match keywords
48
+ selected_items.append(item.strip())
49
+ return selected_items
50
+
51
+ def main():
52
+ """Main program logic."""
53
+ speak("Welcome to the restaurant voice assistant.")
54
+ speak("Would you like to hear the menu?")
55
+
56
+ response = listen()
57
+ if response and "yes" in response:
58
+ menu = read_menu()
59
+ if menu:
60
+ speak("Here is the menu.")
61
+ for item in menu:
62
+ speak(item)
63
+ else:
64
+ return
65
+
66
+ speak("What would you like to order?")
67
+ order = listen()
68
+ if not order:
69
+ speak("I couldn't hear your order. Please try again.")
70
+ return
71
+
72
+ menu = read_menu()
73
+ selected_items = process_order(order, menu)
74
+ if selected_items:
75
+ speak("You have ordered:")
76
+ for item in selected_items:
77
+ speak(item)
78
+ speak("Would you like to confirm this order?")
79
+
80
+ confirmation = listen()
81
+ if confirmation and "yes" in confirmation:
82
+ speak("Your order has been placed successfully. Thank you!")
83
+ else:
84
+ speak("Order cancelled. Please let me know if you'd like to order something else.")
85
+ else:
86
+ speak("I couldn't match your order with any menu items. Please try again.")
87
+
88
+ if __name__ == "__main__":
89
+ main()