Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
from docx import Document
|
| 5 |
+
from pptx import Presentation
|
| 6 |
+
from deep_translator import GoogleTranslator
|
| 7 |
+
|
| 8 |
+
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"已複製文件到 {copied_file_path}")
|
| 19 |
+
|
| 20 |
+
# 處理 .docx 文件
|
| 21 |
+
if copied_file_path.endswith(".docx"):
|
| 22 |
+
doc = Document(copied_file_path)
|
| 23 |
+
|
| 24 |
+
# 翻譯並替換段落文本
|
| 25 |
+
for paragraph in doc.paragraphs:
|
| 26 |
+
if paragraph.text.strip(): # 只翻譯非空段落
|
| 27 |
+
translated_text = GoogleTranslator(source='auto', target=target_language).translate(paragraph.text)
|
| 28 |
+
paragraph.text = translated_text # 用翻譯後的文本替換原始文本
|
| 29 |
+
|
| 30 |
+
# 處理表格中的文本
|
| 31 |
+
for table in doc.tables:
|
| 32 |
+
for row in table.rows:
|
| 33 |
+
for cell in row.cells:
|
| 34 |
+
if cell.text.strip(): # 只翻譯非空單元格
|
| 35 |
+
translated_text = GoogleTranslator(source='auto', target=target_language).translate(cell.text)
|
| 36 |
+
cell.text = translated_text # 用翻譯後的文本替換原始文本
|
| 37 |
+
|
| 38 |
+
# 處理文本框和形狀中的文本
|
| 39 |
+
for inline_shape in doc.inline_shapes:
|
| 40 |
+
if inline_shape.type == 3: # 3 表示文本框
|
| 41 |
+
if inline_shape.text_frame and inline_shape.text_frame.text.strip():
|
| 42 |
+
translated_text = GoogleTranslator(source='auto', target=target_language).translate(inline_shape.text_frame.text)
|
| 43 |
+
inline_shape.text_frame.text = translated_text # 替換文本框中的文本
|
| 44 |
+
|
| 45 |
+
output_path = "translated_" + os.path.basename(original_file_path)
|
| 46 |
+
doc.save(output_path)
|
| 47 |
+
log_messages.append(f"已將翻譯後的文件儲存為 {output_path}.")
|
| 48 |
+
|
| 49 |
+
# 處理 .pptx 文件
|
| 50 |
+
elif copied_file_path.endswith(".pptx"):
|
| 51 |
+
prs = Presentation(copied_file_path)
|
| 52 |
+
|
| 53 |
+
# 翻譯並替換文本
|
| 54 |
+
for slide in prs.slides:
|
| 55 |
+
for shape in slide.shapes:
|
| 56 |
+
if hasattr(shape, "text") and shape.text.strip(): # 只翻譯非空文本框
|
| 57 |
+
translated_text = GoogleTranslator(source='auto', target=target_language).translate(shape.text)
|
| 58 |
+
shape.text = translated_text # 用翻譯後的文本替換原始文本
|
| 59 |
+
|
| 60 |
+
output_path = "translated_" + os.path.basename(original_file_path)
|
| 61 |
+
prs.save(output_path)
|
| 62 |
+
log_messages.append(f"已將翻譯後的簡報儲存為 {output_path}.")
|
| 63 |
+
|
| 64 |
+
return output_path, "\n".join(log_messages) # 返回文件路徑和日誌信息
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
log_messages.append(f"發生錯誤: {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"], 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="日誌輸出", lines=10) # 添加一個文本框來顯示日誌信息
|
| 81 |
+
],
|
| 82 |
+
title="文件翻譯器",
|
| 83 |
+
description="翻譯您的文件,同時保留原始格式。",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
iface.launch()
|