Update main.py
Browse files
main.py
CHANGED
@@ -7,42 +7,46 @@ import time
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
|
|
|
|
|
|
|
|
10 |
# Load the Whisper model
|
11 |
-
print("Loading Whisper model...")
|
12 |
model = whisper.load_model("tiny")
|
13 |
-
print("Whisper model loaded.")
|
14 |
|
15 |
def transcribe(audio_path):
|
16 |
-
print(f"Transcribing audio from: {audio_path}")
|
17 |
|
18 |
# Load audio and pad/trim it to fit 30 seconds
|
19 |
-
print("Loading and processing audio...")
|
20 |
audio = whisper.load_audio(audio_path)
|
21 |
audio = whisper.pad_or_trim(audio)
|
22 |
|
23 |
# Make log-Mel spectrogram and move to the same device as the model
|
24 |
-
print("Creating log-Mel spectrogram...")
|
25 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
26 |
|
27 |
# Detect the spoken language
|
28 |
-
print("Detecting language...")
|
29 |
_, probs = model.detect_language(mel)
|
30 |
language = max(probs, key=probs.get)
|
31 |
-
print(f"Detected language: {language}")
|
32 |
|
33 |
# Decode the audio
|
34 |
-
print("Decoding audio...")
|
35 |
options = whisper.DecodingOptions(fp16=False)
|
36 |
result = whisper.decode(model, mel, options)
|
37 |
|
38 |
-
print("Transcription complete.")
|
39 |
return result.text, language
|
40 |
|
41 |
@app.route('/transcribe', methods=['POST'])
|
42 |
def transcribe_audio():
|
43 |
# Record the time when the request was received
|
44 |
request_received_time = datetime.now()
|
45 |
-
print(f"Received request at /transcribe at {request_received_time}")
|
46 |
|
47 |
if 'audio' not in request.files:
|
48 |
print("Error: No audio file provided")
|
@@ -56,7 +60,7 @@ def transcribe_audio():
|
|
56 |
audio_path = os.path.join("temp_audio", audio_file.filename)
|
57 |
os.makedirs("temp_audio", exist_ok=True)
|
58 |
audio_file.save(audio_path)
|
59 |
-
print(f"Audio file saved to: {audio_path} (Size: {audio_file_size} bytes)")
|
60 |
|
61 |
# Record the time before starting transcription
|
62 |
transcription_start_time = time.time()
|
@@ -65,7 +69,7 @@ def transcribe_audio():
|
|
65 |
try:
|
66 |
transcription, language = transcribe(audio_path)
|
67 |
except Exception as e:
|
68 |
-
print(f"Error during transcription: {str(e)}")
|
69 |
return jsonify({"error": f"An error occurred: {str(e)}"}), 500
|
70 |
|
71 |
# Calculate the time taken for transcription
|
@@ -74,13 +78,13 @@ def transcribe_audio():
|
|
74 |
|
75 |
# Clean up the saved file
|
76 |
os.remove(audio_path)
|
77 |
-
print(f"Audio file removed from: {audio_path}")
|
78 |
|
79 |
# Record the time when the response is being sent
|
80 |
response_sent_time = datetime.now()
|
81 |
|
82 |
# Return the transcription, detected language, and timing information
|
83 |
-
print(f"Transcription: {transcription}, Language: {language}")
|
84 |
return jsonify({
|
85 |
"transcription": transcription,
|
86 |
"language": language,
|
@@ -92,9 +96,9 @@ def transcribe_audio():
|
|
92 |
|
93 |
@app.route('/healthcheck', methods=['GET'])
|
94 |
def healthcheck():
|
95 |
-
print("Received request at /healthcheck")
|
96 |
return jsonify({"status": "API is running"}), 200
|
97 |
|
98 |
if __name__ == '__main__':
|
99 |
-
print("Starting Flask app...")
|
100 |
app.run(host="0.0.0.0", port=5000)
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
+
@app.route("/")
|
11 |
+
def hello():
|
12 |
+
return "Semabox, listens to you!"
|
13 |
+
|
14 |
# Load the Whisper model
|
15 |
+
print("Loading Whisper model...", flush=True)
|
16 |
model = whisper.load_model("tiny")
|
17 |
+
print("Whisper model loaded.", flush=True)
|
18 |
|
19 |
def transcribe(audio_path):
|
20 |
+
print(f"Transcribing audio from: {audio_path}", flush=True)
|
21 |
|
22 |
# Load audio and pad/trim it to fit 30 seconds
|
23 |
+
print("Loading and processing audio...", flush=True)
|
24 |
audio = whisper.load_audio(audio_path)
|
25 |
audio = whisper.pad_or_trim(audio)
|
26 |
|
27 |
# Make log-Mel spectrogram and move to the same device as the model
|
28 |
+
print("Creating log-Mel spectrogram...", flush=True)
|
29 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
30 |
|
31 |
# Detect the spoken language
|
32 |
+
print("Detecting language...", flush=True)
|
33 |
_, probs = model.detect_language(mel)
|
34 |
language = max(probs, key=probs.get)
|
35 |
+
print(f"Detected language: {language}", flush=True)
|
36 |
|
37 |
# Decode the audio
|
38 |
+
print("Decoding audio...", flush=True)
|
39 |
options = whisper.DecodingOptions(fp16=False)
|
40 |
result = whisper.decode(model, mel, options)
|
41 |
|
42 |
+
print("Transcription complete.", flush=True)
|
43 |
return result.text, language
|
44 |
|
45 |
@app.route('/transcribe', methods=['POST'])
|
46 |
def transcribe_audio():
|
47 |
# Record the time when the request was received
|
48 |
request_received_time = datetime.now()
|
49 |
+
print(f"Received request at /transcribe at {request_received_time}", flush=True)
|
50 |
|
51 |
if 'audio' not in request.files:
|
52 |
print("Error: No audio file provided")
|
|
|
60 |
audio_path = os.path.join("temp_audio", audio_file.filename)
|
61 |
os.makedirs("temp_audio", exist_ok=True)
|
62 |
audio_file.save(audio_path)
|
63 |
+
print(f"Audio file saved to: {audio_path} (Size: {audio_file_size} bytes)", flush=True)
|
64 |
|
65 |
# Record the time before starting transcription
|
66 |
transcription_start_time = time.time()
|
|
|
69 |
try:
|
70 |
transcription, language = transcribe(audio_path)
|
71 |
except Exception as e:
|
72 |
+
print(f"Error during transcription: {str(e)}", flush=True)
|
73 |
return jsonify({"error": f"An error occurred: {str(e)}"}), 500
|
74 |
|
75 |
# Calculate the time taken for transcription
|
|
|
78 |
|
79 |
# Clean up the saved file
|
80 |
os.remove(audio_path)
|
81 |
+
print(f"Audio file removed from: {audio_path}", flush=True)
|
82 |
|
83 |
# Record the time when the response is being sent
|
84 |
response_sent_time = datetime.now()
|
85 |
|
86 |
# Return the transcription, detected language, and timing information
|
87 |
+
print(f"Transcription: {transcription}, Language: {language}", flush=True)
|
88 |
return jsonify({
|
89 |
"transcription": transcription,
|
90 |
"language": language,
|
|
|
96 |
|
97 |
@app.route('/healthcheck', methods=['GET'])
|
98 |
def healthcheck():
|
99 |
+
print("Received request at /healthcheck", flush=True)
|
100 |
return jsonify({"status": "API is running"}), 200
|
101 |
|
102 |
if __name__ == '__main__':
|
103 |
+
print("Starting Flask app...", flush=True)
|
104 |
app.run(host="0.0.0.0", port=5000)
|