Spaces:
Build error
Build error
DSatishchandra
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import speech_recognition as sr
|
3 |
+
import pyttsx3
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Initialize the text-to-speech engine
|
7 |
+
engine = pyttsx3.init()
|
8 |
+
|
9 |
+
# Initialize the transformer pipeline for NLP (Text Classification or any specific task)
|
10 |
+
nlp = pipeline("zero-shot-classification")
|
11 |
+
|
12 |
+
# Function to convert speech to text
|
13 |
+
def speech_to_text(audio_file):
|
14 |
+
recognizer = sr.Recognizer()
|
15 |
+
with sr.AudioFile(audio_file.name) as source:
|
16 |
+
audio = recognizer.record(source)
|
17 |
+
try:
|
18 |
+
text = recognizer.recognize_google(audio)
|
19 |
+
return text
|
20 |
+
except sr.UnknownValueError:
|
21 |
+
return "Sorry, I didn't catch that."
|
22 |
+
except sr.RequestError:
|
23 |
+
return "Sorry, there's an issue with the speech recognition service."
|
24 |
+
|
25 |
+
# Function to process text (handle menu ordering)
|
26 |
+
def process_order(text):
|
27 |
+
# You can add your logic here for handling various food orders and preferences
|
28 |
+
result = nlp(text, candidate_labels=["Vegan", "Halal", "Guilt-Free", "Regular"])
|
29 |
+
category = result['labels'][0]
|
30 |
+
|
31 |
+
if "Vegan" in category:
|
32 |
+
response = "You've chosen a Vegan dish."
|
33 |
+
elif "Halal" in category:
|
34 |
+
response = "You've chosen a Halal dish."
|
35 |
+
elif "Guilt-Free" in category:
|
36 |
+
response = "You've chosen a Guilt-Free dish."
|
37 |
+
else:
|
38 |
+
response = "You've chosen a regular dish."
|
39 |
+
|
40 |
+
return response
|
41 |
+
|
42 |
+
# Function for Text-to-Speech (Response back to user)
|
43 |
+
def speak_response(text):
|
44 |
+
engine.say(text)
|
45 |
+
engine.runAndWait()
|
46 |
+
|
47 |
+
# Create Gradio interface
|
48 |
+
def voice_assistant(audio_file):
|
49 |
+
text = speech_to_text(audio_file)
|
50 |
+
response = process_order(text)
|
51 |
+
speak_response(response)
|
52 |
+
return response
|
53 |
+
|
54 |
+
iface = gr.Interface(fn=voice_assistant,
|
55 |
+
inputs=gr.inputs.Audio(source="microphone", type="file"),
|
56 |
+
outputs="text",
|
57 |
+
live=True)
|
58 |
+
|
59 |
+
# Launch Gradio app
|
60 |
+
if __name__ == "__main__":
|
61 |
+
iface.launch()
|