File size: 2,324 Bytes
6086481
 
32b35a3
8e1f150
0918613
018537d
6086481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3090328
6086481
 
 
 
 
 
 
 
 
 
32b35a3
6086481
e925a0f
32b35a3
6086481
 
 
ba5672b
 
 
0918613
ba5672b
 
 
 
 
a3ed2ae
 
 
 
ba5672b
5f9624d
ba5672b
 
ff284fc
5f9624d
 
018537d
 
6086481
 
32b35a3
02e8372
5f9624d
 
 
ba5672b
6086481
 
 
3090328
 
ba5672b
 
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
from threading import Thread
import redis, time
from flask import Flask, jsonify, send_from_directory, Response, request
from waitress import serve
from flask_cors import CORS, cross_origin
from model import SugoiTranslator
from typing import List


queue_key = "translation_queue"
translated_key = "translated_temp_store"
redis_client = redis.Redis(host='localhost', port=6379, db=0)

def queue_process():
    while True:
        task_list = []
        for _ in range(5):
            task = redis_client.rpop(queue_key) # right pop
            if task: task_list.append(task)
            else: break

        if len(task_list): task_process(task_list)
        time.sleep(0.5)

def task_process(input_text_list: List[str]):
    sugoiTranslator = SugoiTranslator()
    translations = sugoiTranslator.translate(input_text_list)
    for index, translation in enumerate(translations): 
        redis_client.hset(translated_key, input_text_list[index], translation)

def query_translation(input_text: str):
    for _ in range(30):
        translated_text = redis_client.hget(translated_key, input_text)
        if translated_text is not None: 
            redis_client.hdel(translated_key, input_text)
            try: return translated_text.decode('utf-8')
            except AttributeError: return translated_text
        time.sleep(1)




app = Flask(__name__)
CORS(app)

@app.route('/')
def index():
    return send_from_directory('dist', "index.html")

@app.route('/<path:filename>')
def download_file(filename):
    return send_from_directory('.', filename)

@app.route('/assets/<path:filename>')
def download_assets(filename):
    return send_from_directory('dist/assets', filename)

@app.route('/api/translate', methods= ['POST'])
@cross_origin()
def translate_api(): 
    input_text = request.args.get('text')
    if isinstance(input_text, str) and len(input_text) > 0: 
        redis_client.lpush(queue_key, input_text) # left push
        result = query_translation(input_text)
        if result is not None: return jsonify({ "text": result })
        else: return Response(status= 500)
    return Response(status= 400)


if __name__ == '__main__':
    Thread(target=queue_process, daemon=True).start()
    print("Starting server...")
    try: serve(app, port= 7860)
    except BaseException as e: 
        print(e)