Tri4 commited on
Commit
15dce1a
1 Parent(s): 9ff8a4f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import whisper
3
+ import os
4
+
5
+ # Initialize Flask app
6
+ app = Flask(__name__)
7
+
8
+ print("\nHello welcome to SemaBox\n", flush=True)
9
+
10
+ # Load Whisper model
11
+ model = whisper.load_model("small")
12
+
13
+ def transcribe(audio_path):
14
+ # Load audio and pad/trim it to fit 30 seconds
15
+ audio = whisper.load_audio(audio_path)
16
+ audio = whisper.pad_or_trim(audio)
17
+
18
+ # Make log-Mel spectrogram and move to the same device as the model
19
+ mel = whisper.log_mel_spectrogram(audio).to(model.device)
20
+
21
+ # Detect the spoken language
22
+ _, probs = model.detect_language(mel)
23
+ detected_language = max(probs, key=probs.get)
24
+ print(f"Detected language: {detected_language}")
25
+
26
+ # Decode the audio
27
+ options = whisper.DecodingOptions(fp16=False)
28
+ result = whisper.decode(model, mel, options)
29
+ return result.text, detected_language
30
+
31
+ # Define the route for transcription
32
+ @app.route('/transcribe', methods=['POST'])
33
+ def transcribe_audio():
34
+ # Check if an audio file is included in the request
35
+ if 'audio' not in request.files:
36
+ return jsonify({"error": "No audio file provided"}), 400
37
+
38
+ audio_file = request.files['audio']
39
+
40
+ # Save the uploaded audio file
41
+ audio_path = os.path.join("temp_audio", audio_file.filename)
42
+ audio_file.save(audio_path)
43
+
44
+ # Transcribe the audio
45
+ transcription, language = transcribe(audio_path)
46
+
47
+ # Clean up the saved file
48
+ os.remove(audio_path)
49
+
50
+ # Return the transcription and detected language
51
+ return jsonify({"transcription": transcription, "language": language}), 200
52
+
53
+ # Healthcheck endpoint
54
+ @app.route('/healthcheck', methods=['GET'])
55
+ def healthcheck():
56
+ return jsonify({"status": "API is running"}), 200
57
+
58
+ # Run the Flask app
59
+ if __name__ == '__main__':
60
+ # Ensure the temp_audio directory exists
61
+ if not os.path.exists("temp_audio"):
62
+ os.makedirs("temp_audio")
63
+
64
+ app.run(host="0.0.0.0", port=5000)