speech-to-text / app.py
Nechba's picture
Update app.py
8ec66b1 verified
raw
history blame
906 Bytes
from flask import Flask, request, jsonify
import whisper
import io
app = Flask(__name__)
# Load Whisper model once for efficiency
model = whisper.load_model("small")
@app.route('/transcribe', methods=['POST'])
def transcribe():
try:
# Read raw bytes from request
audio_bytes = request.data
if not audio_bytes:
return jsonify({"error": "No audio data provided"}), 400
# Save bytes to a temporary file (required for Whisper)
temp_audio_path = "temp_audio.wav"
with open(temp_audio_path, "wb") as f:
f.write(audio_bytes)
# Transcribe using Whisper
result = model.transcribe(temp_audio_path)
return jsonify({"text": result["text"]})
except Exception as e:
print("Error:", str(e)) # Log error for debugging
return jsonify({"error": "Internal Server Error", "details": str(e)}), 500