Subbu1304 commited on
Commit
c2d1596
·
verified ·
1 Parent(s): 59d0669

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -34
app.py CHANGED
@@ -1,44 +1,71 @@
1
- import openai
2
- import pyttsx3 # TTS
3
  from transformers import pipeline
4
- from speech_recognition import Recognizer, Microphone
5
 
6
- # Initialize TTS engine
 
 
 
7
  engine = pyttsx3.init()
8
 
9
- # Speech recognition
10
- recognizer = Recognizer()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def listen():
13
- with Microphone() as source:
14
- print("Listening...")
15
- audio = recognizer.listen(source)
16
- return recognizer.recognize_google(audio)
17
 
18
- def process_order(text):
19
- # Sample NLP pipeline
20
- menu = {"chicken biryani": 200, "garlic naan": 50, "paneer curry": 150}
21
- order = [item for item in menu if item in text.lower()]
22
- total = sum(menu[item] for item in order)
23
- return order, total
24
 
25
- def speak(response):
26
  engine.say(response)
27
  engine.runAndWait()
 
28
 
29
- # Main loop
30
- print("Voice ordering system for blind individuals.")
31
- speak("Welcome to our restaurant. Please place your order.")
32
- while True:
33
- try:
34
- user_input = listen()
35
- items, total = process_order(user_input)
36
- if items:
37
- response = f"You've ordered {', '.join(items)}. Total is {total}."
38
- print(response)
39
- speak(response)
40
- speak("Say confirm to place your order or cancel to restart.")
41
- else:
42
- speak("I couldn't find that on the menu. Please try again.")
43
- except Exception as e:
44
- speak("Sorry, I didn't catch that. Please repeat.")
 
 
 
 
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()