from flask import Flask, request, jsonify, send_from_directory from faster_whisper import WhisperModel import tempfile, os, subprocess app = Flask(__name__) # Load Whisper model once on startup model = WhisperModel("tiny", device="cpu") # --- HTML served directly --- HTML_PAGE = """ Simple STT with 20s Timeout

🎙️ Speech to Text (Whisper Local)

Press the button to record up to 20s of audio.
""" @app.get("/") def index(): return HTML_PAGE @app.post("/transcribe") def transcribe_audio(): try: with tempfile.NamedTemporaryFile(delete=False, suffix=".webm") as tmp: tmp.write(request.data) tmp_path = tmp.name wav_path = tmp_path.replace(".webm", ".wav") subprocess.run( ["ffmpeg", "-y", "-i", tmp_path, "-ar", "16000", "-ac", "1", wav_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) segments, info = model.transcribe(wav_path, beam_size=1) text = " ".join([seg.text for seg in segments]).strip() os.remove(tmp_path) os.remove(wav_path) return jsonify({"text": text, "language": info.language}) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)