Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,33 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import sounddevice as sd
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
engine.say(text)
|
| 12 |
-
engine.runAndWait()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
microphone = sr.Microphone()
|
| 17 |
-
|
| 18 |
-
speak(prompt)
|
| 19 |
-
|
| 20 |
-
with microphone as source:
|
| 21 |
-
print(prompt)
|
| 22 |
-
recognizer.adjust_for_ambient_noise(source)
|
| 23 |
-
try:
|
| 24 |
-
audio = recognizer.listen(source, timeout=timeout)
|
| 25 |
-
except sr.WaitTimeoutError:
|
| 26 |
-
speak("Time is up. Please try again.")
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
text = recognizer.recognize_google(audio)
|
| 31 |
-
print(f"Recognized: {text}")
|
| 32 |
-
return text
|
| 33 |
-
except sr.UnknownValueError:
|
| 34 |
-
speak("Sorry, I could not understand. Please repeat.")
|
| 35 |
-
return None
|
| 36 |
-
except sr.RequestError:
|
| 37 |
-
speak("Speech service is unavailable right now.")
|
| 38 |
-
return None
|
| 39 |
|
| 40 |
-
@app.route(
|
| 41 |
-
def home():
|
| 42 |
-
return render_template('index.html')
|
| 43 |
-
|
| 44 |
-
@app.route('/voice_input', methods=['POST'])
|
| 45 |
def voice_input():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
return jsonify({"response": response})
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
speak("Welcome to Biryani Hub")
|
| 60 |
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Load ASR model
|
| 8 |
+
asr_pipeline = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-large-960h")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Store user data
|
| 11 |
+
user_data = {"name": "", "email": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
@app.route("/voice_input", methods=["POST"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def voice_input():
|
| 15 |
+
file = request.files["audio"]
|
| 16 |
+
file_path = "temp.wav"
|
| 17 |
+
file.save(file_path)
|
| 18 |
+
|
| 19 |
+
# Convert Speech to Text
|
| 20 |
+
text = asr_pipeline(file_path)["text"]
|
| 21 |
+
|
| 22 |
+
# Check which field is missing
|
| 23 |
+
if user_data["name"] == "":
|
| 24 |
+
user_data["name"] = text
|
| 25 |
+
return jsonify({"message": "Name recorded", "name": text})
|
| 26 |
+
elif user_data["email"] == "":
|
| 27 |
+
user_data["email"] = text
|
| 28 |
+
return jsonify({"message": "Email recorded", "email": text})
|
| 29 |
|
| 30 |
+
return jsonify({"message": "All details recorded", "data": user_data})
|
|
|
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
|
|
|
| 33 |
app.run(debug=True)
|