Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : prompt not work with GUI. #495

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pdf2zh/converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List
from typing import Dict
from enum import Enum

from pdfminer.pdfinterp import PDFGraphicState, PDFResourceManager
Expand All @@ -17,6 +17,7 @@
import concurrent.futures
import numpy as np
import unicodedata
from string import Template
from tenacity import retry, wait_fixed
from pdf2zh.translator import (
AzureOpenAITranslator,
Expand Down Expand Up @@ -144,7 +145,7 @@ def __init__(
noto_name: str = "",
noto: Font = None,
envs: Dict = None,
prompt: List = None,
prompt: Template = None,
) -> None:
super().__init__(rsrcmgr)
self.vfont = vfont
Expand All @@ -159,8 +160,6 @@ def __init__(
service_model = param[1] if len(param) > 1 else None
if not envs:
envs = {}
if not prompt:
prompt = []
for translator in [GoogleTranslator, BingTranslator, DeepLTranslator, DeepLXTranslator, OllamaTranslator, XinferenceTranslator, AzureOpenAITranslator,
OpenAITranslator, ZhipuTranslator, ModelScopeTranslator, SiliconTranslator, GeminiTranslator, AzureTranslator, TencentTranslator, DifyTranslator, AnythingLLMTranslator, ArgosTranslator, GorkTranslator, GroqTranslator, DeepseekTranslator, OpenAIlikedTranslator,]:
if service_name == translator.name:
Expand Down
2 changes: 1 addition & 1 deletion pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def progress_bar(t: tqdm.tqdm):
"callback": progress_bar,
"cancellation_event": cancellation_event_map[session_id],
"envs": _envs,
"prompt": Template(prompt),
"prompt": Template(prompt) if prompt else None,
"model": ModelInstance.value,
}
try:
Expand Down
7 changes: 4 additions & 3 deletions pdf2zh/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import urllib.request
from asyncio import CancelledError
from pathlib import Path
from string import Template
from typing import Any, BinaryIO, List, Optional, Dict

import numpy as np
Expand Down Expand Up @@ -78,7 +79,7 @@ def translate_patch(
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
envs: Dict = None,
prompt: List = None,
prompt: Template = None,
**kwarg: Any,
) -> None:
rsrcmgr = PDFResourceManager()
Expand Down Expand Up @@ -172,7 +173,7 @@ def translate_stream(
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
envs: Dict = None,
prompt: List = None,
prompt: Template = None,
**kwarg: Any,
):
font_list = [("tiro", None)]
Expand Down Expand Up @@ -297,7 +298,7 @@ def translate(
cancellation_event: asyncio.Event = None,
model: OnnxModel = None,
envs: Dict = None,
prompt: List = None,
prompt: Template = None,
**kwarg: Any,
):
if not files:
Expand Down
26 changes: 0 additions & 26 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
self.client = ollama.Client()
self.prompttext = prompt
self.add_cache_impact_parameters("temperature", self.options["temperature"])
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text):
maxlen = max(2000, len(text) * 5)
Expand Down Expand Up @@ -320,8 +318,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
self.client = xinference_client.RESTfulClient(self.envs["XINFERENCE_HOST"])
self.prompttext = prompt
self.add_cache_impact_parameters("temperature", self.options["temperature"])
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text):
maxlen = max(2000, len(text) * 5)
Expand Down Expand Up @@ -384,8 +380,6 @@ def __init__(
)
self.prompttext = prompt
self.add_cache_impact_parameters("temperature", self.options["temperature"])
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text) -> str:
response = self.client.chat.completions.create(
Expand Down Expand Up @@ -438,8 +432,6 @@ def __init__(
)
self.prompttext = prompt
self.add_cache_impact_parameters("temperature", self.options["temperature"])
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text) -> str:
response = self.client.chat.completions.create(
Expand Down Expand Up @@ -476,8 +468,6 @@ def __init__(
model = self.envs["MODELSCOPE_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class ZhipuTranslator(OpenAITranslator):
Expand All @@ -497,8 +487,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["ZHIPU_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text) -> str:
try:
Expand Down Expand Up @@ -534,8 +522,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["SILICON_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class GeminiTranslator(OpenAITranslator):
Expand All @@ -555,8 +541,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["GEMINI_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class AzureTranslator(BaseTranslator):
Expand Down Expand Up @@ -634,8 +618,6 @@ def __init__(self, lang_out, lang_in, model, envs=None, prompt=None):
"Content-Type": "application/json",
}
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)

def do_translate(self, text):
messages = self.prompt(text, self.prompttext)
Expand Down Expand Up @@ -752,8 +734,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["GORK_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class GroqTranslator(OpenAITranslator):
Expand All @@ -772,8 +752,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["GROQ_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class DeepseekTranslator(OpenAITranslator):
Expand All @@ -792,8 +770,6 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
model = self.envs["DEEPSEEK_MODEL"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)


class OpenAIlikedTranslator(OpenAITranslator):
Expand Down Expand Up @@ -822,5 +798,3 @@ def __init__(self, lang_in, lang_out, model, envs=None, prompt=None):
api_key = self.envs["OPENAILIKED_API_KEY"]
super().__init__(lang_in, lang_out, model, base_url=base_url, api_key=api_key)
self.prompttext = prompt
if prompt:
self.add_cache_impact_parameters("prompt", prompt.template)
Loading