Skip to content

Commit

Permalink
Merge pull request #397 from timelic/fix/circle-import-model
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu authored Jan 3, 2025
2 parents ba7b3f2 + 09726b4 commit 3c2b7e3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pdf2zh/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tqdm
import json
import io
from pdf2zh.pdf2zh import model
from pdf2zh.doclayout import ModelInstance

flask_app = Flask("pdf2zh")
flask_app.config.from_mapping(
Expand Down Expand Up @@ -48,7 +48,7 @@ def progress_bar(t: tqdm.tqdm):
doc_mono, doc_dual = translate_stream(
stream,
callback=progress_bar,
model=model,
model=ModelInstance.value,
**args,
)
return doc_mono, doc_dual
Expand Down
5 changes: 5 additions & 0 deletions pdf2zh/doclayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def __init__(self, data):


class OnnxModel(DocLayoutModel):

def __init__(self, model_path: str):
self.model_path = model_path

Expand Down Expand Up @@ -173,3 +174,7 @@ def predict(self, image, imgsz=1024, **kwargs):
(new_h, new_w), preds[..., :4], (orig_h, orig_w)
)
return [YoloResult(boxes=preds, names=self._names)]


class ModelInstance:
value: OnnxModel = None
4 changes: 2 additions & 2 deletions pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pdf2zh import __version__
from pdf2zh.high_level import translate
from pdf2zh.pdf2zh import model
from pdf2zh.doclayout import ModelInstance
from pdf2zh.translator import (
AnythingLLMTranslator,
AzureOpenAITranslator,
Expand Down Expand Up @@ -274,7 +274,7 @@ def progress_bar(t: tqdm.tqdm):
"cancellation_event": cancellation_event_map[session_id],
"envs": _envs,
"prompt": prompt,
"model": model,
"model": ModelInstance.value,
}
try:
translate(**param)
Expand Down
15 changes: 6 additions & 9 deletions pdf2zh/pdf2zh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from pdf2zh import __version__, log
from pdf2zh.high_level import translate
from pdf2zh.doclayout import OnnxModel
from pdf2zh.doclayout import OnnxModel, ModelInstance
import os


Expand Down Expand Up @@ -199,21 +199,18 @@ def find_all_files_in_directory(directory_path):
return file_paths


model = None


def main(args: Optional[List[str]] = None) -> int:
logging.basicConfig()

parsed_args = parse_args(args)

if parsed_args.debug:
log.setLevel(logging.DEBUG)
global model

if parsed_args.onnx:
model = OnnxModel(parsed_args.onnx)
ModelInstance.value = OnnxModel(parsed_args.onnx)
else:
model = OnnxModel.load_available()
ModelInstance.value = OnnxModel.load_available()

if parsed_args.interactive:
from pdf2zh.gui import setup_gui
Expand Down Expand Up @@ -250,10 +247,10 @@ def main(args: Optional[List[str]] = None) -> int:
untranlate_file = find_all_files_in_directory(parsed_args.files[0])
parsed_args.files = untranlate_file
print(parsed_args)
translate(model=model, **vars(parsed_args))
translate(model=ModelInstance.value, **vars(parsed_args))
return 0
# print(parsed_args)
translate(model=model, **vars(parsed_args))
translate(model=ModelInstance.value, **vars(parsed_args))
return 0


Expand Down

0 comments on commit 3c2b7e3

Please sign in to comment.