AIVoice / app.py
DSatishchandra's picture
Create app.py
1912209 verified
raw
history blame
1.94 kB
import gradio as gr
import speech_recognition as sr
import pyttsx3
from transformers import pipeline
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Initialize the transformer pipeline for NLP (Text Classification or any specific task)
nlp = pipeline("zero-shot-classification")
# Function to convert speech to text
def speech_to_text(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file.name) as source:
audio = recognizer.record(source)
try:
text = recognizer.recognize_google(audio)
return text
except sr.UnknownValueError:
return "Sorry, I didn't catch that."
except sr.RequestError:
return "Sorry, there's an issue with the speech recognition service."
# Function to process text (handle menu ordering)
def process_order(text):
# You can add your logic here for handling various food orders and preferences
result = nlp(text, candidate_labels=["Vegan", "Halal", "Guilt-Free", "Regular"])
category = result['labels'][0]
if "Vegan" in category:
response = "You've chosen a Vegan dish."
elif "Halal" in category:
response = "You've chosen a Halal dish."
elif "Guilt-Free" in category:
response = "You've chosen a Guilt-Free dish."
else:
response = "You've chosen a regular dish."
return response
# Function for Text-to-Speech (Response back to user)
def speak_response(text):
engine.say(text)
engine.runAndWait()
# Create Gradio interface
def voice_assistant(audio_file):
text = speech_to_text(audio_file)
response = process_order(text)
speak_response(response)
return response
iface = gr.Interface(fn=voice_assistant,
inputs=gr.inputs.Audio(source="microphone", type="file"),
outputs="text",
live=True)
# Launch Gradio app
if __name__ == "__main__":
iface.launch()