File size: 1,672 Bytes
bf00a95
8b9c4e4
70324fc
8b9c4e4
822d179
 
 
1bb9b5d
15833f6
074798d
7b910fd
822d179
ee3e7dd
 
 
27407e5
1bb9b5d
dcdfd1b
7b910fd
1bb9b5d
7b910fd
4bc235e
8b9c4e4
 
 
 
27407e5
 
8b9c4e4
 
 
 
 
27407e5
8b9c4e4
 
 
 
 
 
7b910fd
 
1caf542
8b9c4e4
7b910fd
1caf542
8b9c4e4
1caf542
43cd7bc
1caf542
43cd7bc
8b9c4e4
1caf542
 
 
 
8b9c4e4
1caf542
a41efc2
7b910fd
4bc235e
1bb9b5d
27407e5
 
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
from flask import Flask, request
from gradio_client import Client
from uuid import uuid4
import os, json, requests, hashlib

app = Flask(__name__)

secret = os.environ.get("auth")
headers = {'Authorization': os.environ.get("auth")}

res_dict = {}

@app.route('/queue', methods=['POST'])
def queue():
    if request.json.get('secret') != secret:
        return {}, 400
        
    uuid = str(uuid4())
    res_dict[uuid] = request.json
    
    return { 'uuid': uuid }, 200


@app.route('/6asr', methods=['POST'])
def sixasr():
    data = res_dict.get(request.json.get('uuid'))
    if not data:
        return {}, 400

    base64 = request.json.get('base64')
    dialectCode = request.json.get('dialectCode')
    if not base64 or not dialectCode:
        return {}, 400
    
    if hashlib.sha256(base64.encode('utf-8')).hexdigest() != data.get("hash"):
        return {}, 400

    URL = 'https://nuu-lab-gradio-6asr-whisper.hf.space/run/predict'
    body = {'data': ["whisper-large-v3", f"htia_{dialectCode}", None, base64, '']}

    res = requests.post(URL, json=body, headers=headers)
    if res.status_code == 200:
        res_json = res.json()

        callbackURL = data.get('callbackURL')
        callback_body = data.get('callbackBody')

        if res_json.get('data'):
            callback_body['length'] = len(res_json['data'][0])
        if res_json.get('srt'):
            callback_body['srt'] = res_json['srt'][0]

        requests.post(callbackURL, json=callback_body)

        print(callback_body)
        print(res_json)

        return res_json

    return {'error': '請求失敗'}, 500


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860)