Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import os
|
|
3 |
import shutil
|
4 |
from docx import Document
|
5 |
from pptx import Presentation
|
6 |
-
|
7 |
|
8 |
def translate_document(file, target_language, target_country, api_key):
|
9 |
log_messages = []
|
@@ -18,6 +18,8 @@ def translate_document(file, target_language, target_country, api_key):
|
|
18 |
shutil.copy(original_file_path, copied_file_path)
|
19 |
log_messages.append(f"Copied file to {copied_file_path}")
|
20 |
|
|
|
|
|
21 |
# 處理 .docx 文件
|
22 |
if copied_file_path.endswith(".docx"):
|
23 |
doc = Document(copied_file_path)
|
@@ -25,7 +27,10 @@ def translate_document(file, target_language, target_country, api_key):
|
|
25 |
# 翻譯並替換段落文本
|
26 |
for paragraph in doc.paragraphs:
|
27 |
if paragraph.text.strip(): # 只翻譯非空段落
|
28 |
-
translated_text =
|
|
|
|
|
|
|
29 |
paragraph.text = translated_text # 用翻譯後的文本替換原始文本
|
30 |
|
31 |
# 處理表格中的文本
|
@@ -33,16 +38,12 @@ def translate_document(file, target_language, target_country, api_key):
|
|
33 |
for row in table.rows:
|
34 |
for cell in row.cells:
|
35 |
if cell.text.strip(): # 只翻譯非空單元格
|
36 |
-
translated_text =
|
|
|
|
|
|
|
37 |
cell.text = translated_text # 用翻譯後的文本替換原始文本
|
38 |
|
39 |
-
# 處理文本框和形狀中的文本
|
40 |
-
for inline_shape in doc.inline_shapes:
|
41 |
-
if inline_shape.type == 3: # 3 表示文本框
|
42 |
-
if inline_shape.text_frame and inline_shape.text_frame.text.strip():
|
43 |
-
translated_text = GoogleTranslator(source='auto', target=target_language).translate(inline_shape.text_frame.text)
|
44 |
-
inline_shape.text_frame.text = translated_text # 替換文本框中的文本
|
45 |
-
|
46 |
output_path = "translated_" + os.path.basename(original_file_path)
|
47 |
doc.save(output_path)
|
48 |
log_messages.append(f"Saved translated document as {output_path}.")
|
@@ -55,20 +56,12 @@ def translate_document(file, target_language, target_country, api_key):
|
|
55 |
for slide in prs.slides:
|
56 |
for shape in slide.shapes:
|
57 |
if hasattr(shape, "text") and shape.text.strip(): # 只翻譯非空文本框
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
translated_text = GoogleTranslator(source='auto', target=target_language).translate(shape.text)
|
64 |
shape.text = translated_text # 用翻譯後的文本替換原始文本
|
65 |
|
66 |
-
# 恢復原始格式
|
67 |
-
if shape.has_text_frame and original_font_size is not None:
|
68 |
-
for paragraph in shape.text_frame.paragraphs:
|
69 |
-
for run in paragraph.runs:
|
70 |
-
run.font.size = original_font_size
|
71 |
-
|
72 |
output_path = "translated_" + os.path.basename(original_file_path)
|
73 |
prs.save(output_path)
|
74 |
log_messages.append(f"Saved translated presentation as {output_path}.")
|
@@ -87,9 +80,9 @@ iface = gr.Interface(
|
|
87 |
fn=translate_document,
|
88 |
inputs=[
|
89 |
gr.File(label="上傳文件 (.docx 或 .pptx)"),
|
90 |
-
gr.Dropdown(choices=["
|
91 |
gr.Dropdown(choices=["US", "UK", "ES", "FR", "DE", "JP", "KR", "TW", "VN"], label="Target Country"),
|
92 |
-
gr.Textbox(label="API
|
93 |
],
|
94 |
outputs=[
|
95 |
gr.File(label="Download Translated Document"),
|
|
|
3 |
import shutil
|
4 |
from docx import Document
|
5 |
from pptx import Presentation
|
6 |
+
import openai
|
7 |
|
8 |
def translate_document(file, target_language, target_country, api_key):
|
9 |
log_messages = []
|
|
|
18 |
shutil.copy(original_file_path, copied_file_path)
|
19 |
log_messages.append(f"Copied file to {copied_file_path}")
|
20 |
|
21 |
+
openai.api_key = api_key # 設定 OpenAI API 金鑰
|
22 |
+
|
23 |
# 處理 .docx 文件
|
24 |
if copied_file_path.endswith(".docx"):
|
25 |
doc = Document(copied_file_path)
|
|
|
27 |
# 翻譯並替換段落文本
|
28 |
for paragraph in doc.paragraphs:
|
29 |
if paragraph.text.strip(): # 只翻譯非空段落
|
30 |
+
translated_text = openai.ChatCompletion.create(
|
31 |
+
model="gpt-3.5-turbo",
|
32 |
+
messages=[{"role": "user", "content": f"Translate this to {target_language}: {paragraph.text}"}]
|
33 |
+
)["choices"][0]["message"]["content"]
|
34 |
paragraph.text = translated_text # 用翻譯後的文本替換原始文本
|
35 |
|
36 |
# 處理表格中的文本
|
|
|
38 |
for row in table.rows:
|
39 |
for cell in row.cells:
|
40 |
if cell.text.strip(): # 只翻譯非空單元格
|
41 |
+
translated_text = openai.ChatCompletion.create(
|
42 |
+
model="gpt-3.5-turbo",
|
43 |
+
messages=[{"role": "user", "content": f"Translate this to {target_language}: {cell.text}"}]
|
44 |
+
)["choices"][0]["message"]["content"]
|
45 |
cell.text = translated_text # 用翻譯後的文本替換原始文本
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
output_path = "translated_" + os.path.basename(original_file_path)
|
48 |
doc.save(output_path)
|
49 |
log_messages.append(f"Saved translated document as {output_path}.")
|
|
|
56 |
for slide in prs.slides:
|
57 |
for shape in slide.shapes:
|
58 |
if hasattr(shape, "text") and shape.text.strip(): # 只翻譯非空文本框
|
59 |
+
translated_text = openai.ChatCompletion.create(
|
60 |
+
model="gpt-3.5-turbo",
|
61 |
+
messages=[{"role": "user", "content": f"Translate this to {target_language}: {shape.text}"}]
|
62 |
+
)["choices"][0]["message"]["content"]
|
|
|
|
|
63 |
shape.text = translated_text # 用翻譯後的文本替換原始文本
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
output_path = "translated_" + os.path.basename(original_file_path)
|
66 |
prs.save(output_path)
|
67 |
log_messages.append(f"Saved translated presentation as {output_path}.")
|
|
|
80 |
fn=translate_document,
|
81 |
inputs=[
|
82 |
gr.File(label="上傳文件 (.docx 或 .pptx)"),
|
83 |
+
gr.Dropdown(choices=["English", "Spanish", "French", "German", "Japanese", "Korean", "Chinese", "Vietnamese"], label="Target Language"),
|
84 |
gr.Dropdown(choices=["US", "UK", "ES", "FR", "DE", "JP", "KR", "TW", "VN"], label="Target Country"),
|
85 |
+
gr.Textbox(label="API 金鑰(必填)", placeholder="請輸入您的 OpenAI API 金鑰")
|
86 |
],
|
87 |
outputs=[
|
88 |
gr.File(label="Download Translated Document"),
|