DSatishchandra commited on
Commit
c5e0ddf
·
verified ·
1 Parent(s): fcae29d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -34
app.py CHANGED
@@ -1,38 +1,33 @@
1
  import gradio as gr
2
- from vosk import Model, KaldiRecognizer
3
- import sounddevice as sd
4
- import json
5
  from gtts import gTTS
6
  import os
7
-
8
- # Load Vosk Speech Model
9
- MODEL_PATH = "vosk-model-small-en-us-0.15" # Replace with your downloaded model path
10
- model = Model(MODEL_PATH)
11
 
12
  # Text-to-Speech Function
13
  def speak(text):
14
  tts = gTTS(text=text, lang='en')
15
  tts.save("output.mp3")
16
- os.system("play output.mp3 || mpg123 output.mp3") # Use play as a fallback if mpg123 is unavailable
17
 
18
- # Speech Recognition Function
19
- def recognize_speech():
20
- recognizer = KaldiRecognizer(model, 16000)
21
- print("Listening...")
22
-
23
- def callback(indata, frames, time, status):
24
- if status:
25
- print(status)
26
- if recognizer.AcceptWaveform(indata):
27
- result = json.loads(recognizer.Result())
28
- return result.get("text", "")
29
-
30
- with sd.RawInputStream(samplerate=16000, blocksize=8000, dtype="int16",
31
- channels=1, callback=callback):
32
- return callback
33
 
34
  # Voice Assistant Logic
35
- def voice_assistant(user_input):
 
 
 
 
36
  menu = {
37
  "Bread": ["Roti", "Naan", "Paratha"],
38
  "Veg Curries": ["Paneer Curry", "Mix Veg", "Dal Tadka"],
@@ -75,20 +70,15 @@ def voice_assistant(user_input):
75
  return response
76
 
77
  # Gradio Interface
78
- def interact_with_voice_assistant():
79
- user_input = recognize_speech()
80
- return voice_assistant(user_input)
81
-
82
  iface = gr.Interface(
83
- fn=interact_with_voice_assistant,
84
- inputs=[],
85
  outputs="text",
86
  title="DynamicMenuApp Voice Assistant",
87
  description=(
88
- "Interact with the DynamicMenuApp Voice Assistant to navigate the menu, "
89
- "customize your food, and place orders seamlessly."
90
- ),
91
- live=True
92
  )
93
 
94
  if __name__ == "__main__":
 
1
  import gradio as gr
 
 
 
2
  from gtts import gTTS
3
  import os
4
+ import speech_recognition as sr
 
 
 
5
 
6
  # Text-to-Speech Function
7
  def speak(text):
8
  tts = gTTS(text=text, lang='en')
9
  tts.save("output.mp3")
10
+ os.system("play output.mp3 || mpg123 output.mp3")
11
 
12
+ # Speech Recognition (Voice Input) Using Uploaded Audio
13
+ def recognize_speech(audio_file):
14
+ recognizer = sr.Recognizer()
15
+ with sr.AudioFile(audio_file) as source:
16
+ audio_data = recognizer.record(source)
17
+ try:
18
+ text = recognizer.recognize_google(audio_data)
19
+ return text
20
+ except sr.UnknownValueError:
21
+ return "I didn't catch that. Could you please repeat?"
22
+ except sr.RequestError:
23
+ return "Sorry, there was an issue recognizing your speech. Try again later."
 
 
 
24
 
25
  # Voice Assistant Logic
26
+ def voice_assistant(audio_file):
27
+ user_input = recognize_speech(audio_file)
28
+ if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
29
+ return user_input
30
+
31
  menu = {
32
  "Bread": ["Roti", "Naan", "Paratha"],
33
  "Veg Curries": ["Paneer Curry", "Mix Veg", "Dal Tadka"],
 
70
  return response
71
 
72
  # Gradio Interface
 
 
 
 
73
  iface = gr.Interface(
74
+ fn=voice_assistant,
75
+ inputs=gr.Audio(source="microphone", type="filepath"),
76
  outputs="text",
77
  title="DynamicMenuApp Voice Assistant",
78
  description=(
79
+ "Upload your voice commands to interact with the DynamicMenuApp Voice Assistant, "
80
+ "navigate the menu, customize your food, and place orders seamlessly."
81
+ )
 
82
  )
83
 
84
  if __name__ == "__main__":