DSatishchandra commited on
Commit
d67403c
1 Parent(s): e7f0157

Update order_assistant.py

Browse files
Files changed (1) hide show
  1. order_assistant.py +70 -52
order_assistant.py CHANGED
@@ -1,68 +1,86 @@
1
- import json
2
  import random
 
3
  import speech_recognition as sr
4
  import edge_tts
5
- import asyncio
6
  import tempfile
 
7
 
8
  # Load the food menu from menu.json
9
- with open("menu.json") as f:
10
- food_menu = json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Function to get food suggestions based on user input
13
- def get_food_suggestion(food_type, filter_type):
14
- if food_type in food_menu:
15
- return random.choice(food_menu[food_type].get(filter_type, []))
16
- return "No suggestion available"
17
 
18
- # Speech-to-Text function
19
- def recognize_speech_from_mic():
20
- recognizer = sr.Recognizer()
21
- with sr.Microphone() as source:
22
- print("Say something!")
23
- audio = recognizer.listen(source)
24
- try:
25
- return recognizer.recognize_google(audio) # Use Google Speech Recognition
26
- except sr.UnknownValueError:
27
- return "Sorry I didn't catch that"
28
- except sr.RequestError:
29
- return "Sorry, I'm having trouble reaching the service"
30
 
31
- # Text-to-Speech function
32
- async def text_to_speech(text):
33
- communicate = edge_tts.Communicate(text, "en-US-AriaNeural", rate="0%", pitch="0Hz")
34
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
35
- tmp_path = tmp_file.name
36
- await communicate.save(tmp_path)
37
- return tmp_path
38
 
39
- # AI Food Ordering Assistant Function
40
- async def food_order_assistant(audio_input, food_type):
41
- # Greeting and introduction
42
- greeting_text = "Welcome to the restaurant! What would you like to order today? Are you looking for Vegan, Halal, or Guilt-Free options?"
43
- audio_path = await text_to_speech(greeting_text)
44
 
45
- # Recognize customer's preference (Vegan, Halal, Guilt-Free)
46
- dietary_preference = recognize_speech_from_mic().lower()
 
 
 
 
 
 
 
 
 
47
 
48
- if dietary_preference not in ["vegan", "halal", "guilt-free"]:
49
- dietary_preference = "vegan" # Default to Vegan if unrecognized
 
 
 
 
50
 
51
- # Get food suggestion based on preference
52
- suggestion = get_food_suggestion(food_type, dietary_preference)
 
53
 
54
- suggestion_text = f"I suggest you try {suggestion}. Does that sound good?"
55
- suggestion_audio = await text_to_speech(suggestion_text)
 
56
 
57
- # Wait for customer confirmation
58
- confirmation = recognize_speech_from_mic().lower()
 
59
 
60
- if "yes" in confirmation:
61
- confirmation_text = f"Your order for {suggestion} has been confirmed. Sending the order to the kitchen."
62
- confirmation_audio = await text_to_speech(confirmation_text)
63
- # Simulate sending order to kitchen
64
- return confirmation_audio, f"Confirmed order: {suggestion}"
65
- else:
66
- cancellation_text = "Okay, let's try again."
67
- cancellation_audio = await text_to_speech(cancellation_text)
68
- return cancellation_audio, "Order not confirmed"
 
1
+ import base64
2
  import random
3
+ import numpy as np
4
  import speech_recognition as sr
5
  import edge_tts
 
6
  import tempfile
7
+ import json
8
 
9
  # Load the food menu from menu.json
10
+ def load_food_menu():
11
+ with open("menu.json") as f:
12
+ return json.load(f)
13
+
14
+ food_menu = load_food_menu()
15
+
16
+ class AudioProcessor:
17
+ @staticmethod
18
+ def encode_audio(data, sample_rate):
19
+ encoded = base64.b64encode(data.tobytes()).decode("UTF-8")
20
+ return {
21
+ "realtimeInput": {
22
+ "mediaChunks": [
23
+ {
24
+ "mimeType": f"audio/pcm;rate={sample_rate}",
25
+ "data": encoded,
26
+ }
27
+ ],
28
+ },
29
+ }
30
 
31
+ @staticmethod
32
+ def process_audio_response(data):
33
+ audio_data = base64.b64decode(data)
34
+ return np.frombuffer(audio_data, dtype=np.int16)
 
35
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ class OrderAssistant:
38
+ def __init__(self, food_menu):
39
+ self.food_menu = food_menu
40
+ self.audio_processor = AudioProcessor()
 
 
 
41
 
42
+ def get_food_suggestion(self, food_type, dietary_preference):
43
+ if food_type in self.food_menu:
44
+ return random.choice(self.food_menu[food_type].get(dietary_preference, []))
45
+ return "Sorry, we don't have that option."
 
46
 
47
+ def recognize_speech_from_mic(self):
48
+ recognizer = sr.Recognizer()
49
+ with sr.Microphone() as source:
50
+ print("Say something!")
51
+ audio = recognizer.listen(source)
52
+ try:
53
+ return recognizer.recognize_google(audio) # Google Speech Recognition
54
+ except sr.UnknownValueError:
55
+ return "Sorry I didn't catch that"
56
+ except sr.RequestError:
57
+ return "Sorry, I'm having trouble reaching the service"
58
 
59
+ async def text_to_speech(self, text):
60
+ communicate = edge_tts.Communicate(text, "en-US-AriaNeural", rate="0%", pitch="0Hz")
61
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
62
+ tmp_path = tmp_file.name
63
+ await communicate.save(tmp_path)
64
+ return tmp_path
65
 
66
+ async def food_order_assistant(self, audio_input, food_type):
67
+ greeting_text = "Welcome to the restaurant! What would you like to order today? Are you looking for Vegan, Halal, or Guilt-Free options?"
68
+ audio_path = await self.text_to_speech(greeting_text)
69
 
70
+ dietary_preference = self.recognize_speech_from_mic().lower()
71
+ if dietary_preference not in ["vegan", "halal", "guilt-free"]:
72
+ dietary_preference = "vegan" # Default to Vegan if unrecognized
73
 
74
+ suggestion = self.get_food_suggestion(food_type, dietary_preference)
75
+ suggestion_text = f"I suggest you try {suggestion}. Does that sound good?"
76
+ suggestion_audio = await self.text_to_speech(suggestion_text)
77
 
78
+ confirmation = self.recognize_speech_from_mic().lower()
79
+ if "yes" in confirmation:
80
+ confirmation_text = f"Your order for {suggestion} has been confirmed. Sending the order to the kitchen."
81
+ confirmation_audio = await self.text_to_speech(confirmation_text)
82
+ return confirmation_audio, f"Confirmed order: {suggestion}"
83
+ else:
84
+ cancellation_text = "Okay, let's try again."
85
+ cancellation_audio = await self.text_to_speech(cancellation_text)
86
+ return cancellation_audio, "Order not confirmed"