Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import pyttsx3
|
| 3 |
from transformers import pipeline
|
| 4 |
-
|
| 5 |
|
| 6 |
-
# Initialize
|
|
|
|
|
|
|
|
|
|
| 7 |
engine = pyttsx3.init()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return recognizer.recognize_google(audio)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
total = sum(menu[item] for item in order)
|
| 23 |
-
return order, total
|
| 24 |
|
| 25 |
-
|
| 26 |
engine.say(response)
|
| 27 |
engine.runAndWait()
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pyttsx3
|
| 3 |
from transformers import pipeline
|
| 4 |
+
import whisper
|
| 5 |
|
| 6 |
+
# Initialize Whisper Model
|
| 7 |
+
model = whisper.load_model("base")
|
| 8 |
+
|
| 9 |
+
# Initialize Text-to-Speech
|
| 10 |
engine = pyttsx3.init()
|
| 11 |
|
| 12 |
+
# Load NLP Model
|
| 13 |
+
nlp_pipeline = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 14 |
+
|
| 15 |
+
# Define Menu
|
| 16 |
+
MENU = {
|
| 17 |
+
"biryani": 200,
|
| 18 |
+
"naan": 50,
|
| 19 |
+
"curry": 150,
|
| 20 |
+
"paneer": 180,
|
| 21 |
+
"samosa": 20,
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Speech-to-Text Function
|
| 25 |
+
def speech_to_text(audio):
|
| 26 |
+
try:
|
| 27 |
+
audio_path = audio
|
| 28 |
+
result = model.transcribe(audio_path)
|
| 29 |
+
return result["text"]
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: {str(e)}"
|
| 32 |
+
|
| 33 |
+
# NLP Intent Detection
|
| 34 |
+
def process_order(transcription):
|
| 35 |
+
items = []
|
| 36 |
+
total_cost = 0
|
| 37 |
|
| 38 |
+
for item in MENU.keys():
|
| 39 |
+
if item in transcription.lower():
|
| 40 |
+
items.append(item)
|
| 41 |
+
total_cost += MENU[item]
|
|
|
|
| 42 |
|
| 43 |
+
if items:
|
| 44 |
+
response = f"You've ordered: {', '.join(items)}. Total cost is ₹{total_cost}."
|
| 45 |
+
else:
|
| 46 |
+
response = "Sorry, I could not find any menu items in your order."
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Text-to-Speech for Response
|
| 49 |
engine.say(response)
|
| 50 |
engine.runAndWait()
|
| 51 |
+
return response
|
| 52 |
|
| 53 |
+
# Gradio Interface
|
| 54 |
+
def order_from_audio(audio):
|
| 55 |
+
transcription = speech_to_text(audio)
|
| 56 |
+
return transcription, process_order(transcription)
|
| 57 |
+
|
| 58 |
+
# Gradio UI
|
| 59 |
+
app = gr.Interface(
|
| 60 |
+
fn=order_from_audio,
|
| 61 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Textbox(label="Transcription"),
|
| 64 |
+
gr.Textbox(label="Order Confirmation"),
|
| 65 |
+
],
|
| 66 |
+
title="Voice Ordering System",
|
| 67 |
+
description="Speak your order, and the system will process it.",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
app.launch()
|