Update main.py
Browse files
main.py
CHANGED
|
@@ -5,7 +5,6 @@ import time
|
|
| 5 |
import pytz
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
@app.route("/")
|
|
@@ -29,7 +28,15 @@ def get_time():
|
|
| 29 |
full_date = f"{curr_day} | {curr_date} | {curr_time}"
|
| 30 |
return full_date, curr_time
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def transcribe(audio_path):
|
| 34 |
print(f"Transcribing audio from: {audio_path}\n", flush=True)
|
| 35 |
|
|
@@ -67,14 +74,15 @@ def transcribe_audio():
|
|
| 67 |
return jsonify({"error": "No audio file provided"}), 400
|
| 68 |
|
| 69 |
audio_file = request.files['audio']
|
| 70 |
-
|
| 71 |
audio_file.seek(0) # Reset the file pointer after reading
|
|
|
|
| 72 |
|
| 73 |
# Save the uploaded audio file
|
| 74 |
audio_path = os.path.join("temp_audio", audio_file.filename)
|
| 75 |
os.makedirs("temp_audio", exist_ok=True)
|
| 76 |
audio_file.save(audio_path)
|
| 77 |
-
print(f"Audio file saved to: {audio_path} (Size: {audio_file_size}
|
| 78 |
|
| 79 |
# Record the time before starting transcription
|
| 80 |
transcription_start_time = time.time()
|
|
@@ -88,7 +96,7 @@ def transcribe_audio():
|
|
| 88 |
|
| 89 |
# Calculate the time taken for transcription
|
| 90 |
transcription_end_time = time.time()
|
| 91 |
-
transcription_duration = transcription_end_time - transcription_start_time
|
| 92 |
|
| 93 |
# Clean up the saved file
|
| 94 |
os.remove(audio_path)
|
|
@@ -103,9 +111,9 @@ def transcribe_audio():
|
|
| 103 |
"transcription": transcription,
|
| 104 |
"language": language,
|
| 105 |
"request_received_time": request_received_time,
|
| 106 |
-
"
|
| 107 |
"response_sent_time": response_sent_time,
|
| 108 |
-
"
|
| 109 |
}), 200
|
| 110 |
|
| 111 |
@app.route('/healthcheck', methods=['GET'])
|
|
|
|
| 5 |
import pytz
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
@app.route("/")
|
|
|
|
| 28 |
full_date = f"{curr_day} | {curr_date} | {curr_time}"
|
| 29 |
return full_date, curr_time
|
| 30 |
|
| 31 |
+
# Convert file size from bytes to KB or MB
|
| 32 |
+
def convert_size(bytes):
|
| 33 |
+
if bytes < 1024:
|
| 34 |
+
return f"{bytes} bytes"
|
| 35 |
+
elif bytes < 1024**2:
|
| 36 |
+
return f"{bytes / 1024:.2f} KB"
|
| 37 |
+
else:
|
| 38 |
+
return f"{bytes / 1024**2:.2f} MB"
|
| 39 |
+
|
| 40 |
def transcribe(audio_path):
|
| 41 |
print(f"Transcribing audio from: {audio_path}\n", flush=True)
|
| 42 |
|
|
|
|
| 74 |
return jsonify({"error": "No audio file provided"}), 400
|
| 75 |
|
| 76 |
audio_file = request.files['audio']
|
| 77 |
+
audio_file_size_bytes = len(audio_file.read()) # Calculate the size of the file in bytes
|
| 78 |
audio_file.seek(0) # Reset the file pointer after reading
|
| 79 |
+
audio_file_size = convert_size(audio_file_size_bytes) # Convert file size to KB or MB
|
| 80 |
|
| 81 |
# Save the uploaded audio file
|
| 82 |
audio_path = os.path.join("temp_audio", audio_file.filename)
|
| 83 |
os.makedirs("temp_audio", exist_ok=True)
|
| 84 |
audio_file.save(audio_path)
|
| 85 |
+
print(f"Audio file saved to: {audio_path} (Size: {audio_file_size})\n", flush=True)
|
| 86 |
|
| 87 |
# Record the time before starting transcription
|
| 88 |
transcription_start_time = time.time()
|
|
|
|
| 96 |
|
| 97 |
# Calculate the time taken for transcription
|
| 98 |
transcription_end_time = time.time()
|
| 99 |
+
transcription_duration = round(transcription_end_time - transcription_start_time, 2)
|
| 100 |
|
| 101 |
# Clean up the saved file
|
| 102 |
os.remove(audio_path)
|
|
|
|
| 111 |
"transcription": transcription,
|
| 112 |
"language": language,
|
| 113 |
"request_received_time": request_received_time,
|
| 114 |
+
"transcription_duration_seconds": transcription_duration,
|
| 115 |
"response_sent_time": response_sent_time,
|
| 116 |
+
"audio_file_size": audio_file_size
|
| 117 |
}), 200
|
| 118 |
|
| 119 |
@app.route('/healthcheck', methods=['GET'])
|