Spaces:
Sleeping
Sleeping
File size: 906 Bytes
c2864d3 2ea3a36 419ab6f fcf5834 c2864d3 2ea3a36 8ec66b1 2ea3a36 5020140 68d753f 8ec66b1 9bb1bc6 68d753f 8ec66b1 fa58d25 8ec66b1 fa58d25 68d753f 2ea3a36 68d753f 9bb1bc6 8ec66b1 |
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 |
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 |