geethareddy commited on
Commit
7467739
·
verified ·
1 Parent(s): 7a7aab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
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 (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")
34
 
35
- @app.route("/process_audio", methods=["POST"])
36
- def process_audio():
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/temp.wav"
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
 
52
  if __name__ == "__main__":
53
- app.run(host="0.0.0.0", port=7860, debug=True)
 
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)