forked from zyddnys/manga-image-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoeflow_worker.py
171 lines (138 loc) · 5.22 KB
/
moeflow_worker.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# consume tasks from moeflow job worker with manga-image-translator code
import re
from typing import Any, Awaitable
from celery import Celery
from asgiref.sync import async_to_sync
import manga_translator.detection as detection
import manga_translator.ocr as mit_ocr
import manga_translator.textline_merge as textline_merge
import manga_translator.utils.generic as utils_generic
import manga_translator.utils.textblock as utils_textblock
# FIXME: impl better translator , maybe with Langchain
# FIXME: maybe create a different translators package
import manga_translator.translators as translators
import logging
import json
import os
import dotenv
from PIL import Image
import numpy as np
dotenv.load_dotenv()
BROKER_URL = os.environ.get("CELERY_BROKER_URL")
BACKEND_URL = os.environ.get("CELERY_BACKEND_URL")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
celery_app = Celery(
"manga-image-translator-moeflow-worker",
broker=BROKER_URL,
backend=BACKEND_URL,
result_expires=7 * 24 * 60 * 60, # 7d
)
def to_json(value: object) -> Any:
"""
:return: a json-serizable deep clone of `value`
"""
return json.loads(json.dumps(value, cls=JSONEncoder))
@celery_app.task(name="tasks.mit.detect_text")
def mit_detect_text(path_or_url: str, **kwargs):
logger.debug("Running text segmentation %s %s", path_or_url, kwargs)
result = async_detection(path_or_url, **kwargs)
logger.debug("Running text segmentation result = %s", result)
return result
# OCR + detect_textblocks + merge_textlines
@celery_app.task(name="tasks.mit.ocr")
def mit_ocr(path_or_url: str, **kwargs):
logger.debug("Running OCR %s %s", path_or_url, kwargs)
# for unknown reason async_ocr returns [[Quad]] instead of [result]
textlines = async_ocr(path_or_url, **kwargs)
# return json.loads(json.dumps(result, cls=JSONEncoder)),
logger.debug("Running OCR result = %s", textlines)
img_w, img_h, *_rest = load_rgb_image(path_or_url).shape
min_text_length = kwargs.get("min_text_length", 0)
text_blocks_all: list[utils_textblock.TextBlock] = async_textline_merge(
textlines=textlines, width=img_w, height=img_h
)
# logger.debug("text_blocks_all = %s", text_regions_all)
text_blocks = filter(
lambda r: len(r.text) > min_text_length
and utils_generic.is_valuable_text(r.text),
text_blocks_all,
)
return to_json(text_blocks)
@celery_app.task(name="tasks.mit.translate")
def mit_translate(**kwargs):
logger.debug("Running translate %s", kwargs)
result = async_translate(**kwargs)
logger.debug("Running translate result = %s", result)
return result
@celery_app.task(name="tasks.mit.inpaint")
def mit_inpaint(path_or_url: str, **kwargs):
raise NotImplementedError()
def load_rgb_image(path_or_url: str) -> np.ndarray:
if re.match(r"^https?://", path_or_url):
raise NotImplementedError("URL not supported yet")
img = Image.open(path_or_url)
img_rgb, img_alpha = utils_generic.load_image(img)
return img_rgb
def deserialize_quad_list(text_lines: list[dict]) -> list[utils_generic.Quadrilateral]:
def create(json_value: dict) -> utils_generic.Quadrilateral:
optional_args = {
k: json_value[k]
for k in ["fg_r", "fg_g", "fg_b", "bg_r", "bg_g", "bg_b"]
if k in json_value
}
return utils_generic.Quadrilateral(
pts=np.array(json_value["pts"]),
text=json_value["text"],
prob=json_value["prob"],
**optional_args,
)
return list(map(create, text_lines))
@async_to_sync
async def async_detection(path_or_url: str, **kwargs: str):
await detection.prepare(kwargs["detector_key"])
img = load_rgb_image(path_or_url)
textlines, mask_raw, mask = await detection.dispatch(
image=img,
# detector_key=kwargs['detector_key'],
**kwargs,
)
return {
"textlines": json.loads(json.dumps(textlines, cls=JSONEncoder)),
# 'mask_raw': mask_raw,
# 'mask': mask,
}
@async_to_sync
async def async_ocr(
path_or_url: str, **kwargs
) -> Awaitable[list[utils_generic.Quadrilateral]]:
await mit_ocr.prepare(kwargs["ocr_key"])
img = load_rgb_image(path_or_url)
quads = deserialize_quad_list(kwargs["regions"])
result: list[utils_generic.Quadrilateral] = await mit_ocr.dispatch(
ocr_key=kwargs["ocr_key"],
image=img,
regions=quads,
args=kwargs,
verbose=kwargs.get("verbose", False),
)
return result
@async_to_sync
async def async_textline_merge(
*, textlines: list[utils_generic.Quadrilateral], width: int, height: int
) -> list[utils_textblock.TextBlock]:
return await textline_merge.dispatch(textlines, width, height)
@async_to_sync
async def async_translate(**kwargs) -> Awaitable[list[str]]:
query = kwargs["query"]
target_lang = kwargs["target_lang"]
translator = translators.get_translator(kwargs["translator"])
if isinstance(translator, translators.OfflineTranslator):
await translator.download()
await translator.load("auto", target_lang, device="cpu")
result = await translator.translate(
from_lang="auto",
to_lang=target_lang,
queries=[query],
)
return result