Spaces:
Runtime error
Runtime error
File size: 1,860 Bytes
69067ae adb5e2a 69067ae 0197ed3 adb5e2a a3e60d6 0197ed3 69067ae a3e60d6 7494646 a3e60d6 8ab530a 69067ae adb5e2a a3e60d6 685e8d2 a3e60d6 69067ae 8ab530a 69067ae 8ab530a 7494646 a3e60d6 7494646 69067ae 0197ed3 69067ae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
from flask import Flask, render_template, request, jsonify
import os
import torch
import speech_recognition as sr
from transformers import pipeline
from gtts import gTTS
import re
app = Flask(__name__)
recognizer = sr.Recognizer()
# Load Whisper Model (English Only)
device = "cuda" if torch.cuda.is_available() else "cpu"
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=0 if device == "cuda" else -1)
# Function to generate and save voice prompts in English
def generate_audio(text, filename):
tts = gTTS(text=text, lang="en")
tts.save(filename)
# Generate all voice prompts before starting
generate_audio("Welcome to Biryani Hub.", "static/welcome.mp3")
generate_audio("Tell me your name.", "static/ask_name.mp3")
generate_audio("Please provide your email.", "static/ask_email.mp3")
generate_audio("Thank you for registration.", "static/thank_you.mp3")
# Function to clean text and remove non-English characters
def clean_text(text):
return re.sub(r'[^a-zA-Z0-9@.\s]', '', text) # Allow only English letters, numbers, @, and spaces
@app.route("/")
def home():
return render_template("index.html")
@app.route("/process_audio", methods=["POST"])
def process_audio():
if "audio" not in request.files:
return jsonify({"error": "No audio file"}), 400
audio_file = request.files["audio"]
audio_path = "static/temp.wav"
audio_file.save(audio_path)
try:
# Force Whisper to transcribe in English only
text = speech_to_text(audio_path, generate_kwargs={"language": "en"})["text"]
cleaned_text = clean_text(text) # Clean non-English characters
return jsonify({"text": cleaned_text})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)
|