Spaces:
Build error
Build error
File size: 4,153 Bytes
b35a678 9b360f4 a19cea3 511ffb7 9b360f4 b35a678 0267ef0 b35a678 0267ef0 9b360f4 5cb25c7 a19cea3 9b360f4 511ffb7 b35a678 a19cea3 928afe5 a19cea3 511ffb7 a19cea3 0267ef0 b35a678 6a170e8 511ffb7 a19cea3 f74e1cc a19cea3 6218372 a19cea3 f74e1cc a19cea3 9b360f4 f74e1cc 9b360f4 f74e1cc 9b360f4 511ffb7 0685a97 511ffb7 f74e1cc 511ffb7 f74e1cc 511ffb7 a19cea3 b35a678 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
from asyncio.log import logger
import logging
import os
import whisper
from flask import Flask, jsonify, request
from werkzeug.utils import secure_filename
try:
import gunicorn.app.base
gunicorn_present = True
except ImportError:
logging.exception("gunicorn not installed")
gunicorn_present = False
gunicorn_present = False
STARTING_SIZE = 'small'
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'ogg', 'mp3', 'mp4', 'wav',
'flac', 'm4a', 'aac', 'wma', 'webm', 'opus'}
normal_size = STARTING_SIZE
small_size = 'base'
PORT = 7860
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
model = whisper.load_model(normal_size)
model_en = whisper.load_model(f"{normal_size}.en")
model_small = whisper.load_model(small_size)
model_small_en = whisper.load_model(f"{small_size}.en")
if gunicorn_present:
class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()
def load_config(self):
config = {key: value for key, value in self.options.items()
if key in self.cfg.settings and value is not None}
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
def inference(audio_file, model=model, model_en=model_en):
audio = whisper.load_audio(audio_file)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
lang = max(probs, key=probs.get)
if lang == "en":
_model = model_en
else:
_model = model
result = _model.transcribe(audio_file, fp16=False, language=lang)
segmented_text_list = []
text = result['text']
for segment in result["segments"]:
segmented_text_list.append(
# f'[{segment["start"]:.1f}-{segment["end"]:.1f}] {segment["text"]}')
f'{segment["start"]:.1f}s: {segment["text"]}')
segmented_text = "\n".join(segmented_text_list)
return segmented_text, text
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
return "no file sent!"
uploaded_file = request.files['file']
if uploaded_file.filename == '':
return "no file sent!"
if uploaded_file and allowed_file(uploaded_file.filename):
filename = secure_filename(uploaded_file.filename)
uploaded_file.save(os.path.join(
app.config['UPLOAD_FOLDER'], filename))
timed_transcription, transcription = inference(os.path.join(
app.config['UPLOAD_FOLDER'], filename))
return jsonify({"results": timed_transcription, "text": transcription})
return "nothing yet to see here"
@app.route("/small", methods=['POST'])
def small():
if 'file' not in request.files:
return "no file sent!"
uploaded_file = request.files['file']
if uploaded_file.filename == '':
return "no file sent!"
if uploaded_file and allowed_file(uploaded_file.filename):
filename = secure_filename(uploaded_file.filename)
uploaded_file.save(os.path.join(
app.config['UPLOAD_FOLDER'], filename))
timed_transcription, transcription = inference(os.path.join(
app.config['UPLOAD_FOLDER'], filename), model_small, model_small_en)
return jsonify({"results": timed_transcription, "text": transcription})
if __name__ == "__main__":
options = {
'bind': f'0.0.0.0:{PORT}',
# 'workers': 4,
'timeout': 600,
}
try:
StandaloneApplication(app, options).run()
except:
logging.exception("Error starting server, backing off to debug mode")
app.run(host="0.0.0.0", port=PORT)
|