File size: 1,388 Bytes
69067ae
adb5e2a
69067ae
 
0197ed3
adb5e2a
0197ed3
69067ae
 
 
 
7494646
 
 
69067ae
 
 
adb5e2a
 
69067ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7494646
69067ae
 
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
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

app = Flask(__name__)
recognizer = sr.Recognizer()

# Load Hugging Face Whisper Model
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 convert text to speech
def generate_audio(text, filename="static/output.mp3"):
    tts = gTTS(text=text, lang="en")
    tts.save(filename)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/get_prompt")
def get_prompt():
    generate_audio("Welcome to Biryani Hub. Please tell me your name.", "static/welcome.mp3")
    return jsonify({"audio_url": "/static/welcome.mp3"})

@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)["text"]
        return jsonify({"text": 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)