Update main.py
Browse files
main.py
CHANGED
@@ -14,6 +14,26 @@ app = Flask(__name__)
|
|
14 |
CORS(app)
|
15 |
print("\nHello, welcome to SemaBox\n", flush=True)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@app.route("/")
|
18 |
def hello():
|
19 |
return "Semabox, listens to you!"
|
@@ -39,6 +59,29 @@ def transcribe_audio():
|
|
39 |
'transcript': result['text'],
|
40 |
})
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# Healthcheck endpoint
|
43 |
@app.route('/healthcheck', methods=['GET'])
|
44 |
def healthcheck():
|
|
|
14 |
CORS(app)
|
15 |
print("\nHello, welcome to SemaBox\n", flush=True)
|
16 |
|
17 |
+
|
18 |
+
def transcribe(audio):
|
19 |
+
|
20 |
+
#time.sleep(3)
|
21 |
+
# load audio and pad/trim it to fit 30 seconds
|
22 |
+
audio = whisper.load_audio(audio)
|
23 |
+
audio = whisper.pad_or_trim(audio)
|
24 |
+
|
25 |
+
# make log-Mel spectrogram and move to the same device as the model
|
26 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
27 |
+
|
28 |
+
# detect the spoken language
|
29 |
+
_, probs = model.detect_language(mel)
|
30 |
+
print(f"Detected language: {max(probs, key=probs.get)}")
|
31 |
+
|
32 |
+
# decode the audio
|
33 |
+
options = whisper.DecodingOptions(fp16 = False)
|
34 |
+
result = whisper.decode(model, mel, options)
|
35 |
+
return result.text
|
36 |
+
|
37 |
@app.route("/")
|
38 |
def hello():
|
39 |
return "Semabox, listens to you!"
|
|
|
59 |
'transcript': result['text'],
|
60 |
})
|
61 |
|
62 |
+
|
63 |
+
# Define the route for transcription
|
64 |
+
@app.route('/transcribe', methods=['POST'])
|
65 |
+
def transcribe_audio():
|
66 |
+
# Check if an audio file is included in the request
|
67 |
+
if 'audio' not in request.files:
|
68 |
+
return jsonify({"error": "No audio file provided"}), 400
|
69 |
+
|
70 |
+
audio_file = request.files['audio']
|
71 |
+
|
72 |
+
# Save the uploaded audio file
|
73 |
+
audio_path = os.path.join("temp_audio", audio_file.filename)
|
74 |
+
audio_file.save(audio_path)
|
75 |
+
|
76 |
+
# Transcribe the audio
|
77 |
+
transcription, language = transcribe(audio_path)
|
78 |
+
|
79 |
+
# Clean up the saved file
|
80 |
+
os.remove(audio_path)
|
81 |
+
|
82 |
+
# Return the transcription and detected language
|
83 |
+
return jsonify({"transcription": transcription, "language": language}), 200
|
84 |
+
|
85 |
# Healthcheck endpoint
|
86 |
@app.route('/healthcheck', methods=['GET'])
|
87 |
def healthcheck():
|