Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,13 +9,13 @@ def translate_document(file, target_language, target_country, api_key):
|
|
| 9 |
log_messages = []
|
| 10 |
|
| 11 |
try:
|
| 12 |
-
log_messages.append("
|
| 13 |
|
| 14 |
# 複製文件到新文件
|
| 15 |
original_file_path = file.name
|
| 16 |
copied_file_path = "copy_" + os.path.basename(original_file_path)
|
| 17 |
shutil.copy(original_file_path, copied_file_path)
|
| 18 |
-
log_messages.append(f"
|
| 19 |
|
| 20 |
# 處理 .docx 文件
|
| 21 |
if copied_file_path.endswith(".docx"):
|
|
@@ -44,7 +44,7 @@ def translate_document(file, target_language, target_country, api_key):
|
|
| 44 |
|
| 45 |
output_path = "translated_" + os.path.basename(original_file_path)
|
| 46 |
doc.save(output_path)
|
| 47 |
-
log_messages.append(f"
|
| 48 |
|
| 49 |
# 處理 .pptx 文件
|
| 50 |
elif copied_file_path.endswith(".pptx"):
|
|
@@ -59,28 +59,28 @@ def translate_document(file, target_language, target_country, api_key):
|
|
| 59 |
|
| 60 |
output_path = "translated_" + os.path.basename(original_file_path)
|
| 61 |
prs.save(output_path)
|
| 62 |
-
log_messages.append(f"
|
| 63 |
|
| 64 |
return output_path, "\n".join(log_messages) # 返回文件路徑和日誌信息
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
log_messages.append(f"
|
| 68 |
return None, "\n".join(log_messages) # 返回 None 和日誌
|
| 69 |
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=translate_document,
|
| 72 |
inputs=[
|
| 73 |
gr.File(label="上傳文件 (.docx 或 .pptx)"),
|
| 74 |
-
gr.Dropdown(choices=["en", "es", "fr", "de", "ja", "ko"], label="
|
| 75 |
-
gr.Dropdown(choices=["US", "UK", "ES", "FR", "DE", "JP", "KR"], label="
|
| 76 |
gr.Textbox(label="API 金鑰(可選)", placeholder="如果需要,請輸入您的 API 金鑰")
|
| 77 |
],
|
| 78 |
outputs=[
|
| 79 |
-
gr.File(label="
|
| 80 |
-
gr.Textbox(label="
|
| 81 |
],
|
| 82 |
-
title="
|
| 83 |
-
description="
|
| 84 |
)
|
| 85 |
|
| 86 |
iface.launch()
|
|
|
|
| 9 |
log_messages = []
|
| 10 |
|
| 11 |
try:
|
| 12 |
+
log_messages.append("Starting translation process...")
|
| 13 |
|
| 14 |
# 複製文件到新文件
|
| 15 |
original_file_path = file.name
|
| 16 |
copied_file_path = "copy_" + os.path.basename(original_file_path)
|
| 17 |
shutil.copy(original_file_path, copied_file_path)
|
| 18 |
+
log_messages.append(f"Copied file to {copied_file_path}")
|
| 19 |
|
| 20 |
# 處理 .docx 文件
|
| 21 |
if copied_file_path.endswith(".docx"):
|
|
|
|
| 44 |
|
| 45 |
output_path = "translated_" + os.path.basename(original_file_path)
|
| 46 |
doc.save(output_path)
|
| 47 |
+
log_messages.append(f"Saved translated document as {output_path}.")
|
| 48 |
|
| 49 |
# 處理 .pptx 文件
|
| 50 |
elif copied_file_path.endswith(".pptx"):
|
|
|
|
| 59 |
|
| 60 |
output_path = "translated_" + os.path.basename(original_file_path)
|
| 61 |
prs.save(output_path)
|
| 62 |
+
log_messages.append(f"Saved translated presentation as {output_path}.")
|
| 63 |
|
| 64 |
return output_path, "\n".join(log_messages) # 返回文件路徑和日誌信息
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
+
log_messages.append(f"An error occurred: {e}")
|
| 68 |
return None, "\n".join(log_messages) # 返回 None 和日誌
|
| 69 |
|
| 70 |
iface = gr.Interface(
|
| 71 |
fn=translate_document,
|
| 72 |
inputs=[
|
| 73 |
gr.File(label="上傳文件 (.docx 或 .pptx)"),
|
| 74 |
+
gr.Dropdown(choices=["en", "es", "fr", "de", "ja", "ko", "zh-TW", "vi"], label="Target Language"),
|
| 75 |
+
gr.Dropdown(choices=["US", "UK", "ES", "FR", "DE", "JP", "KR", "TW", "VN"], label="Target Country"),
|
| 76 |
gr.Textbox(label="API 金鑰(可選)", placeholder="如果需要,請輸入您的 API 金鑰")
|
| 77 |
],
|
| 78 |
outputs=[
|
| 79 |
+
gr.File(label="Download Translated Document"),
|
| 80 |
+
gr.Textbox(label="Log Output", lines=10) # 添加一個文本框來顯示日誌信息
|
| 81 |
],
|
| 82 |
+
title="Document Translator",
|
| 83 |
+
description="Translate your documents while preserving the original format.",
|
| 84 |
)
|
| 85 |
|
| 86 |
iface.launch()
|