semalab / main.py
Tri4's picture
Create main.py
2809a56 verified
raw
history blame
1.36 kB
from flask import Flask, request, jsonify, abort
import whisper
import os
from flask_cors import CORS
from tempfile import NamedTemporaryFile
# Load Whisper model
print("\nLoading Whisper\n", flush=True)
model = whisper.load_model("small")
# Initialize Flask app
app = Flask(__name__)
CORS(app)
print("\nHello, welcome to SemaBox\n", flush=True)
@app.route("/")
def hello():
return "Semabox, listens to you!"
@app.route('/whisper', methods=['POST'])
def transcribe_audio():
if 'audio' not in request.files:
# If no audio file is submitted, return a 400 (Bad Request) error.
abort(400, description="No audio file provided")
audio_file = request.files['audio']
# Create a temporary file to save the uploaded audio.
with NamedTemporaryFile(suffix=".wav", delete=True) as temp:
audio_file.save(temp.name)
# Perform transcription using the Whisper model.
result = model.transcribe(temp.name)
# Return the transcribed text in JSON format.
return jsonify({
'filename': audio_file.filename,
'transcript': result['text'],
})
# Healthcheck endpoint
@app.route('/healthcheck', methods=['GET'])
def healthcheck():
return jsonify({"status": "API is running"}), 200
# Run the Flask app
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)