Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -109,7 +109,7 @@ custom_css = """
|
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
| 112 |
-
def split_text(text, min_words=
|
| 113 |
chunks = []
|
| 114 |
current_chunk = ""
|
| 115 |
current_words = 0
|
|
@@ -150,6 +150,7 @@ def translate(content_text, language, api_key):
|
|
| 150 |
text_chunks = split_text(content_text)
|
| 151 |
final_translations = []
|
| 152 |
gpt_url="https://api.openai.com/v1"
|
|
|
|
| 153 |
for chunk in text_chunks:
|
| 154 |
print(chunk)
|
| 155 |
if not api_key:
|
|
@@ -225,13 +226,12 @@ Remember: 翻譯規則:
|
|
| 225 |
)
|
| 226 |
result = response.choices[0].message.content.strip()
|
| 227 |
chunk_translation = result.split("### 第五階段:翻譯終稿")[-1].strip()
|
| 228 |
-
print(chunk_translation)
|
| 229 |
final_translations.append(chunk_translation)
|
| 230 |
except Exception as e:
|
| 231 |
return f"Error in chunk {len(final_translations) + 1}:{e}", None
|
| 232 |
|
| 233 |
final_translation = "\n\n".join(final_translations)
|
| 234 |
-
|
| 235 |
try:
|
| 236 |
with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8', suffix='.txt', delete=False) as temp_file:
|
| 237 |
temp_file.write(f"【原稿({language_dict[language]})】\n\n{content_text}\n\n")
|
|
|
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
| 112 |
+
def split_text(text, min_words=400, max_words=800):
|
| 113 |
chunks = []
|
| 114 |
current_chunk = ""
|
| 115 |
current_words = 0
|
|
|
|
| 150 |
text_chunks = split_text(content_text)
|
| 151 |
final_translations = []
|
| 152 |
gpt_url="https://api.openai.com/v1"
|
| 153 |
+
gr.Info(f"文章越長,翻譯時間越久,請耐心等候。")
|
| 154 |
for chunk in text_chunks:
|
| 155 |
print(chunk)
|
| 156 |
if not api_key:
|
|
|
|
| 226 |
)
|
| 227 |
result = response.choices[0].message.content.strip()
|
| 228 |
chunk_translation = result.split("### 第五階段:翻譯終稿")[-1].strip()
|
|
|
|
| 229 |
final_translations.append(chunk_translation)
|
| 230 |
except Exception as e:
|
| 231 |
return f"Error in chunk {len(final_translations) + 1}:{e}", None
|
| 232 |
|
| 233 |
final_translation = "\n\n".join(final_translations)
|
| 234 |
+
print(final_translation)
|
| 235 |
try:
|
| 236 |
with tempfile.NamedTemporaryFile(mode='w+', encoding='utf-8', suffix='.txt', delete=False) as temp_file:
|
| 237 |
temp_file.write(f"【原稿({language_dict[language]})】\n\n{content_text}\n\n")
|