Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,58 @@
|
|
| 1 |
-
|
| 2 |
from gtts import gTTS
|
| 3 |
-
import
|
| 4 |
-
import speech_recognition as sr
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
audio_file = "response.mp3"
|
| 14 |
tts.save(audio_file)
|
| 15 |
return audio_file
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
with sr.AudioFile(audio_file) as source:
|
| 21 |
-
audio_data = recognizer.record(source)
|
| 22 |
-
try:
|
| 23 |
-
return recognizer.recognize_google(audio_data)
|
| 24 |
-
except sr.UnknownValueError:
|
| 25 |
-
return "I'm sorry, I couldn't understand that. Could you repeat?"
|
| 26 |
-
except sr.RequestError:
|
| 27 |
-
return "There was an error with the speech recognition service."
|
| 28 |
-
|
| 29 |
-
# Chatbot Logic using OpenAI GPT
|
| 30 |
-
def chatbot_response(user_input):
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
engine="text-davinci-003", # Use a powerful GPT model
|
| 34 |
-
prompt=f"User: {user_input}\nChatbot:",
|
| 35 |
-
max_tokens=150,
|
| 36 |
-
temperature=0.7,
|
| 37 |
-
)
|
| 38 |
-
return response.choices[0].text.strip()
|
| 39 |
except Exception as e:
|
| 40 |
-
return f"Error
|
| 41 |
-
|
| 42 |
-
# Gradio Interface Logic
|
| 43 |
-
def process_interaction(audio_file):
|
| 44 |
-
# Convert user speech to text
|
| 45 |
-
user_text = speech_to_text(audio_file)
|
| 46 |
-
if "Error" in user_text or "sorry" in user_text:
|
| 47 |
-
return user_text, None
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# Convert
|
| 53 |
-
|
| 54 |
|
| 55 |
-
return
|
| 56 |
|
| 57 |
# Gradio Interface
|
| 58 |
-
|
| 59 |
-
fn=
|
| 60 |
-
inputs=gr.Audio(
|
| 61 |
-
outputs=[gr.Textbox(label="
|
| 62 |
-
title="
|
| 63 |
-
description="
|
| 64 |
-
live=True,
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
from gtts import gTTS
|
| 3 |
+
import gradio as gr
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Initialize Whisper pipeline for speech-to-text
|
| 7 |
+
pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v3-turbo")
|
| 8 |
+
|
| 9 |
+
# Menu for the restaurant
|
| 10 |
+
menu = {
|
| 11 |
+
"Starters": ["Soup", "Spring Rolls"],
|
| 12 |
+
"Main Course": ["Paneer Butter Masala", "Chicken Curry", "Veg Biryani"],
|
| 13 |
+
"Breads": ["Roti", "Naan", "Paratha"],
|
| 14 |
+
"Desserts": ["Gulab Jamun", "Ice Cream"],
|
| 15 |
+
"Drinks": ["Mango Lassi", "Soda", "Water"]
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Function to convert text to speech
|
| 19 |
+
def text_to_speech(text):
|
| 20 |
+
tts = gTTS(text, lang="en")
|
| 21 |
audio_file = "response.mp3"
|
| 22 |
tts.save(audio_file)
|
| 23 |
return audio_file
|
| 24 |
|
| 25 |
+
# Chatbot logic
|
| 26 |
+
def chatbot_conversation(audio_file):
|
| 27 |
+
# Speech-to-text using Whisper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
+
transcription = pipe(audio_file)["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
+
return f"Error: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Generate a response based on transcription
|
| 34 |
+
if "menu" in transcription.lower():
|
| 35 |
+
response = "Our menu categories are: " + ", ".join(menu.keys())
|
| 36 |
+
elif "order" in transcription.lower():
|
| 37 |
+
response = "What would you like to order? We have " + ", ".join(menu["Main Course"])
|
| 38 |
+
elif "thank you" in transcription.lower():
|
| 39 |
+
response = "You're welcome! Enjoy your meal!"
|
| 40 |
+
else:
|
| 41 |
+
response = "I'm sorry, I didn't understand that. Could you please repeat?"
|
| 42 |
|
| 43 |
+
# Convert response to audio
|
| 44 |
+
audio_response = text_to_speech(response)
|
| 45 |
|
| 46 |
+
return response, audio_response
|
| 47 |
|
| 48 |
# Gradio Interface
|
| 49 |
+
iface = gr.Interface(
|
| 50 |
+
fn=chatbot_conversation,
|
| 51 |
+
inputs=gr.Audio(type="filepath"),
|
| 52 |
+
outputs=[gr.Textbox(label="Transcription"), gr.Audio(label="Response Audio")],
|
| 53 |
+
title="Restaurant Chatbot with Whisper ASR",
|
| 54 |
+
description="Speak to the chatbot and get a response!",
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
iface.launch()
|