Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tuhm1 committed Mar 8, 2024
0 parents commit 8a33772
Show file tree
Hide file tree
Showing 42 changed files with 5,539 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.venv
__pycache__
dist
*.spec
build
browser_context
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Transmanga

Translate manga using Microsoft Copilot (Bing Chat) or Google Translate.

| Original | English |
| ------------------------------ | ----------------------------- |
| ![](docs/example/original.jpg) | ![](docs/example/english.png) |
12 changes: 12 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import subprocess
import pathlib


def build():
client_path = pathlib.Path(__file__).parent / "transmanga" / "client"
subprocess.run("npm install", shell=True, check=True, cwd=client_path)
subprocess.run("npm run build", shell=True, check=True, cwd=client_path)


if __name__ == "__main__":
build()
Binary file added docs/example/english.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/example/original.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import transmanga.__main__

transmanga.__main__.main()
2,826 changes: 2,826 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[tool.poetry]
name = "transmanga"
version = "0.1.0"
description = "Translate manga"
authors = ["tuhm1 <hmtu117@gmail.com>"]
readme = "README.md"
build = "build.py"

[tool.poetry.dependencies]
python = ">=3.11,<3.13"
manga-ocr = "^0.1.11"
translators = "^5.8.9"
mediapipe = "^0.10.9"
pywebview = "^4.4.1"
playwright = "^1.41.2"

[tool.poetry.group.dev.dependencies]
pyinstaller = "^6.3.0"
poethepoet = "^0.24.4"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poe.tasks]
build = '''pyinstaller
--add-data transmanga/client/dist:transmanga/client/dist
--add-data transmanga/detector.tflite:transmanga
--collect-data mediapipe
--hidden-import unidic_lite
--collect-data unidic_lite
--collect-binaries unidic_lite
--collect-data manga_ocr
--windowed
--name transmanga
--icon translate.ico
--clean
main.py'''
Empty file added tests/__init__.py
Empty file.
Binary file added translate.ico
Binary file not shown.
Empty file added transmanga/__init__.py
Empty file.
83 changes: 83 additions & 0 deletions transmanga/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import mediapipe as mp
import cv2
import numpy as np
from manga_ocr import MangaOcr
from PIL import Image
import io
import base64
import translators
import webview
import mimetypes as mime
import json
import pathlib
from transmanga import copilot
from concurrent import futures
import os


def main():
detector = mp.tasks.vision.ObjectDetector.create_from_options(
mp.tasks.vision.ObjectDetectorOptions(
base_options=mp.tasks.BaseOptions(
model_asset_path=pathlib.Path(__file__, "../detector.tflite"),
),
score_threshold=0.5,
running_mode=mp.tasks.vision.RunningMode.IMAGE,
)
)

mocr_executor = futures.ThreadPoolExecutor()
future_mocr = mocr_executor.submit(MangaOcr)

with futures.ThreadPoolExecutor(
max_workers=1, initializer=copilot.initialize
) as copilot_executor:
for _ in range(copilot_executor._max_workers):
copilot_executor.submit(lambda: None) # to pre-initialize threads

class Api:
def detect(self, img_b64: str):
img_bytes = base64.b64decode(img_b64)
img_array = np.frombuffer(img_bytes, np.uint8)
img_cv2 = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
img_cv2 = cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB)
img_mp = mp.Image(image_format=mp.ImageFormat.SRGB, data=img_cv2)
return to_dict(detector.detect(img_mp))

def ocr(self, img_b64: str):
img_bytes = base64.b64decode(img_b64)
with Image.open(io.BytesIO(img_bytes)) as img:
mocr = future_mocr.result()
mocr_executor.shutdown()
return mocr(img)

def translate(self, texts: list[str], language: str):
joined_texts = "\n".join(texts)
joined_translations = translators.translate_text(
joined_texts, to_language=language, translator="google"
)
return joined_translations.split("\n")

def translate_copilot(self, texts: list[str], language: str):
return copilot_executor.submit(
copilot.translate, texts, language
).result()

# fix https://github.com/python/cpython/issues/88141
mime.add_type("text/javascript", ".js")

webview.create_window(
"Transmanga",
pathlib.Path(__file__, "../client/dist/index.html").as_posix(),
js_api=Api(),
maximized=True,
)
webview.start(private_mode=False, debug=os.environ.get("DEBUG") == "1")


def to_dict(obj):
return json.loads(json.dumps(obj, default=lambda o: getattr(o, "__dict__", str(o))))


if __name__ == "__main__":
main()
24 changes: 24 additions & 0 deletions transmanga/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
24 changes: 24 additions & 0 deletions transmanga/client/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
28 changes: 28 additions & 0 deletions transmanga/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Usage

```bash
$ npm install # or pnpm install or yarn install
```

### Learn more on the [Solid Website](https://solidjs.com) and come chat with us on our [Discord](https://discord.com/invite/solidjs)

## Available Scripts

In the project directory, you can run:

### `npm run dev`

Runs the app in the development mode.<br>
Open [http://localhost:5173](http://localhost:5173) to view it in the browser.

### `npm run build`

Builds the app for production to the `dist` folder.<br>
It correctly bundles Solid in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

## Deployment

Learn more about deploying your application with the [documentations](https://vitejs.dev/guide/static-deploy.html)
13 changes: 13 additions & 0 deletions transmanga/client/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Transmanga</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>
Loading

0 comments on commit 8a33772

Please sign in to comment.