Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,30 @@ import torch
|
|
4 |
import speech_recognition as sr
|
5 |
from transformers import pipeline
|
6 |
from gtts import gTTS
|
7 |
-
import
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
recognizer = sr.Recognizer()
|
11 |
|
12 |
-
# Load
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=0 if device == "cuda" else -1)
|
15 |
|
16 |
-
# Function to generate and save voice prompts
|
17 |
def generate_audio(text, filename):
|
18 |
tts = gTTS(text=text, lang="en")
|
19 |
tts.save(filename)
|
20 |
|
21 |
-
# Generate all prompts before starting
|
22 |
generate_audio("Welcome to Biryani Hub.", "static/welcome.mp3")
|
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 |
@app.route("/")
|
28 |
def home():
|
29 |
return render_template("index.html")
|
@@ -38,8 +42,10 @@ def process_audio():
|
|
38 |
audio_file.save(audio_path)
|
39 |
|
40 |
try:
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
except Exception as e:
|
44 |
return jsonify({"error": str(e)}), 500
|
45 |
|
|
|
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 (English Only)
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=0 if device == "cuda" else -1)
|
15 |
|
16 |
+
# Function to generate and save voice prompts in English
|
17 |
def generate_audio(text, filename):
|
18 |
tts = gTTS(text=text, lang="en")
|
19 |
tts.save(filename)
|
20 |
|
21 |
+
# Generate all voice prompts before starting
|
22 |
generate_audio("Welcome to Biryani Hub.", "static/welcome.mp3")
|
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 |
+
# Function to clean text and remove non-English characters
|
28 |
+
def clean_text(text):
|
29 |
+
return re.sub(r'[^a-zA-Z0-9@.\s]', '', text) # Allow only English letters, numbers, @, and spaces
|
30 |
+
|
31 |
@app.route("/")
|
32 |
def home():
|
33 |
return render_template("index.html")
|
|
|
42 |
audio_file.save(audio_path)
|
43 |
|
44 |
try:
|
45 |
+
# Force Whisper to transcribe in English only
|
46 |
+
text = speech_to_text(audio_path, generate_kwargs={"language": "en"})["text"]
|
47 |
+
cleaned_text = clean_text(text) # Clean non-English characters
|
48 |
+
return jsonify({"text": cleaned_text})
|
49 |
except Exception as e:
|
50 |
return jsonify({"error": str(e)}), 500
|
51 |
|