Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
|
|
| 1 |
import speech_recognition as sr
|
| 2 |
import pyttsx3
|
| 3 |
-
import
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
def speak(text):
|
| 7 |
engine = pyttsx3.init()
|
|
@@ -21,7 +23,7 @@ def get_voice_input(prompt, timeout=30):
|
|
| 21 |
audio = recognizer.listen(source, timeout=timeout)
|
| 22 |
except sr.WaitTimeoutError:
|
| 23 |
speak("Time is up. Please try again.")
|
| 24 |
-
return
|
| 25 |
|
| 26 |
try:
|
| 27 |
text = recognizer.recognize_google(audio)
|
|
@@ -29,27 +31,29 @@ def get_voice_input(prompt, timeout=30):
|
|
| 29 |
return text
|
| 30 |
except sr.UnknownValueError:
|
| 31 |
speak("Sorry, I could not understand. Please repeat.")
|
| 32 |
-
return
|
| 33 |
except sr.RequestError:
|
| 34 |
speak("Speech service is unavailable right now.")
|
| 35 |
return None
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
else:
|
| 45 |
-
|
| 46 |
-
exit()
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
else:
|
| 55 |
-
speak("No response received for the email. Please try again later.")
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
import speech_recognition as sr
|
| 3 |
import pyttsx3
|
| 4 |
+
import pyaudio
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
|
| 8 |
def speak(text):
|
| 9 |
engine = pyttsx3.init()
|
|
|
|
| 23 |
audio = recognizer.listen(source, timeout=timeout)
|
| 24 |
except sr.WaitTimeoutError:
|
| 25 |
speak("Time is up. Please try again.")
|
| 26 |
+
return None
|
| 27 |
|
| 28 |
try:
|
| 29 |
text = recognizer.recognize_google(audio)
|
|
|
|
| 31 |
return text
|
| 32 |
except sr.UnknownValueError:
|
| 33 |
speak("Sorry, I could not understand. Please repeat.")
|
| 34 |
+
return None
|
| 35 |
except sr.RequestError:
|
| 36 |
speak("Speech service is unavailable right now.")
|
| 37 |
return None
|
| 38 |
|
| 39 |
+
@app.route('/')
|
| 40 |
+
def home():
|
| 41 |
+
return render_template('index.html')
|
| 42 |
+
|
| 43 |
+
@app.route('/voice_input', methods=['POST'])
|
| 44 |
+
def voice_input():
|
| 45 |
+
data = request.json
|
| 46 |
+
field = data.get("field")
|
| 47 |
+
if field == "username":
|
| 48 |
+
prompt = "Please tell me your name"
|
| 49 |
+
elif field == "email":
|
| 50 |
+
prompt = "Please tell me your email address"
|
| 51 |
else:
|
| 52 |
+
return jsonify({"error": "Invalid field"}), 400
|
|
|
|
| 53 |
|
| 54 |
+
response = get_voice_input(prompt, timeout=30)
|
| 55 |
+
return jsonify({"response": response})
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
speak("Welcome to Biryani Hub")
|
| 59 |
+
app.run(debug=True)
|
|
|
|
|
|