feat: add redis queue
Browse files- Dockerfile +6 -2
- server/app.py +42 -4
- server/model.py +1 -1
- server/requirements.txt +2 -1
- server/start.sh +7 -0
- src/Home/index.tsx +1 -1
Dockerfile
CHANGED
@@ -22,10 +22,14 @@ WORKDIR /app
|
|
22 |
# Copie o build gerado pelo Node.js para o novo estágio
|
23 |
COPY --from=builder /app/server ./
|
24 |
|
|
|
25 |
RUN pip install --no-cache-dir -r requirements.txt
|
26 |
RUN python setup.py -download
|
27 |
|
28 |
EXPOSE 7860
|
|
|
|
|
29 |
|
30 |
-
# Comando para executar o
|
31 |
-
|
|
|
|
22 |
# Copie o build gerado pelo Node.js para o novo estágio
|
23 |
COPY --from=builder /app/server ./
|
24 |
|
25 |
+
RUN apt-get update && apt-get install -y redis-server && apt-get clean
|
26 |
RUN pip install --no-cache-dir -r requirements.txt
|
27 |
RUN python setup.py -download
|
28 |
|
29 |
EXPOSE 7860
|
30 |
+
# Porta padrão do Redis
|
31 |
+
EXPOSE 6379
|
32 |
|
33 |
+
# Comando para executar o container
|
34 |
+
RUN chmod +x /app/start.sh
|
35 |
+
CMD ["/app/start.sh"]
|
server/app.py
CHANGED
@@ -1,7 +1,41 @@
|
|
|
|
|
|
1 |
from flask import Flask, send_from_directory, json, Response, request
|
2 |
from waitress import serve
|
3 |
from flask_cors import CORS, cross_origin
|
4 |
from model import SugoiTranslator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
app = Flask(__name__)
|
@@ -23,14 +57,18 @@ def download_assets(filename):
|
|
23 |
@cross_origin()
|
24 |
def translate_api():
|
25 |
input_text = request.args.get('text')
|
26 |
-
sugoiTranslator = SugoiTranslator()
|
27 |
if isinstance(input_text, str) and len(input_text) > 0:
|
28 |
-
|
29 |
-
|
|
|
|
|
30 |
return Response(status= 400)
|
31 |
|
32 |
|
33 |
if __name__ == '__main__':
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
|
|
|
1 |
+
from threading import Thread
|
2 |
+
import redis, time
|
3 |
from flask import Flask, send_from_directory, json, Response, request
|
4 |
from waitress import serve
|
5 |
from flask_cors import CORS, cross_origin
|
6 |
from model import SugoiTranslator
|
7 |
+
from typing import List
|
8 |
+
|
9 |
+
|
10 |
+
queue_key = "translation_queue"
|
11 |
+
translated_key = "translated_temp_store"
|
12 |
+
redis_client = redis.Redis(host='localhost', port=6379, db=0)
|
13 |
+
|
14 |
+
def queue_process():
|
15 |
+
while True:
|
16 |
+
task_list = []
|
17 |
+
for _ in range(5):
|
18 |
+
task = redis_client.rpop(queue_key) # right pop
|
19 |
+
if task: task_list.append(task)
|
20 |
+
else: break
|
21 |
+
|
22 |
+
if len(task_list): task_process(task_list)
|
23 |
+
|
24 |
+
def task_process(input_text_list: List[str]):
|
25 |
+
sugoiTranslator = SugoiTranslator()
|
26 |
+
translations = sugoiTranslator.translate(input_text_list)
|
27 |
+
for index, translation in enumerate(translations):
|
28 |
+
redis_client.hset(translated_key, input_text_list[index], translation)
|
29 |
+
|
30 |
+
def query_translation(input_text: str):
|
31 |
+
for _ in range(30):
|
32 |
+
translated_text = redis_client.hget(translated_key, input_text)
|
33 |
+
if translated_text:
|
34 |
+
redis_client.hdel(translated_key, input_text)
|
35 |
+
return translated_text
|
36 |
+
time.sleep(1)
|
37 |
+
|
38 |
+
|
39 |
|
40 |
|
41 |
app = Flask(__name__)
|
|
|
57 |
@cross_origin()
|
58 |
def translate_api():
|
59 |
input_text = request.args.get('text')
|
|
|
60 |
if isinstance(input_text, str) and len(input_text) > 0:
|
61 |
+
redis_client.lpush(queue_key, input_text) # left push
|
62 |
+
result = query_translation(input_text)
|
63 |
+
if result is not None: return json.dumps({ "text": str(result) })
|
64 |
+
else: Response(status= 500)
|
65 |
return Response(status= 400)
|
66 |
|
67 |
|
68 |
if __name__ == '__main__':
|
69 |
+
Thread(target=queue_process, daemon=True).start()
|
70 |
+
print("Starting server...")
|
71 |
+
try: serve(app, port= 7860)
|
72 |
+
except: serve(app, port= 7860)
|
73 |
|
74 |
|
server/model.py
CHANGED
@@ -16,7 +16,7 @@ class SugoiTranslator:
|
|
16 |
self.sp_source_model = os.path.join(modelDir, "spm.ja.nopretok.model")
|
17 |
self.sp_target_model = os.path.join(modelDir, "spm.en.nopretok.model")
|
18 |
# inter_threads: quantas operações independentes podem ser executadas simultaneamente
|
19 |
-
self.translator = ctranslate2.Translator(modelDir, device="cpu", intra_threads=
|
20 |
|
21 |
def tokenizeBatch(self, text):
|
22 |
sp = spm.SentencePieceProcessor(self.sp_source_model)
|
|
|
16 |
self.sp_source_model = os.path.join(modelDir, "spm.ja.nopretok.model")
|
17 |
self.sp_target_model = os.path.join(modelDir, "spm.en.nopretok.model")
|
18 |
# inter_threads: quantas operações independentes podem ser executadas simultaneamente
|
19 |
+
self.translator = ctranslate2.Translator(modelDir, device="cpu", intra_threads=3, inter_threads=2)
|
20 |
|
21 |
def tokenizeBatch(self, text):
|
22 |
sp = spm.SentencePieceProcessor(self.sp_source_model)
|
server/requirements.txt
CHANGED
@@ -3,4 +3,5 @@ waitress
|
|
3 |
flask_cors
|
4 |
huggingface_hub
|
5 |
CTranslate2
|
6 |
-
sentencepiece
|
|
|
|
3 |
flask_cors
|
4 |
huggingface_hub
|
5 |
CTranslate2
|
6 |
+
sentencepiece
|
7 |
+
redis
|
server/start.sh
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# Inicia o Redis em segundo plano
|
4 |
+
service redis-server start
|
5 |
+
|
6 |
+
# Inicia o servidor web (substitua pelo seu comando)
|
7 |
+
python app.py # Ou qualquer outro comando que inicie seu servidor
|
src/Home/index.tsx
CHANGED
@@ -25,7 +25,7 @@ export default function App() {
|
|
25 |
const response = await fetch(`/api/translate?text=${text}`, { method: "POST" }).then(response => {
|
26 |
if (response.status === 200) { return response.json() }
|
27 |
|
28 |
-
}).then(response => response?.
|
29 |
.catch(() => null)
|
30 |
|
31 |
return { originalText: text, translatedText: response }
|
|
|
25 |
const response = await fetch(`/api/translate?text=${text}`, { method: "POST" }).then(response => {
|
26 |
if (response.status === 200) { return response.json() }
|
27 |
|
28 |
+
}).then(response => response?.text)
|
29 |
.catch(() => null)
|
30 |
|
31 |
return { originalText: text, translatedText: response }
|