Skip to content

Commit

Permalink
Merge pull request #166 from hellofinch/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Byaidu authored Dec 4, 2024
2 parents 069205f + a983d6f commit 33f6a7f
Showing 1 changed file with 180 additions and 31 deletions.
211 changes: 180 additions & 31 deletions pdf2zh/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,92 @@
# Map service names to pdf2zh service options
service_map = {
"Google": ("google", None, None),
"DeepL": ("deepl", "DEEPL_AUTH_KEY", None),
"DeepLX": ("deeplx", "DEEPLX_AUTH_KEY", None),
"Ollama": ("ollama", None, "gemma2"),
"OpenAI": ("openai", "OPENAI_API_KEY", "gpt-4o"),
"Azure": ("azure", "AZURE_APIKEY", None),
"Tencent": ("tencent", "TENCENT_SECRET_KEY", None),
"DeepL": ("deepl", "DEEPL_SERVER_URL", "DEEPL_AUTH_KEY"),
"DeepLX": ("deeplx", "DEEPLX_SERVER_URL", "DEEPLX_AUTH_KEY"),
"Ollama": ("ollama", None, None),
"OpenAI": ("openai", "OPENAI_API_KEY", None),
"Azure": ("azure", "AZURE_APIKEY", "AZURE_ENDPOINT", "AZURE_REGION"),
"Tencent": ("tencent", "TENCENT_SECRET_KEY", "TENCENT_SECRET_ID"),
}
service_config = {
"google": {
"apikey_content": {"visible": False},
"apikey2_visibility": {"visible": False},
"model_visibility": {"visible": False},
"apikey3_visibility": {"visible": False},
},
"deepl": {
"apikey_content": lambda s: {
"visible": True,
"value": os.environ.get(s[1]),
"label": s[1],
},
"apikey2_visibility": lambda s: {
"visible": True,
"value": os.environ.get(s[2]),
"label": s[2],
},
"model_visibility": {"visible": False},
"apikey3_visibility": {"visible": False},
},
"deeplx": {
"apikey_content": lambda s: {
"visible": True,
"value": os.environ.get(s[1]),
"label": s[1],
},
"apikey2_visibility": lambda s: {
"visible": True,
"value": os.environ.get(s[2]),
"label": s[2],
},
"model_visibility": {"visible": False},
"apikey3_visibility": {"visible": False},
},
"ollama": {
"apikey_content": {"visible": False},
"apikey2_visibility": {"visible": False},
"model_visibility": lambda s: {"visible": True, "value": s[1]},
"apikey3_visibility": {"visible": False},
},
"openai": {
"apikey_content": lambda s: {
"visible": True,
"value": os.environ.get(s[1]),
"label": s[1],
},
"apikey2_visibility": {"visible": False},
"model_visibility": {"visible": True, "value": "gpt-4o"},
"apikey3_visibility": {"visible": False},
},
"azure": {
"apikey_content": lambda s: {
"visible": True,
"value": os.environ.get(s[1]),
"label": s[1],
},
"apikey2_visibility": lambda s: {
"visible": True,
"value": os.environ.get(s[2]),
"label": s[2],
},
"model_visibility": {"visible": False},
"apikey3_visibility": lambda s: {
"visible": True,
"value": os.environ.get(s[3]),
"label": s[3],
},
},
"tencent": {
"apikey_content": lambda s: {
"visible": True,
"value": os.environ.get(s[1]),
"label": s[1],
},
"apikey2_visibility": lambda s: {"visible": True, "value": "", "label": s[2]},
"model_visibility": {"visible": False},
"apikey3_visibility": {"visible": False},
},
}
lang_map = {
"Chinese": "zh",
Expand Down Expand Up @@ -114,6 +194,8 @@ def translate(
link_input,
service,
apikey,
apikey2,
apikey3,
model_id,
lang_from,
lang_to,
Expand Down Expand Up @@ -149,14 +231,40 @@ def translate(
file_dual = output / f"{filename}-dual.pdf"

selected_service = service_map[service][0]
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
selected_page = page_map[page_range]
lang_from = lang_map[lang_from]
lang_to = lang_map[lang_to]

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
elif selected_service == "deepl":
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
if service_map[service][2]:
os.environ.setdefault(service_map[service][2], apikey2)
elif selected_service == "deeplx":
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
if service_map[service][2]:
os.environ.setdefault(service_map[service][2], apikey2)
elif selected_service == "openai":
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
elif selected_service == "azure":
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
if service_map[service][2]:
os.environ.setdefault(service_map[service][2], apikey2)
if service_map[service][3]:
os.environ.setdefault(service_map[service][3], apikey3)
elif selected_service == "tencent":
if service_map[service][1]:
os.environ.setdefault(service_map[service][1], apikey)
if service_map[service][2]:
os.environ.setdefault(service_map[service][2], apikey2)
else:
raise gr.Error("Strange Service")

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

Expand Down Expand Up @@ -331,6 +439,16 @@ def progress_bar(t: tqdm.tqdm):
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):
Expand All @@ -344,42 +462,69 @@ def details_wrapper(text_markdown):
return text

def env_var_checker(env_var_name: str) -> str:
if env_var_name:
if not os.environ.get(env_var_name):
envs_status = (
f"<span class='env-warning'>- Warning: environmental not found or error ({env_var_name})."
+ "</span><br>- Please make sure that the environment variables are properly configured "
+ "(<a href='https://github.com/Byaidu/PDFMathTranslate'>guide</a>).<br>"
)
else:
value = str(os.environ.get(env_var_name))
envs_status = "<span class='env-success'>- Properly configured.</span><br>"
envs_status += (
f"- {env_var_name}: <code>{value[:13]}***</code><br>"
)
else:
envvarflag = True
envs_status = ""
for envvar in env_var_name[1:]:
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_map[service][1]:
service_type = service_map[service][0]

if service_type in service_config:
config = service_config[service_type]
apikey_content = gr.update(
visible=True, value=os.environ.get(service_map[service][1])
**(
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"]
)
)
else:
apikey_content = gr.update(visible=False)
if service_map[service][2]:
model_visibility = gr.update(
visible=True, value=service_map[service][2]
**(
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:
model_visibility = gr.update(visible=False)
raise gr.Error("Strange Service")
return (
env_var_checker(service_map[service][1]),
env_var_checker(service_map[service]),
model_visibility,
apikey_content,
apikey2_visibility,
apikey3_visibility,
)

def on_select_filetype(file_type):
Expand All @@ -403,7 +548,9 @@ def on_select_filetype(file_type):
elem_classes=["secondary-text"],
)
service.select(
on_select_service, service, [tech_details_tog, model_id, apikey]
on_select_service,
service,
[tech_details_tog, model_id, apikey, apikey2, apikey3],
)
file_type.select(
on_select_filetype,
Expand Down Expand Up @@ -460,6 +607,8 @@ def on_select_filetype(file_type):
link_input,
service,
apikey,
apikey2,
apikey3,
model_id,
lang_from,
lang_to,
Expand Down

0 comments on commit 33f6a7f

Please sign in to comment.