nagasurendra commited on
Commit
c480fa5
·
verified ·
1 Parent(s): 9993a51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -21
app.py CHANGED
@@ -3,10 +3,11 @@ from gtts import gTTS
3
  import os
4
  import tempfile
5
  import json
6
- import speech_recognition as sr
 
7
  import threading
8
- import time
9
- from playsound import playsound
10
 
11
  # Store cart in a temporary storage
12
  cart = []
@@ -33,29 +34,46 @@ def generate_voice_response(text):
33
  def calculate_total(cart):
34
  return sum(menu_items[item] for item in cart)
35
 
36
- def play_audio_with_control(audio_path):
37
- global playback_control
38
- playback_control["stop"] = False
39
- playback_control["pause"] = False
40
  try:
41
- for _ in range(3): # Retry loop in case of interruptions
42
- if playback_control["stop"]:
43
- break
44
- playsound(audio_path)
45
- break
46
  except Exception as e:
47
  print("Error playing audio:", e)
48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  def restaurant_voice_assistant():
50
  global cart
51
  recognizer = sr.Recognizer()
52
 
53
  # Continuously listen for user commands
54
  while True:
55
- with sr.Microphone() as source:
56
- print("Listening for input...")
57
- try:
58
- audio = recognizer.listen(source)
 
 
 
 
59
  input_text = recognizer.recognize_google(audio)
60
  print("You said:", input_text)
61
 
@@ -99,12 +117,12 @@ def restaurant_voice_assistant():
99
 
100
  # Generate and play audio response
101
  audio_path = generate_voice_response(response)
102
- threading.Thread(target=play_audio_with_control, args=(audio_path,)).start()
103
 
104
- except sr.UnknownValueError:
105
- print("Sorry, I didn’t catch that. Could you please repeat?")
106
- except Exception as e:
107
- print("Error occurred:", e)
108
 
109
  # Run the assistant
110
  threading.Thread(target=restaurant_voice_assistant).start()
 
3
  import os
4
  import tempfile
5
  import json
6
+ import sounddevice as sd
7
+ import queue
8
  import threading
9
+ from scipy.io.wavfile import write
10
+ import speech_recognition as sr
11
 
12
  # Store cart in a temporary storage
13
  cart = []
 
34
  def calculate_total(cart):
35
  return sum(menu_items[item] for item in cart)
36
 
37
+ def play_audio(audio_path):
38
+ import simpleaudio as sa
 
 
39
  try:
40
+ playback = sa.WaveObject.from_wave_file(audio_path).play()
41
+ playback.wait_done()
 
 
 
42
  except Exception as e:
43
  print("Error playing audio:", e)
44
 
45
+ # Audio recording setup
46
+ q = queue.Queue()
47
+ def audio_callback(indata, frames, time, status):
48
+ if status:
49
+ print(status)
50
+ q.put(indata.copy())
51
+
52
+ def record_audio():
53
+ fs = 16000 # Sample rate
54
+ seconds = 5 # Duration
55
+ print("Recording...")
56
+ audio_data = []
57
+ with sd.InputStream(callback=audio_callback, samplerate=fs, channels=1):
58
+ for _ in range(int(fs * seconds / 1024)):
59
+ audio_data.append(q.get())
60
+ print("Recording finished.")
61
+ return b"".join(audio_data)
62
+
63
  def restaurant_voice_assistant():
64
  global cart
65
  recognizer = sr.Recognizer()
66
 
67
  # Continuously listen for user commands
68
  while True:
69
+ try:
70
+ audio_data = record_audio()
71
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
72
+ temp_audio.write(audio_data)
73
+ temp_audio_path = temp_audio.name
74
+
75
+ with sr.AudioFile(temp_audio_path) as source:
76
+ audio = recognizer.record(source)
77
  input_text = recognizer.recognize_google(audio)
78
  print("You said:", input_text)
79
 
 
117
 
118
  # Generate and play audio response
119
  audio_path = generate_voice_response(response)
120
+ play_audio(audio_path)
121
 
122
+ except sr.UnknownValueError:
123
+ print("Sorry, I didn’t catch that. Could you please repeat?")
124
+ except Exception as e:
125
+ print("Error occurred:", e)
126
 
127
  # Run the assistant
128
  threading.Thread(target=restaurant_voice_assistant).start()