Tri4 commited on
Commit
d8bbc70
·
verified ·
1 Parent(s): 93157c8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -13
main.py CHANGED
@@ -13,9 +13,9 @@ def hello():
13
  return "Semabox, listens to you!"
14
 
15
  # Load the Whisper model
16
- print("Loading Whisper model...", flush=True)
17
  model = whisper.load_model("tiny")
18
- print("Whisper model loaded.", flush=True)
19
 
20
  # Get time of request
21
  def get_time():
@@ -31,36 +31,36 @@ def get_time():
31
 
32
 
33
  def transcribe(audio_path):
34
- print(f"Transcribing audio from: {audio_path}", flush=True)
35
 
36
  # Load audio and pad/trim it to fit 30 seconds
37
- print("Loading and processing audio...", flush=True)
38
  audio = whisper.load_audio(audio_path)
39
  audio = whisper.pad_or_trim(audio)
40
 
41
  # Make log-Mel spectrogram and move to the same device as the model
42
- print("Creating log-Mel spectrogram...", flush=True)
43
  mel = whisper.log_mel_spectrogram(audio).to(model.device)
44
 
45
  # Detect the spoken language
46
  print("Detecting language...", flush=True)
47
  _, probs = model.detect_language(mel)
48
  language = max(probs, key=probs.get)
49
- print(f"Detected language: {language}", flush=True)
50
 
51
  # Decode the audio
52
  print("Decoding audio...", flush=True)
53
  options = whisper.DecodingOptions(fp16=False)
54
  result = whisper.decode(model, mel, options)
55
 
56
- print("Transcription complete.", flush=True)
57
  return result.text, language
58
 
59
  @app.route('/transcribe', methods=['POST'])
60
  def transcribe_audio():
61
  # Record the time when the request was received
62
  request_received_time, _ = get_time()
63
- print(f"Received request at /transcribe at {request_received_time}", flush=True)
64
 
65
  if 'audio' not in request.files:
66
  print("Error: No audio file provided", flush=True)
@@ -74,7 +74,7 @@ def transcribe_audio():
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} bytes)", flush=True)
78
 
79
  # Record the time before starting transcription
80
  transcription_start_time = time.time()
@@ -92,13 +92,13 @@ def transcribe_audio():
92
 
93
  # Clean up the saved file
94
  os.remove(audio_path)
95
- print(f"Audio file removed from: {audio_path}", flush=True)
96
 
97
  # Record the time when the response is being sent
98
  response_sent_time, _ = get_time()
99
 
100
  # Return the transcription, detected language, and timing information
101
- print(f"Transcription: {transcription}, Language: {language}", flush=True)
102
  return jsonify({
103
  "transcription": transcription,
104
  "language": language,
@@ -110,9 +110,9 @@ def transcribe_audio():
110
 
111
  @app.route('/healthcheck', methods=['GET'])
112
  def healthcheck():
113
- print("Received request at /healthcheck", flush=True)
114
  return jsonify({"status": "API is running"}), 200
115
 
116
  if __name__ == '__main__':
117
- print("Starting Flask app...", flush=True)
118
  app.run(host="0.0.0.0", port=5000)
 
13
  return "Semabox, listens to you!"
14
 
15
  # Load the Whisper model
16
+ print("Loading Whisper model...\n", flush=True)
17
  model = whisper.load_model("tiny")
18
+ print("Whisper model loaded.\n", flush=True)
19
 
20
  # Get time of request
21
  def get_time():
 
31
 
32
 
33
  def transcribe(audio_path):
34
+ print(f"Transcribing audio from: {audio_path}\n", flush=True)
35
 
36
  # Load audio and pad/trim it to fit 30 seconds
37
+ print("Loading and processing audio...\n", flush=True)
38
  audio = whisper.load_audio(audio_path)
39
  audio = whisper.pad_or_trim(audio)
40
 
41
  # Make log-Mel spectrogram and move to the same device as the model
42
+ print("Creating log-Mel spectrogram...\n", flush=True)
43
  mel = whisper.log_mel_spectrogram(audio).to(model.device)
44
 
45
  # Detect the spoken language
46
  print("Detecting language...", flush=True)
47
  _, probs = model.detect_language(mel)
48
  language = max(probs, key=probs.get)
49
+ print(f"Detected language: {language}\n", flush=True)
50
 
51
  # Decode the audio
52
  print("Decoding audio...", flush=True)
53
  options = whisper.DecodingOptions(fp16=False)
54
  result = whisper.decode(model, mel, options)
55
 
56
+ print("Transcription complete.\n", flush=True)
57
  return result.text, language
58
 
59
  @app.route('/transcribe', methods=['POST'])
60
  def transcribe_audio():
61
  # Record the time when the request was received
62
  request_received_time, _ = get_time()
63
+ print(f"Received request at /transcribe at {request_received_time}\n", flush=True)
64
 
65
  if 'audio' not in request.files:
66
  print("Error: No audio file provided", flush=True)
 
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} bytes)\n", flush=True)
78
 
79
  # Record the time before starting transcription
80
  transcription_start_time = time.time()
 
92
 
93
  # Clean up the saved file
94
  os.remove(audio_path)
95
+ print(f"Audio file removed from: {audio_path}\n", flush=True)
96
 
97
  # Record the time when the response is being sent
98
  response_sent_time, _ = get_time()
99
 
100
  # Return the transcription, detected language, and timing information
101
+ print(f"Transcription: {transcription}, Language: {language}\n", flush=True)
102
  return jsonify({
103
  "transcription": transcription,
104
  "language": language,
 
110
 
111
  @app.route('/healthcheck', methods=['GET'])
112
  def healthcheck():
113
+ print("Received request at /healthcheck\n", flush=True)
114
  return jsonify({"status": "API is running"}), 200
115
 
116
  if __name__ == '__main__':
117
+ print("Starting Flask app...\n", flush=True)
118
  app.run(host="0.0.0.0", port=5000)