DSatishchandra commited on
Commit
397d198
1 Parent(s): 3d3c5c6

Create order_assistant.py

Browse files
Files changed (1) hide show
  1. order_assistant.py +64 -0
order_assistant.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import speech_recognition as sr
3
+ import edge_tts
4
+ import asyncio
5
+ import random
6
+ import tempfile
7
+
8
+ # Example food menu
9
+ food_menu = {
10
+ "Starters": ["Spring Rolls", "Samosa", "Garlic Bread"],
11
+ "Main Course": ["Biryani", "Pizza", "Pasta", "Butter Chicken"],
12
+ "Desserts": ["Ice Cream", "Gulab Jamun", "Chocolate Cake"]
13
+ }
14
+
15
+ # Function to simulate food suggestions based on user input
16
+ def get_food_suggestion(food_type):
17
+ if food_type in food_menu:
18
+ return random.choice(food_menu[food_type])
19
+ return "No suggestion available"
20
+
21
+ # Speech-to-Text function
22
+ def recognize_speech_from_mic():
23
+ recognizer = sr.Recognizer()
24
+ with sr.Microphone() as source:
25
+ print("Say something!")
26
+ audio = recognizer.listen(source)
27
+ try:
28
+ return recognizer.recognize_google(audio)
29
+ except sr.UnknownValueError:
30
+ return "Sorry I didn't catch that"
31
+ except sr.RequestError:
32
+ return "Sorry, I'm having trouble reaching the service"
33
+
34
+ # Text-to-Speech function
35
+ async def text_to_speech(text):
36
+ communicate = edge_tts.Communicate(text, "en-US-AriaNeural", rate="0%", pitch="0Hz")
37
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
38
+ tmp_path = tmp_file.name
39
+ await communicate.save(tmp_path)
40
+ return tmp_path
41
+
42
+ # AI Food Ordering Assistant Function
43
+ async def food_order_assistant():
44
+ # Greeting
45
+ greeting_text = "Welcome to the restaurant! Please tell me your food preference."
46
+ audio_path = await text_to_speech(greeting_text)
47
+
48
+ # Wait for user to input food type
49
+ user_food_type = recognize_speech_from_mic()
50
+ suggestion = get_food_suggestion(user_food_type)
51
+
52
+ suggestion_text = f"I suggest you try {suggestion}."
53
+ suggestion_audio = await text_to_speech(suggestion_text)
54
+
55
+ # Wait for confirmation
56
+ confirmation = recognize_speech_from_mic()
57
+ if "yes" in confirmation.lower():
58
+ confirmation_text = f"Your order for {suggestion} has been confirmed."
59
+ confirmation_audio = await text_to_speech(confirmation_text)
60
+ return confirmation_audio, f"Confirmed order: {suggestion}"
61
+ else:
62
+ cancellation_text = "Okay, let's try again."
63
+ cancellation_audio = await text_to_speech(cancellation_text)
64
+ return cancellation_audio, "Order not confirmed"