Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,56 @@
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
2 |
-
import os
|
3 |
import torch
|
4 |
-
import speech_recognition as sr
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
|
|
7 |
import re
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
-
recognizer = sr.Recognizer()
|
11 |
|
12 |
-
# Load Whisper Model
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
-
|
15 |
|
16 |
-
# Function to generate
|
17 |
-
def
|
18 |
tts = gTTS(text=text, lang="en")
|
19 |
-
tts.save(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
generate_audio("Tell me your name.", "static/ask_name.mp3")
|
24 |
-
generate_audio("Please provide your email.", "static/ask_email.mp3")
|
25 |
-
generate_audio("Thank you for registration.", "static/thank_you.mp3")
|
26 |
|
27 |
-
#
|
28 |
-
def
|
29 |
-
return re.sub(r
|
30 |
|
31 |
@app.route("/")
|
32 |
-
def
|
33 |
return render_template("index.html")
|
34 |
|
35 |
-
@app.route("/
|
36 |
-
def
|
37 |
if "audio" not in request.files:
|
38 |
-
return jsonify({"error": "No audio file"}), 400
|
39 |
|
40 |
audio_file = request.files["audio"]
|
41 |
-
audio_path = "static
|
42 |
audio_file.save(audio_path)
|
43 |
|
44 |
try:
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
return jsonify({"text":
|
49 |
except Exception as e:
|
50 |
return jsonify({"error": str(e)}), 500
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
-
app.run(host="0.0.0.0", port=
|
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
|
|
2 |
import torch
|
|
|
3 |
from transformers import pipeline
|
4 |
from gtts import gTTS
|
5 |
+
import os
|
6 |
import re
|
7 |
|
8 |
app = Flask(__name__)
|
|
|
9 |
|
10 |
+
# Load Whisper Model for English Transcription
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
asr_model = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=0 if device == "cuda" else -1)
|
13 |
|
14 |
+
# Function to generate audio prompts
|
15 |
+
def generate_audio_prompt(text, filename):
|
16 |
tts = gTTS(text=text, lang="en")
|
17 |
+
tts.save(os.path.join("static", filename))
|
18 |
+
|
19 |
+
# Generate audio prompts
|
20 |
+
prompts = {
|
21 |
+
"welcome": "Welcome to Biryani Hub.",
|
22 |
+
"ask_name": "Tell me your name.",
|
23 |
+
"ask_email": "Please provide your email address.",
|
24 |
+
"thank_you": "Thank you for registration."
|
25 |
+
}
|
26 |
|
27 |
+
for key, text in prompts.items():
|
28 |
+
generate_audio_prompt(text, f"{key}.mp3")
|
|
|
|
|
|
|
29 |
|
30 |
+
# Clean transcribed text to allow only English letters, numbers, and basic punctuation
|
31 |
+
def clean_transcription(text):
|
32 |
+
return re.sub(r"[^a-zA-Z0-9@.\s]", "", text)
|
33 |
|
34 |
@app.route("/")
|
35 |
+
def index():
|
36 |
return render_template("index.html")
|
37 |
|
38 |
+
@app.route("/transcribe", methods=["POST"])
|
39 |
+
def transcribe():
|
40 |
if "audio" not in request.files:
|
41 |
+
return jsonify({"error": "No audio file provided"}), 400
|
42 |
|
43 |
audio_file = request.files["audio"]
|
44 |
+
audio_path = os.path.join("static", "temp.wav")
|
45 |
audio_file.save(audio_path)
|
46 |
|
47 |
try:
|
48 |
+
# Transcribe audio to text
|
49 |
+
result = asr_model(audio_path, generate_kwargs={"language": "en"})
|
50 |
+
transcribed_text = clean_transcription(result["text"])
|
51 |
+
return jsonify({"text": transcribed_text})
|
52 |
except Exception as e:
|
53 |
return jsonify({"error": str(e)}), 500
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
+
app.run(host="0.0.0.0", port=5000, debug=True)
|