nagasurendra commited on
Commit
e0d893e
·
verified ·
1 Parent(s): d7d506a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -14
app.py CHANGED
@@ -76,19 +76,38 @@ def process_audio(audio_path):
76
  response = f"An error occurred: {str(e)}"
77
 
78
  audio_response_path = generate_voice_response(response)
79
- return response, audio_response_path
80
-
81
- def run_assistant(audio):
82
- transcription, audio_response_path = process_audio(audio)
83
  os.system(f"start {audio_response_path}") # Automatically play the response audio
84
- return transcription
85
-
86
- with gr.Blocks() as demo:
87
- gr.Markdown("# Voice-Activated Restaurant Assistant")
88
- audio_input = gr.Audio(type="filepath", label="Speak Now", streaming=True)
89
-
90
- output_text = gr.Textbox(label="Transcription")
91
 
92
- audio_input.change(fn=run_assistant, inputs=audio_input, outputs=output_text)
93
-
94
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()