Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, abort
|
2 |
+
import whisper
|
3 |
+
import os
|
4 |
+
|
5 |
+
from flask_cors import CORS
|
6 |
+
from tempfile import NamedTemporaryFile
|
7 |
+
|
8 |
+
# Load Whisper model
|
9 |
+
print("\nLoading Whisper\n", flush=True)
|
10 |
+
model = whisper.load_model("small")
|
11 |
+
|
12 |
+
# Initialize Flask app
|
13 |
+
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!"
|
20 |
+
|
21 |
+
@app.route('/whisper', methods=['POST'])
|
22 |
+
def transcribe_audio():
|
23 |
+
if 'audio' not in request.files:
|
24 |
+
# If no audio file is submitted, return a 400 (Bad Request) error.
|
25 |
+
abort(400, description="No audio file provided")
|
26 |
+
|
27 |
+
audio_file = request.files['audio']
|
28 |
+
|
29 |
+
# Create a temporary file to save the uploaded audio.
|
30 |
+
with NamedTemporaryFile(suffix=".wav", delete=True) as temp:
|
31 |
+
audio_file.save(temp.name)
|
32 |
+
|
33 |
+
# Perform transcription using the Whisper model.
|
34 |
+
result = model.transcribe(temp.name)
|
35 |
+
|
36 |
+
# Return the transcribed text in JSON format.
|
37 |
+
return jsonify({
|
38 |
+
'filename': audio_file.filename,
|
39 |
+
'transcript': result['text'],
|
40 |
+
})
|
41 |
+
|
42 |
+
# Healthcheck endpoint
|
43 |
+
@app.route('/healthcheck', methods=['GET'])
|
44 |
+
def healthcheck():
|
45 |
+
return jsonify({"status": "API is running"}), 200
|
46 |
+
|
47 |
+
# Run the Flask app
|
48 |
+
if __name__ == '__main__':
|
49 |
+
app.run(host="0.0.0.0", port=5000)
|