Update main.py
Browse files
main.py
CHANGED
@@ -5,29 +5,41 @@ import os
|
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
# Load the Whisper model
|
8 |
-
|
|
|
|
|
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']
|
@@ -36,22 +48,28 @@ def transcribe_audio():
|
|
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)
|
|
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
# Load the Whisper model
|
8 |
+
print("Loading Whisper model...")
|
9 |
+
model = whisper.load_model("tiny")
|
10 |
+
print("Whisper model loaded.")
|
11 |
|
12 |
def transcribe(audio_path):
|
13 |
+
print(f"Transcribing audio from: {audio_path}")
|
14 |
+
|
15 |
# Load audio and pad/trim it to fit 30 seconds
|
16 |
+
print("Loading and processing audio...")
|
17 |
audio = whisper.load_audio(audio_path)
|
18 |
audio = whisper.pad_or_trim(audio)
|
19 |
|
20 |
# Make log-Mel spectrogram and move to the same device as the model
|
21 |
+
print("Creating log-Mel spectrogram...")
|
22 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
23 |
|
24 |
# Detect the spoken language
|
25 |
+
print("Detecting language...")
|
26 |
_, probs = model.detect_language(mel)
|
27 |
language = max(probs, key=probs.get)
|
28 |
+
print(f"Detected language: {language}")
|
29 |
+
|
30 |
# Decode the audio
|
31 |
+
print("Decoding audio...")
|
32 |
options = whisper.DecodingOptions(fp16=False)
|
33 |
result = whisper.decode(model, mel, options)
|
34 |
|
35 |
+
print("Transcription complete.")
|
36 |
return result.text, language
|
37 |
|
38 |
@app.route('/transcribe', methods=['POST'])
|
39 |
def transcribe_audio():
|
40 |
+
print("Received request at /transcribe")
|
41 |
if 'audio' not in request.files:
|
42 |
+
print("Error: No audio file provided")
|
43 |
return jsonify({"error": "No audio file provided"}), 400
|
44 |
|
45 |
audio_file = request.files['audio']
|
|
|
48 |
audio_path = os.path.join("temp_audio", audio_file.filename)
|
49 |
os.makedirs("temp_audio", exist_ok=True)
|
50 |
audio_file.save(audio_path)
|
51 |
+
print(f"Audio file saved to: {audio_path}")
|
52 |
|
53 |
# Transcribe the audio
|
54 |
try:
|
55 |
transcription, language = transcribe(audio_path)
|
56 |
except Exception as e:
|
57 |
+
print(f"Error during transcription: {str(e)}")
|
58 |
return jsonify({"error": f"An error occurred: {str(e)}"}), 500
|
59 |
|
60 |
# Clean up the saved file
|
61 |
os.remove(audio_path)
|
62 |
+
print(f"Audio file removed from: {audio_path}")
|
63 |
|
64 |
# Return the transcription and detected language
|
65 |
+
print(f"Transcription: {transcription}, Language: {language}")
|
66 |
return jsonify({"transcription": transcription, "language": language}), 200
|
67 |
|
68 |
@app.route('/healthcheck', methods=['GET'])
|
69 |
def healthcheck():
|
70 |
+
print("Received request at /healthcheck")
|
71 |
return jsonify({"status": "API is running"}), 200
|
72 |
|
73 |
if __name__ == '__main__':
|
74 |
+
print("Starting Flask app...")
|
75 |
app.run(host="0.0.0.0", port=5000)
|