File size: 1,938 Bytes
1912209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()