|
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() |
|
|
|
|
|
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) |
|
|
|
|
|
def generate_audio(text, filename): |
|
tts = gTTS(text=text, lang="en") |
|
tts.save(filename) |
|
|
|
|
|
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") |
|
|
|
|
|
def clean_text(text): |
|
return re.sub(r'[^a-zA-Z0-9@.\s]', '', text) |
|
|
|
@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: |
|
|
|
text = speech_to_text(audio_path, generate_kwargs={"language": "en"})["text"] |
|
cleaned_text = clean_text(text) |
|
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) |
|
|