nagasurendra commited on
Commit
ff2f5a3
·
verified ·
1 Parent(s): fd92587

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -82
app.py CHANGED
@@ -1,15 +1,10 @@
1
  import gradio as gr
2
  from gtts import gTTS
 
3
  import tempfile
4
  import os
5
- import speech_recognition as sr
6
- import threading
7
- import time
8
 
9
- # Store cart in a temporary storage
10
- cart = []
11
-
12
- # Define the menu items dynamically
13
  menu_items = {
14
  "Pizza": 10.99,
15
  "Burger": 8.49,
@@ -18,96 +13,79 @@ menu_items = {
18
  "Soda": 2.49
19
  }
20
 
21
- def generate_voice_response(text):
22
- tts = gTTS(text)
23
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
24
- temp_file.close()
25
- tts.save(temp_file.name)
26
- return temp_file.name
27
 
28
- def process_audio(audio_path):
29
  global cart
30
  recognizer = sr.Recognizer()
31
  response = ""
32
 
33
  try:
34
- with sr.AudioFile(audio_path) as source:
 
35
  audio = recognizer.record(source)
36
- input_text = recognizer.recognize_google(audio)
37
- print("User said:", input_text)
38
-
39
- if "menu" in input_text.lower():
40
- response = "Here is our menu:\n"
41
- for item, price in menu_items.items():
42
- response += f"{item}: ${price:.2f}\n"
43
- response += "\nWhat would you like to add to your cart?"
44
-
45
- elif any(item.lower() in input_text.lower() for item in menu_items):
46
- for item in menu_items:
47
- if item.lower() in input_text.lower():
48
- cart.append(item)
49
- total = sum(menu_items[cart_item] for cart_item in cart)
50
- response = f"{item} has been added to your cart. Your current cart includes:\n"
51
- for cart_item in cart:
52
- response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
53
- response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
54
- break
55
-
56
- elif "final order" in input_text.lower() or "submit order" in input_text.lower():
57
- if cart:
58
- total = sum(menu_items[cart_item] for cart_item in cart)
59
- response = "Your final order includes:\n"
60
- for item in cart:
61
- response += f"- {item}: ${menu_items[item]:.2f}\n"
62
- response += f"\nTotal: ${total:.2f}. Thank you for ordering!"
63
- cart = [] # Clear cart after finalizing order
64
- else:
65
- response = "Your cart is empty. Would you like to order something?"
66
-
67
- elif "stop" in input_text.lower():
68
- response = "Stopping voice assistant."
69
-
70
  else:
71
- response = "I didn’t quite catch that. Please tell me what you’d like to order."
 
 
 
72
 
73
  except sr.UnknownValueError:
74
- response = "Sorry, I didn’t catch that. Could you please repeat?"
75
  except Exception as e:
76
  response = f"An error occurred: {str(e)}"
77
 
78
- audio_response_path = generate_voice_response(response)
79
- os.system(f"start {audio_response_path}") # Automatically play the response audio
80
- return response
81
-
82
- def run_assistant_continuous():
83
- recognizer = sr.Recognizer()
84
- mic = sr.Microphone()
85
-
86
- with mic as source:
87
- recognizer.adjust_for_ambient_noise(source)
88
- print("Listening...")
89
 
90
- while True:
91
- try:
92
- with mic as source:
93
- audio = recognizer.listen(source)
94
- input_text = recognizer.recognize_google(audio)
95
- print("User said:", input_text)
96
 
97
- audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
98
- with open(audio_file.name, "wb") as f:
99
- f.write(audio.get_wav_data())
 
100
 
101
- response = process_audio(audio_file.name)
102
- print("Assistant response:", response)
 
 
 
 
 
103
 
104
- except sr.UnknownValueError:
105
- print("Sorry, I didn’t catch that. Please repeat.")
106
- except KeyboardInterrupt:
107
- print("Stopping voice assistant.")
108
- break
109
- except Exception as e:
110
- print(f"An error occurred: {str(e)}")
111
 
112
- if __name__ == "__main__":
113
- run_assistant_continuous()
 
1
  import gradio as gr
2
  from gtts import gTTS
3
+ import speech_recognition as sr
4
  import tempfile
5
  import os
 
 
 
6
 
7
+ # Menu items
 
 
 
8
  menu_items = {
9
  "Pizza": 10.99,
10
  "Burger": 8.49,
 
13
  "Soda": 2.49
14
  }
15
 
16
+ # Global cart
17
+ cart = []
 
 
 
 
18
 
19
+ def process_audio(audio_file):
20
  global cart
21
  recognizer = sr.Recognizer()
22
  response = ""
23
 
24
  try:
25
+ # Transcribe user input
26
+ with sr.AudioFile(audio_file) as source:
27
  audio = recognizer.record(source)
28
+ user_input = recognizer.recognize_google(audio)
29
+ print("User said:", user_input)
30
+
31
+ # Process user commands
32
+ if "menu" in user_input.lower():
33
+ response = "Here is our menu:\n"
34
+ for item, price in menu_items.items():
35
+ response += f"{item}: ${price:.2f}\n"
36
+ response += "What would you like to add to your cart?"
37
+
38
+ elif any(item.lower() in user_input.lower() for item in menu_items):
39
+ for item in menu_items:
40
+ if item.lower() in user_input.lower():
41
+ cart.append(item)
42
+ total = sum(menu_items[i] for i in cart)
43
+ response = f"{item} added to your cart. Your current cart includes:\n"
44
+ for i in cart:
45
+ response += f"- {i}: ${menu_items[i]:.2f}\n"
46
+ response += f"Total: ${total:.2f}. Anything else?"
47
+ break
48
+
49
+ elif "final order" in user_input.lower():
50
+ if cart:
51
+ total = sum(menu_items[i] for i in cart)
52
+ response = "Your final order includes:\n"
53
+ for i in cart:
54
+ response += f"- {i}: ${menu_items[i]:.2f}\n"
55
+ response += f"Total: ${total:.2f}. Thank you for ordering!"
56
+ cart.clear()
 
 
 
 
 
57
  else:
58
+ response = "Your cart is empty. Please add items to your cart."
59
+
60
+ else:
61
+ response = "Sorry, I didn't understand that. Please try again."
62
 
63
  except sr.UnknownValueError:
64
+ response = "Sorry, I couldn't understand your voice. Please try again."
65
  except Exception as e:
66
  response = f"An error occurred: {str(e)}"
67
 
68
+ # Generate voice response
69
+ tts = gTTS(response)
70
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
71
+ tts.save(temp_file.name)
 
 
 
 
 
 
 
72
 
73
+ return response, temp_file.name
 
 
 
 
 
74
 
75
+ # Gradio Interface
76
+ def voice_assistant(audio):
77
+ response_text, response_audio = process_audio(audio)
78
+ return response_text, response_audio
79
 
80
+ # Gradio App
81
+ with gr.Blocks() as app:
82
+ gr.Markdown("# Voice-Activated Restaurant Assistant")
83
+ with gr.Row():
84
+ voice_input = gr.Audio(source="microphone", type="filepath", label="Speak Now")
85
+ output_text = gr.Textbox(label="Assistant Response")
86
+ output_audio = gr.Audio(label="Audio Response", type="filepath")
87
 
88
+ voice_input.change(voice_assistant, inputs=voice_input, outputs=[output_text, output_audio])
 
 
 
 
 
 
89
 
90
+ # Run the app
91
+ app.launch()