Spaces:
Configuration error
Configuration error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -109,13 +109,11 @@ custom_css = """
|
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
| 112 |
-
# Add this new function after the custom_css definition
|
| 113 |
def split_text(text, min_words=1000, max_words=2000):
|
| 114 |
chunks = []
|
| 115 |
current_chunk = ""
|
| 116 |
current_words = 0
|
| 117 |
paragraphs = text.split('\n')
|
| 118 |
-
|
| 119 |
for paragraph in paragraphs:
|
| 120 |
words = paragraph.split()
|
| 121 |
|
|
@@ -141,24 +139,18 @@ def split_text(text, min_words=1000, max_words=2000):
|
|
| 141 |
current_words = len(sentence_words)
|
| 142 |
else:
|
| 143 |
current_chunk += sentence + ' '
|
| 144 |
-
|
| 145 |
if current_chunk:
|
| 146 |
chunks.append(current_chunk.strip())
|
| 147 |
-
|
| 148 |
return chunks
|
| 149 |
|
| 150 |
-
# Modify the translate function
|
| 151 |
def translate(content_text, language, api_key):
|
| 152 |
start_time = time.time()
|
| 153 |
language_dict = {"繁體中文": "English", "English": "繁體中文"}
|
| 154 |
corr_language = language_dict[language]
|
| 155 |
-
|
| 156 |
-
# Split text into chunks if it's too long
|
| 157 |
text_chunks = split_text(content_text)
|
| 158 |
final_translations = []
|
| 159 |
-
|
| 160 |
for chunk in text_chunks:
|
| 161 |
-
gpt_url="https://api.openai.com/v1"
|
| 162 |
if not api_key:
|
| 163 |
resend.api_key = os.environ["YOUR_API_TOKEN"]
|
| 164 |
params: resend.Emails.SendParams = {
|
|
@@ -232,12 +224,11 @@ Remember: 翻譯規則:
|
|
| 232 |
)
|
| 233 |
result = response.choices[0].message.content.strip()
|
| 234 |
chunk_translation = result.split("### 第五階段:翻譯終稿")[-1].strip()
|
|
|
|
| 235 |
final_translations.append(chunk_translation)
|
| 236 |
-
|
| 237 |
except Exception as e:
|
| 238 |
return f"Error in chunk {len(final_translations) + 1}: {e}", None
|
| 239 |
-
|
| 240 |
-
# Combine all translations
|
| 241 |
final_translation = "\n\n".join(final_translations)
|
| 242 |
|
| 243 |
try:
|
|
|
|
| 109 |
}
|
| 110 |
"""
|
| 111 |
|
|
|
|
| 112 |
def split_text(text, min_words=1000, max_words=2000):
|
| 113 |
chunks = []
|
| 114 |
current_chunk = ""
|
| 115 |
current_words = 0
|
| 116 |
paragraphs = text.split('\n')
|
|
|
|
| 117 |
for paragraph in paragraphs:
|
| 118 |
words = paragraph.split()
|
| 119 |
|
|
|
|
| 139 |
current_words = len(sentence_words)
|
| 140 |
else:
|
| 141 |
current_chunk += sentence + ' '
|
|
|
|
| 142 |
if current_chunk:
|
| 143 |
chunks.append(current_chunk.strip())
|
|
|
|
| 144 |
return chunks
|
| 145 |
|
|
|
|
| 146 |
def translate(content_text, language, api_key):
|
| 147 |
start_time = time.time()
|
| 148 |
language_dict = {"繁體中文": "English", "English": "繁體中文"}
|
| 149 |
corr_language = language_dict[language]
|
|
|
|
|
|
|
| 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 |
if not api_key:
|
| 155 |
resend.api_key = os.environ["YOUR_API_TOKEN"]
|
| 156 |
params: resend.Emails.SendParams = {
|
|
|
|
| 224 |
)
|
| 225 |
result = response.choices[0].message.content.strip()
|
| 226 |
chunk_translation = result.split("### 第五階段:翻譯終稿")[-1].strip()
|
| 227 |
+
print(chunk_translation)
|
| 228 |
final_translations.append(chunk_translation)
|
|
|
|
| 229 |
except Exception as e:
|
| 230 |
return f"Error in chunk {len(final_translations) + 1}: {e}", None
|
| 231 |
+
|
|
|
|
| 232 |
final_translation = "\n\n".join(final_translations)
|
| 233 |
|
| 234 |
try:
|