Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import whisper
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the Whisper model
|
| 8 |
+
model = whisper.load_model("small")
|
| 9 |
+
|
| 10 |
+
def transcribe(audio_path):
|
| 11 |
+
# Load audio and pad/trim it to fit 30 seconds
|
| 12 |
+
audio = whisper.load_audio(audio_path)
|
| 13 |
+
audio = whisper.pad_or_trim(audio)
|
| 14 |
+
|
| 15 |
+
# Make log-Mel spectrogram and move to the same device as the model
|
| 16 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 17 |
+
|
| 18 |
+
# Detect the spoken language
|
| 19 |
+
_, probs = model.detect_language(mel)
|
| 20 |
+
language = max(probs, key=probs.get)
|
| 21 |
+
|
| 22 |
+
# Decode the audio
|
| 23 |
+
options = whisper.DecodingOptions(fp16=False)
|
| 24 |
+
result = whisper.decode(model, mel, options)
|
| 25 |
+
|
| 26 |
+
return result.text, language
|
| 27 |
+
|
| 28 |
+
@app.route('/transcribe', methods=['POST'])
|
| 29 |
+
def transcribe_audio():
|
| 30 |
+
if 'audio' not in request.files:
|
| 31 |
+
return jsonify({"error": "No audio file provided"}), 400
|
| 32 |
+
|
| 33 |
+
audio_file = request.files['audio']
|
| 34 |
+
|
| 35 |
+
# Save the uploaded audio file
|
| 36 |
+
audio_path = os.path.join("temp_audio", audio_file.filename)
|
| 37 |
+
os.makedirs("temp_audio", exist_ok=True)
|
| 38 |
+
audio_file.save(audio_path)
|
| 39 |
+
|
| 40 |
+
# Transcribe the audio
|
| 41 |
+
try:
|
| 42 |
+
transcription, language = transcribe(audio_path)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return jsonify({"error": f"An error occurred: {str(e)}"}), 500
|
| 45 |
+
|
| 46 |
+
# Clean up the saved file
|
| 47 |
+
os.remove(audio_path)
|
| 48 |
+
|
| 49 |
+
# Return the transcription and detected language
|
| 50 |
+
return jsonify({"transcription": transcription, "language": language}), 200
|
| 51 |
+
|
| 52 |
+
@app.route('/healthcheck', methods=['GET'])
|
| 53 |
+
def healthcheck():
|
| 54 |
+
return jsonify({"status": "API is running"}), 200
|
| 55 |
+
|
| 56 |
+
if __name__ == '__main__':
|
| 57 |
+
app.run(host="0.0.0.0", port=5000)
|