From aeb35154627a4c4f8f0da61f9ecc79f67d0e6627 Mon Sep 17 00:00:00 2001 From: hellofinch Date: Tue, 3 Dec 2024 18:36:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9gui=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pdf2zh/gui.py | 222 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 187 insertions(+), 35 deletions(-) diff --git a/pdf2zh/gui.py b/pdf2zh/gui.py index a714125b..2d38ada6 100644 --- a/pdf2zh/gui.py +++ b/pdf2zh/gui.py @@ -13,12 +13,56 @@ # 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", @@ -91,6 +135,8 @@ def translate( file_path, service, apikey, + apikey2, + apikey3, model_id, lang_from, lang_to, @@ -116,14 +162,40 @@ def translate( shutil.copyfile(file_path, file_en) 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)}") @@ -287,6 +359,16 @@ def progress_bar(t: tqdm.tqdm): label="Model ID", visible=False, ) + 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 = "- Properly configured.
" def details_wrapper(text_markdown): @@ -300,42 +382,110 @@ 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"- Warning: environmental not found or error ({env_var_name})." - + "
- Please make sure that the environment variables are properly configured " - + "(guide).
" - ) - else: - value = str(os.environ.get(env_var_name)) - envs_status = "- Properly configured.
" - envs_status += ( - f"- {env_var_name}: {value[:13]}***
" - ) - else: + envvarflag=True + envs_status="" + for envvar in env_var_name[1:]: + if envvar: + if not os.environ.get(envvar): + envs_status += ( + f"- Warning: environmental not found or error ({envvar}).
" + ) + envvarflag=False + else: + value = str(os.environ.get(envvar)) + envs_status += ( + f"- {envvar}: {value[:13]}***
" + ) + + if envvarflag: envs_status = ( "- Properly configured.
" ) + else: + envs_status += ( + "- Please make sure that the environment variables are properly configured " + ) + envs_status += ("(guide).
") return details_wrapper(envs_status) def on_select_service(service, evt: gr.EventData): - if service_map[service][1]: - apikey_content = gr.update( - visible=True, value=os.environ.get(service_map[service][1]) - ) - else: - apikey_content = gr.update(visible=False) - if service_map[service][2]: - model_visibility = gr.update( - visible=True, value=service_map[service][2] - ) + service_type = service_map[service][0] + + if service_type in service_config: + config = service_config[service_type] + 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: - model_visibility = gr.update(visible=False) + raise gr.Error("Strange Service") + # if service_map[service][0] == "google": + # apikey_content = gr.update(visible=False) + # apikey2_visibility = gr.update(visible=False) + # model_visibility = gr.update(visible=False) + # apikey3_visibility = gr.update(visible=False) + # elif service_map[service][0] == "deepl": + # apikey_content = gr.update( + # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] + # ) + # apikey2_visibility = gr.update( + # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] + # ) + # model_visibility = gr.update(visible=False) + # apikey3_visibility = gr.update(visible=False) + # elif service_map[service][0] == "deeplx": + # apikey_content = gr.update( + # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] + # ) + # apikey2_visibility = gr.update( + # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] + # ) + # model_visibility = gr.update(visible=False) + # apikey3_visibility = gr.update(visible=False) + # elif service_map[service][0] == "ollama": + # apikey_content = gr.update(visible=False) + # apikey2_visibility = gr.update(visible=False) + # model_visibility = gr.update( + # visible=True, value=service_map[service][1] + # ) + # apikey3_visibility = gr.update(visible=False) + # elif service_map[service][0] == "openai": + # apikey_content = gr.update( + # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] + # ) + # apikey2_visibility = gr.update(visible=False) + # model_visibility = gr.update( + # visible=True, value="gpt-4o" + # ) + # apikey3_visibility = gr.update(visible=False) + # elif service_map[service][0] == "azure": + # apikey_content = gr.update( + # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] + # ) + # apikey2_visibility = gr.update( + # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] + # ) + # model_visibility = gr.update(visible=False) + # apikey3_visibility = gr.update( + # visible=True, value=os.environ.get(service_map[service][3]),label=service_map[service][3] + # ) + # elif service_map[service][0] == "tencent": + # apikey_content = gr.update( + # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] + # ) + # apikey2_visibility = gr.update( + # visible=True, value="", label=service_map[service][2] + # ) + # model_visibility = gr.update(visible=False) + # apikey3_visibility = gr.update(visible=False) + # else: + # 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, ) output_title = gr.Markdown("## Translated", visible=False) @@ -353,7 +503,7 @@ def on_select_service(service, evt: gr.EventData): 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] ) with gr.Column(scale=2): @@ -388,6 +538,8 @@ def on_select_service(service, evt: gr.EventData): file_input, service, apikey, + apikey2, + apikey3, model_id, lang_from, lang_to, From cb0f4640850d3fcc6c921e1531fab46f365059c9 Mon Sep 17 00:00:00 2001 From: hellofinch Date: Tue, 3 Dec 2024 18:40:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pdf2zh/gui.py | 61 --------------------------------------------------- 1 file changed, 61 deletions(-) diff --git a/pdf2zh/gui.py b/pdf2zh/gui.py index 2d38ada6..127df048 100644 --- a/pdf2zh/gui.py +++ b/pdf2zh/gui.py @@ -419,67 +419,6 @@ def on_select_service(service, evt: gr.EventData): 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") - # if service_map[service][0] == "google": - # apikey_content = gr.update(visible=False) - # apikey2_visibility = gr.update(visible=False) - # model_visibility = gr.update(visible=False) - # apikey3_visibility = gr.update(visible=False) - # elif service_map[service][0] == "deepl": - # apikey_content = gr.update( - # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] - # ) - # apikey2_visibility = gr.update( - # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] - # ) - # model_visibility = gr.update(visible=False) - # apikey3_visibility = gr.update(visible=False) - # elif service_map[service][0] == "deeplx": - # apikey_content = gr.update( - # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] - # ) - # apikey2_visibility = gr.update( - # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] - # ) - # model_visibility = gr.update(visible=False) - # apikey3_visibility = gr.update(visible=False) - # elif service_map[service][0] == "ollama": - # apikey_content = gr.update(visible=False) - # apikey2_visibility = gr.update(visible=False) - # model_visibility = gr.update( - # visible=True, value=service_map[service][1] - # ) - # apikey3_visibility = gr.update(visible=False) - # elif service_map[service][0] == "openai": - # apikey_content = gr.update( - # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] - # ) - # apikey2_visibility = gr.update(visible=False) - # model_visibility = gr.update( - # visible=True, value="gpt-4o" - # ) - # apikey3_visibility = gr.update(visible=False) - # elif service_map[service][0] == "azure": - # apikey_content = gr.update( - # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] - # ) - # apikey2_visibility = gr.update( - # visible=True, value=os.environ.get(service_map[service][2]),label=service_map[service][2] - # ) - # model_visibility = gr.update(visible=False) - # apikey3_visibility = gr.update( - # visible=True, value=os.environ.get(service_map[service][3]),label=service_map[service][3] - # ) - # elif service_map[service][0] == "tencent": - # apikey_content = gr.update( - # visible=True, value=os.environ.get(service_map[service][1]),label=service_map[service][1] - # ) - # apikey2_visibility = gr.update( - # visible=True, value="", label=service_map[service][2] - # ) - # model_visibility = gr.update(visible=False) - # apikey3_visibility = gr.update(visible=False) - # else: - # raise gr.Error("Strange Service") return ( env_var_checker(service_map[service]), model_visibility, From c798a05b1f9514a18d85b1f9e30d8fe6b1aa4555 Mon Sep 17 00:00:00 2001 From: hellofinch Date: Tue, 3 Dec 2024 18:56:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pdf2zh/gui.py | 110 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 84 insertions(+), 26 deletions(-) diff --git a/pdf2zh/gui.py b/pdf2zh/gui.py index 127df048..8a054215 100644 --- a/pdf2zh/gui.py +++ b/pdf2zh/gui.py @@ -17,7 +17,7 @@ "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"), + "Azure": ("azure", "AZURE_APIKEY", "AZURE_ENDPOINT", "AZURE_REGION"), "Tencent": ("tencent", "TENCENT_SECRET_KEY", "TENCENT_SECRET_ID"), } service_config = { @@ -28,14 +28,30 @@ "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]}, + "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]}, + "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}, }, @@ -46,19 +62,39 @@ "apikey3_visibility": {"visible": False}, }, "openai": { - "apikey_content": lambda s: {"visible": True, "value": os.environ.get(s[1]), "label": s[1]}, + "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]}, + "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]}, + "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]}, + "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}, @@ -165,7 +201,7 @@ def translate( 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 @@ -382,15 +418,13 @@ def details_wrapper(text_markdown): return text def env_var_checker(env_var_name: str) -> str: - envvarflag=True - envs_status="" + envvarflag = True + envs_status = "" for envvar in env_var_name[1:]: if envvar: if not os.environ.get(envvar): - envs_status += ( - f"- Warning: environmental not found or error ({envvar}).
" - ) - envvarflag=False + envs_status += f"- Warning: environmental not found or error ({envvar}).
" + envvarflag = False else: value = str(os.environ.get(envvar)) envs_status += ( @@ -402,10 +436,8 @@ def env_var_checker(env_var_name: str) -> str: "- Properly configured.
" ) else: - envs_status += ( - "- Please make sure that the environment variables are properly configured " - ) - envs_status += ("(guide).
") + envs_status += "- Please make sure that the environment variables are properly configured " + envs_status += "(guide).
" return details_wrapper(envs_status) def on_select_service(service, evt: gr.EventData): @@ -413,10 +445,34 @@ def on_select_service(service, evt: gr.EventData): if service_type in service_config: config = service_config[service_type] - 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"])) + 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 ( @@ -442,7 +498,9 @@ def on_select_service(service, evt: gr.EventData): elem_classes=["secondary-text"], ) service.select( - on_select_service, service, [tech_details_tog, model_id, apikey, apikey2, apikey3] + on_select_service, + service, + [tech_details_tog, model_id, apikey, apikey2, apikey3], ) with gr.Column(scale=2):