Tri4 commited on
Commit
d420f32
·
verified ·
1 Parent(s): 52ffc48

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +100 -0
main.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from datetime import datetime
3
+ import whisper
4
+ import os
5
+ import time
6
+
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")
49
+ return jsonify({"error": "No audio file provided"}), 400
50
+
51
+ audio_file = request.files['audio']
52
+ audio_file_size = len(audio_file.read()) # Calculate the size of the file in bytes
53
+ audio_file.seek(0) # Reset the file pointer after reading
54
+
55
+ # Save the uploaded audio file
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()
63
+
64
+ # Transcribe the 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
72
+ transcription_end_time = time.time()
73
+ transcription_duration = transcription_end_time - transcription_start_time
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,
87
+ "request_received_time": request_received_time.isoformat(),
88
+ "transcription_duration": transcription_duration,
89
+ "response_sent_time": response_sent_time.isoformat(),
90
+ "audio_file_size_bytes": audio_file_size
91
+ }), 200
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)