File size: 1,306 Bytes
5edbf12
 
 
 
 
 
 
7992f77
b30d9e2
449af86
97afecf
5edbf12
 
 
 
 
 
 
 
 
 
 
 
 
 
7992f77
5edbf12
7992f77
16096c9
7992f77
 
 
5edbf12
 
 
 
 
 
 
 
 
 
 
c16dfda
5edbf12
 
 
 
 
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
from flask import Flask, request
from flask_restful import Api, Resource
from flask_cors import CORS
from waitress import serve
import whisper
import librosa
import numpy as np
import os

os.environ['TRANSFORMERS_CACHE'] = '/.cache/huggingface/hub'
os.environ['SENTENCE_TRANSFORMERS_HOME'] = './.cache'


app = Flask(__name__)
CORS(app)
api = Api()

class Main(Resource):
    def post(self):
        print(request)
        file = request.files["file"]
        if file:
            print(file)

        audio, sr = librosa.load(file.stream)
        print('NO ERROR 1')
        audio_array = np.array(audio, dtype=np.float32)
        print('NO ERROR 2')
        model = whisper.load_model("medium")
        print('NO ERROR 3')
        result = model.transcribe(audio_array, fp16=False)
        print('NO ERROR 4')
        captions = [[(int)(caption["start"] * 1000), (int)(caption["end"] * 1000), caption["text"]] for caption in
                    result['segments']]

        print(f'Отвечаю: {captions}')

        return {'status': 'ok', 'captions': captions}

    def get(self):
        return {'status': 'ok', 'type': 'get'}


api.add_resource(Main, "/")
api.init_app(app)

if __name__ == "__main__":
    # serve(app, port=5000, host="0.0.0.0")
    app.run(debug=True, port=7860, host="0.0.0.0")