Skip to content

Commit

Permalink
feat: bind envs
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu committed Dec 6, 2024
1 parent 6d1591b commit 4e9a99d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 166 deletions.
200 changes: 34 additions & 166 deletions pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import requests
import cgi

# Map service names to pdf2zh service options
# five value, padding with None
service_map: dict[str, BaseTranslator] = {
"Google": GoogleTranslator,
"Bing": BingTranslator,
Expand Down Expand Up @@ -112,15 +110,12 @@ def translate(
file_input,
link_input,
service,
apikey,
apikey2,
apikey3,
model_id,
lang_from,
lang_to,
page_range,
recaptcha_response,
progress=gr.Progress(),
*envs,
):
"""Translate PDF content using selected service."""
if flag_demo and not verify_recaptcha(recaptcha_response):
Expand Down Expand Up @@ -149,13 +144,13 @@ def translate(
file_zh = output / f"{filename}-zh.pdf"
file_dual = output / f"{filename}-dual.pdf"

selected_service = service
translator = service_map[service]
selected_page = page_map[page_range]
lang_from = lang_map[lang_from]
lang_to = lang_map[lang_to]

VariablesSetter = TranslationVariables(service_map, apikey, apikey2, apikey3)
VariablesSetter.process_service(lang_from, lang_to, selected_service)
for i, env in enumerate(translator.envs.items()):
os.environ.setdefault(env[0], envs[i])

print(f"Files before translation: {os.listdir(output)}")

Expand All @@ -167,7 +162,7 @@ def progress_bar(t: tqdm.tqdm):
"pages": selected_page,
"lang_in": lang_from,
"lang_out": lang_to,
"service": f"{selected_service}:{model_id}",
"service": f"{translator.name}",
"output": output,
"thread": 4,
"callback": progress_bar,
Expand Down Expand Up @@ -196,44 +191,6 @@ def progress_bar(t: tqdm.tqdm):
)


class TranslationVariables:
def __init__(self, service_map, apikey, apikey2=None, apikey3=None):
self.service_map = service_map
self.apikey = apikey
self.apikey2 = apikey2
self.apikey3 = apikey3

def set_language(self, lang_from, lang_to, selected_service):
"""Sets the language parameters based on the selected service."""
if selected_service == "google":
lang_from = "zh-CN" if lang_from == "zh" else lang_from
lang_to = "zh-CN" if lang_to == "zh" else lang_to
return lang_from, lang_to

def set_environment_variables(self, selected_service):
"""Sets the environment variables based on the selected service."""
print(self.service_map, selected_service)
if selected_service in self.service_map:
service_info = self.service_map[selected_service]
if service_info[0]:
os.environ.setdefault(service_info[0], self.apikey)
print(service_info[0], self.apikey)
if service_info[1]:
os.environ.setdefault(service_info[1], self.apikey2)
print(service_info[1], self.apikey2)
if service_info[2]:
os.environ.setdefault(service_info[2], self.apikey3)
print(service_info[2], self.apikey3)
else:
raise gr.Error("Strange Service")

def process_service(self, lang_from, lang_to, selected_service):
"""Main processing method for the selected service."""
lang_from, lang_to = self.set_language(lang_from, lang_to, selected_service)
self.set_environment_variables(selected_service)
return lang_from, lang_to


# Global setup
custom_blue = gr.themes.Color(
c50="#E8F3FF",
Expand Down Expand Up @@ -282,19 +239,6 @@ def process_service(self, lang_from, lang_to, selected_service):
.progress-bar {
border-radius: 8px !important;
}
# .input-file label {
# color: #165DFF !important;
# border: 1.2px dashed #165DFF !important;
# border-left: none !important;
# border-top: none !important;
# }
# .input-file .wrap {
# color: #165DFF !important;
# }
# .input-file .or {
# color: #165DFF !important;
# }
""",
head=(
"""
Expand Down Expand Up @@ -336,16 +280,18 @@ def process_service(self, lang_from, lang_to, selected_service):
interactive=True,
)
gr.Markdown("## Option")
with gr.Row():
service = gr.Dropdown(
label="Service",
choices=service_map.keys(),
value="Google",
)
apikey = gr.Textbox(
label="API Key",
max_lines=1,
visible=False,
service = gr.Dropdown(
label="Service",
choices=service_map.keys(),
value="Google",
)
envs = []
for i in range(3):
envs.append(
gr.Textbox(
visible=False,
interactive=True,
)
)
with gr.Row():
lang_from = gr.Dropdown(
Expand All @@ -363,97 +309,17 @@ def process_service(self, lang_from, lang_to, selected_service):
label="Pages",
value=list(page_map.keys())[0],
)
model_id = gr.Textbox(
label="Model ID",
visible=False,
interactive=True,
)
apikey2 = gr.Textbox(
label="API Key 2",
max_lines=1,
visible=False,
)
apikey3 = gr.Textbox(
label="API Key 3",
max_lines=1,
visible=False,
)
envs_status = "<span class='env-success'>- Properly configured.</span><br>"

def details_wrapper(text_markdown):
text = f"""
<summary>Technical details</summary>
{text_markdown}
- GitHub: <a href="https://github.com/Byaidu/PDFMathTranslate">Byaidu/PDFMathTranslate</a><br>
- GUI by: <a href="https://github.com/reycn">Rongxin</a><br>
- Version: {__version__}
"""
return text

def env_var_checker(env_var_name: str) -> str:
envvarflag = True
envs_status = ""
for envvar in env_var_name:
if envvar:
if not os.environ.get(envvar):
envs_status += f"<span class='env-warning'>- Warning: environmental not found or error ({envvar}).</span><br>"
envvarflag = False
else:
value = str(os.environ.get(envvar))
envs_status += (
f"- {envvar}: <code>{value[:13]}***</code><br>"
)

if envvarflag:
envs_status = (
"<span class='env-success'>- Properly configured.</span><br>"
)
else:
envs_status += "- Please make sure that the environment variables are properly configured "
envs_status += "(<a href='https://github.com/Byaidu/PDFMathTranslate'>guide</a>).<br>"
return details_wrapper(envs_status)

def on_select_service(service, evt: gr.EventData):
# if service in service_config:
# config = service_config[service]
# apikey_content = gr.update(
# **(
# config["apikey_content"](service_map[service])
# if callable(config["apikey_content"])
# else config["apikey_content"]
# )
# )
# apikey2_visibility = gr.update(
# **(
# config["apikey2_visibility"](service_map[service])
# if callable(config["apikey2_visibility"])
# else config["apikey2_visibility"]
# )
# )
# model_visibility = gr.update(
# **(
# config["model_visibility"](service_map[service])
# if callable(config["model_visibility"])
# else config["model_visibility"]
# )
# )
# apikey3_visibility = gr.update(
# **(
# config["apikey3_visibility"](service_map[service])
# if callable(config["apikey3_visibility"])
# else config["apikey3_visibility"]
# )
# )
# else:
# raise gr.Error("Strange Service")
# return (
# env_var_checker(service_map[service]),
# model_visibility,
# apikey_content,
# apikey2_visibility,
# apikey3_visibility,
# )
pass
translator = service_map[service]
_envs = []
for i in range(3):
_envs.append(gr.update(visible=False, value=""))
for i, env in enumerate(translator.envs.items()):
_envs[i] = gr.update(
visible=True, label=env[0], value=os.environ.get(env[0], env[1])
)
return _envs

def on_select_filetype(file_type):
return (
Expand All @@ -472,13 +338,18 @@ def on_select_filetype(file_type):
recaptcha_box = gr.HTML('<div id="recaptcha-box"></div>')
translate_btn = gr.Button("Translate", variant="primary")
tech_details_tog = gr.Markdown(
details_wrapper(envs_status),
f"""
<summary>Technical details</summary>
- GitHub: <a href="https://github.com/Byaidu/PDFMathTranslate">Byaidu/PDFMathTranslate</a><br>
- GUI by: <a href="https://github.com/reycn">Rongxin</a><br>
- Version: {__version__}
""",
elem_classes=["secondary-text"],
)
service.select(
on_select_service,
service,
[tech_details_tog, model_id, apikey, apikey2, apikey3],
envs,
)
file_type.select(
on_select_filetype,
Expand Down Expand Up @@ -534,14 +405,11 @@ def on_select_filetype(file_type):
file_input,
link_input,
service,
apikey,
apikey2,
apikey3,
model_id,
lang_from,
lang_to,
page_range,
recaptcha_response,
*envs,
],
outputs=[
output_file,
Expand Down
11 changes: 11 additions & 0 deletions pdf2zh/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ def remove_control_characters(s):
class BaseTranslator:
name = "base"
envs = {}
lang_map = {}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh" if lang_out == "auto" else lang_out
lang_in = "en" if lang_in == "auto" else lang_in
lang_out = self.lang_map.get(lang_out, lang_out)
lang_in = self.lang_map.get(lang_in, lang_in)
self.service = service
self.lang_out = lang_out
self.lang_in = lang_in
Expand All @@ -51,6 +56,7 @@ def __str__(self):

class GoogleTranslator(BaseTranslator):
name = "google"
lang_map = {"zh": "zh-CN"}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh-CN" if lang_out == "auto" else lang_out
Expand Down Expand Up @@ -83,6 +89,7 @@ class BingTranslator(BaseTranslator):
# https://github.com/immersive-translate/old-immersive-translate/blob/6df13da22664bea2f51efe5db64c63aca59c4e79/src/background/translationService.js
# TODO: IID & IG
name = "bing"
lang_map = {"zh": "zh-Hans"}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh-Hans" if lang_out == "auto" else lang_out
Expand All @@ -104,6 +111,7 @@ def fineSID(self):
return ig, iid, key, token

def translate(self, text):
text = text[:1000] # bing translate max length
ig, iid, key, token = self.fineSID()
resp = self.session.post(
f"{self.endpoint}?IG={ig}&IID={iid}",
Expand Down Expand Up @@ -151,6 +159,7 @@ class DeepLTranslator(BaseTranslator):
"DEEPL_SERVER_URL": "https://api.deepl.com",
"DEEPL_AUTH_KEY": None,
}
lang_map = {"zh": "zh-Hans"}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh" if lang_out == "auto" else lang_out
Expand All @@ -174,6 +183,7 @@ class DeepLXTranslator(BaseTranslator):
envs = {
"DEEPLX_ENDPOINT": "https://api.deepl.com/translate",
}
lang_map = {"zh": "zh-Hans"}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh" if lang_out == "auto" else lang_out
Expand Down Expand Up @@ -254,6 +264,7 @@ class AzureTranslator(BaseTranslator):
"AZURE_ENDPOINT": "https://api.translator.azure.cn",
"AZURE_APIKEY": None,
}
lang_map = {"zh": "zh-Hans"}

def __init__(self, service, lang_out, lang_in, model):
lang_out = "zh-Hans" if lang_out == "auto" else lang_out
Expand Down

0 comments on commit 4e9a99d

Please sign in to comment.