forked from zyddnys/manga-image-translator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from moeflow-com/gradio-add-translate-packager
gradio UI: export to moeflow
- Loading branch information
Showing
16 changed files
with
404 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,4 @@ MANIFEST | |
# Input and Output | ||
/input/ | ||
/input-translated/ | ||
/storage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from .process_file import process_files | ||
from .export_moeflow_project import export_moeflow_project | ||
from ._const import is_cuda_avaiable | ||
|
||
# from .export_moeflow_project import ex | ||
|
||
__all__ = [ | ||
# "process_file", | ||
"process_files", | ||
"export_moeflow_project", | ||
"is_cuda_avaiable", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import uuid | ||
import datetime | ||
from pathlib import Path | ||
import functools | ||
|
||
_storage_dir = Path(__file__).parent.parent.parent / "storage" | ||
storage_dir = _storage_dir.resolve() | ||
|
||
|
||
def create_unique_dir(suffix: str | None = None) -> Path: | ||
if suffix is None: | ||
suffix = "" | ||
parts = [ | ||
datetime.datetime.now().strftime("%Y%m%d-%H%M%S"), | ||
uuid.uuid4().hex[0:8], | ||
] | ||
if suffix: | ||
parts.append(suffix) | ||
|
||
return _storage_dir / "-".join(parts) | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def is_cuda_avaiable(): | ||
import torch | ||
|
||
return torch.cuda.is_available() |
3 changes: 1 addition & 2 deletions
3
manga_translator/gradio/detection.py → manga_translator/moeflow/detection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from pydantic import BaseModel | ||
from .model import FileBatchProcessResult, FileProcessResult | ||
from ._const import create_unique_dir | ||
import zipfile | ||
import logging | ||
from pathlib import Path | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
|
||
def export_moeflow_project( | ||
process_result: FileBatchProcessResult, project_name: str | None | ||
) -> Path: | ||
if not project_name: | ||
project_name = process_result.files[0].local_path.name.rsplit(".", 1)[0] | ||
|
||
meta_json = MoeflowProjectMeta(name=project_name, intro="").model_dump_json() | ||
|
||
output_dir = create_unique_dir("moeflow-export") | ||
output_dir.mkdir(parents=True, exist_ok=True) | ||
output_file = output_dir / f"{project_name}.zip" | ||
output_file.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
with zipfile.ZipFile(output_file, "w") as zf: | ||
zf.writestr("project.json", meta_json) | ||
zf.writestr("translations.txt", _build_file(process_result.files)) | ||
for f in process_result.files: | ||
zf.write(f.local_path, f"images/{f.local_path.name}") | ||
return output_file | ||
|
||
|
||
class MoeflowProjectMeta(BaseModel): | ||
name: str | ||
intro: str | ||
output_language: str = "en" | ||
default_role: str = "supporter" | ||
allow_apply_type: int = 3 | ||
application_check_type: int = 1 | ||
is_need_check_application: bool = False | ||
source_language: str = "ja" | ||
|
||
|
||
def _build_file(files: list[FileProcessResult]) -> str: | ||
result: list[str] = [] | ||
for file in files: | ||
result.append(f">>>>[{file.local_path.name}]<<<<") | ||
if file.translated: | ||
translated_texts: list[str] = next(iter(file.translated.values())) | ||
else: | ||
translated_texts = None | ||
logging.debug( | ||
"file: %s %s x %s", file.local_path.name, file.image_w, file.image_h | ||
) | ||
|
||
for idx, block in enumerate(file.text_blocks): | ||
t = translated_texts[idx] if translated_texts else "" | ||
x = ( | ||
block.center_x / file.image_h | ||
) # this works but IDK why. Is mit using a swapped coordinate system? | ||
y = block.center_y / file.image_w | ||
logging.debug( | ||
"block: %s,%s / %s", block.center_x, block.center_y, block.text | ||
) | ||
position_type = 1 | ||
result.append(f"----[{idx}]----[{x},{y},{position_type}]") | ||
logging.debug("serialized block: %s,%s / %s", x, y, result[-1]) | ||
result.append(t) | ||
return "\n".join(result + [""]) |
Oops, something went wrong.