Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,9 @@ def speak_and_save(text):
|
|
| 24 |
|
| 25 |
# Speech Recognition Function
|
| 26 |
def recognize_speech(audio_file_path):
|
|
|
|
|
|
|
|
|
|
| 27 |
recognizer = sr.Recognizer()
|
| 28 |
try:
|
| 29 |
with sr.AudioFile(audio_file_path) as source:
|
|
@@ -37,10 +40,14 @@ def recognize_speech(audio_file_path):
|
|
| 37 |
|
| 38 |
# Voice Assistant Logic
|
| 39 |
def process_order(audio_file_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
user_input = recognize_speech(audio_file_path)
|
| 41 |
conversation_history.append(f"Customer: {user_input}")
|
| 42 |
|
| 43 |
-
if user_input.startswith("
|
| 44 |
response = user_input
|
| 45 |
conversation_history.append(f"AI: {response}")
|
| 46 |
return response, speak_and_save(response)
|
|
@@ -87,19 +94,19 @@ def create_interface():
|
|
| 87 |
gr.Markdown("## Restaurant Voice Assistant")
|
| 88 |
gr.Markdown("Talk to our voice assistant to place your order or ask about the menu!")
|
| 89 |
|
| 90 |
-
audio_input = gr.Audio(type="filepath", label="Your Voice Input")
|
| 91 |
text_response = gr.Textbox(label="Text Response")
|
| 92 |
audio_response = gr.Audio(label="Audio Response")
|
| 93 |
|
| 94 |
# Add JavaScript for auto-playing audio
|
| 95 |
gr.HTML("""
|
| 96 |
<script>
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
audioPlayer.play();
|
| 101 |
-
}
|
| 102 |
-
}
|
| 103 |
</script>
|
| 104 |
""")
|
| 105 |
|
|
|
|
| 24 |
|
| 25 |
# Speech Recognition Function
|
| 26 |
def recognize_speech(audio_file_path):
|
| 27 |
+
if not audio_file_path or not os.path.exists(audio_file_path):
|
| 28 |
+
return "No valid audio input provided. Please try again."
|
| 29 |
+
|
| 30 |
recognizer = sr.Recognizer()
|
| 31 |
try:
|
| 32 |
with sr.AudioFile(audio_file_path) as source:
|
|
|
|
| 40 |
|
| 41 |
# Voice Assistant Logic
|
| 42 |
def process_order(audio_file_path):
|
| 43 |
+
if not audio_file_path or not os.path.exists(audio_file_path):
|
| 44 |
+
response = "No audio input detected. Please record or upload your voice."
|
| 45 |
+
return response, speak_and_save(response)
|
| 46 |
+
|
| 47 |
user_input = recognize_speech(audio_file_path)
|
| 48 |
conversation_history.append(f"Customer: {user_input}")
|
| 49 |
|
| 50 |
+
if user_input.startswith("No audio input detected") or user_input.startswith("I didn't"):
|
| 51 |
response = user_input
|
| 52 |
conversation_history.append(f"AI: {response}")
|
| 53 |
return response, speak_and_save(response)
|
|
|
|
| 94 |
gr.Markdown("## Restaurant Voice Assistant")
|
| 95 |
gr.Markdown("Talk to our voice assistant to place your order or ask about the menu!")
|
| 96 |
|
| 97 |
+
audio_input = gr.Audio(type="filepath", label="Your Voice Input")
|
| 98 |
text_response = gr.Textbox(label="Text Response")
|
| 99 |
audio_response = gr.Audio(label="Audio Response")
|
| 100 |
|
| 101 |
# Add JavaScript for auto-playing audio
|
| 102 |
gr.HTML("""
|
| 103 |
<script>
|
| 104 |
+
document.addEventListener("change", function() {
|
| 105 |
+
const audioPlayer = document.querySelector("audio");
|
| 106 |
+
if (audioPlayer) {
|
| 107 |
audioPlayer.play();
|
| 108 |
+
}
|
| 109 |
+
});
|
| 110 |
</script>
|
| 111 |
""")
|
| 112 |
|