Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Load menu from the uploaded file
|
| 5 |
menu_file_path = "menu.xlsx"
|
|
@@ -26,20 +30,20 @@ def recommend_items(dish_name):
|
|
| 26 |
return "No specific recommendations."
|
| 27 |
|
| 28 |
def process_order(audio_file):
|
| 29 |
-
import speech_recognition as sr
|
| 30 |
recognizer = sr.Recognizer()
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
with sr.AudioFile(audio_file) as source:
|
| 37 |
audio_data = recognizer.record(source)
|
| 38 |
user_input = recognizer.recognize_google(audio_data)
|
| 39 |
except sr.UnknownValueError:
|
| 40 |
-
return "I couldn't understand that. Could you please repeat?",
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"Error processing audio: {e}",
|
| 43 |
|
| 44 |
global conversation_history
|
| 45 |
|
|
@@ -71,12 +75,20 @@ def process_order(audio_file):
|
|
| 71 |
for user, ai in conversation_history:
|
| 72 |
file.write(f"Customer: {user}\nAI: {ai}\n\n")
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
iface = gr.Interface(
|
| 77 |
fn=process_order,
|
| 78 |
-
inputs=gr.Audio(type="
|
| 79 |
-
outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")],
|
| 80 |
live=True,
|
| 81 |
allow_flagging="never"
|
| 82 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
import tempfile
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
# Load menu from the uploaded file
|
| 9 |
menu_file_path = "menu.xlsx"
|
|
|
|
| 30 |
return "No specific recommendations."
|
| 31 |
|
| 32 |
def process_order(audio_file):
|
|
|
|
| 33 |
recognizer = sr.Recognizer()
|
| 34 |
|
| 35 |
+
# Ensure a valid audio file is passed
|
| 36 |
+
if not audio_file:
|
| 37 |
+
return "No valid audio file provided. Please try again.", None
|
| 38 |
|
| 39 |
try:
|
| 40 |
with sr.AudioFile(audio_file) as source:
|
| 41 |
audio_data = recognizer.record(source)
|
| 42 |
user_input = recognizer.recognize_google(audio_data)
|
| 43 |
except sr.UnknownValueError:
|
| 44 |
+
return "I couldn't understand that. Could you please repeat?", None
|
| 45 |
except Exception as e:
|
| 46 |
+
return f"Error processing audio: {e}", None
|
| 47 |
|
| 48 |
global conversation_history
|
| 49 |
|
|
|
|
| 75 |
for user, ai in conversation_history:
|
| 76 |
file.write(f"Customer: {user}\nAI: {ai}\n\n")
|
| 77 |
|
| 78 |
+
# Generate audio response using gTTS
|
| 79 |
+
try:
|
| 80 |
+
tts = gTTS(response)
|
| 81 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 82 |
+
tts.save(temp_audio_file.name)
|
| 83 |
+
temp_audio_file.close()
|
| 84 |
+
return response, temp_audio_file.name
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Error generating audio response: {e}", None
|
| 87 |
|
| 88 |
iface = gr.Interface(
|
| 89 |
fn=process_order,
|
| 90 |
+
inputs=gr.Audio(type="numpy"), # Accept raw audio input
|
| 91 |
+
outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")], # Handle audio response correctly
|
| 92 |
live=True,
|
| 93 |
allow_flagging="never"
|
| 94 |
)
|