Skip to content

Commit

Permalink
feat:add max_retries to translate_feed function
Browse files Browse the repository at this point in the history
  • Loading branch information
versun committed Sep 27, 2024
1 parent 1f731fe commit 697cf2f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
61 changes: 42 additions & 19 deletions core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,19 @@ def translate_feed(
) # check cache db
translated_text = ""
if not cached:
results = translate_engine.translate(
title, target_language=target_language, source_language=source_language, text_type="title"
)
translated_text = results.get("text", title)
max_retries = 3
for attempt in range(max_retries):
results = translate_engine.translate(
title, target_language=target_language, source_language=source_language, text_type="title"
)
translated_text = results.get("text", "")
if translated_text:
break
logging.warning(f"Empty translation for title, retrying (attempt {attempt + 1}/{max_retries})")

if not translated_text:
translated_text = title # Fallback to original title if all retries fail

total_tokens += results.get("tokens", 0)
translated_characters += len(title)
if title and translated_text:
Expand Down Expand Up @@ -307,8 +316,6 @@ def translate_feed(

# Translate content
if translate_engine and translate_content:
# logging.info("Start Translate Content")
# original_description = entry.get('summary', None) # summary, description
original_content = entry.get("content")
content = (
original_content[0].get("value")
Expand All @@ -317,11 +324,20 @@ def translate_feed(
)

if content:
translated_summary, tokens, characters, need_cache = (
content_translate(
content, target_language, translate_engine, quality, source_language=source_language
max_retries = 3
for attempt in range(max_retries):
translated_summary, tokens, characters, need_cache = (
content_translate(
content, target_language, translate_engine, quality, source_language=source_language
)
)
)
if translated_summary:
break
logging.warning(f"Empty translation for content, retrying (attempt {attempt + 1}/{max_retries})")

if not translated_summary:
translated_summary = content # Fallback to original content if all retries fail

total_tokens += tokens
translated_characters += characters

Expand All @@ -343,8 +359,6 @@ def translate_feed(
if summary_engine == None:
logging.warning("No Summarize engine")
continue
# logging.info("Start Summarize")
# original_description = entry.get('summary') # summary, description
original_content = entry.get("content")
content = (
original_content[0].get("value")
Expand All @@ -353,13 +367,22 @@ def translate_feed(
)

if content:
summary_text, tokens, need_cache = content_summarize(
content,
target_language=target_language,
detail=summary_detail,
engine=summary_engine,
minimum_chunk_size=summary_engine.max_size(),
)
max_retries = 3
for attempt in range(max_retries):
summary_text, tokens, need_cache = content_summarize(
content,
target_language=target_language,
detail=summary_detail,
engine=summary_engine,
minimum_chunk_size=summary_engine.max_size(),
)
if summary_text:
break
logging.warning(f"Empty summary, retrying (attempt {attempt + 1}/{max_retries})")

if not summary_text:
summary_text = content # Fallback to original content if all retries fail

total_tokens += tokens
need_cache_objs.update(need_cache)
html_summary = f"<br />🤖:{mistune.html(summary_text)}<br />---------------<br />"
Expand Down
2 changes: 1 addition & 1 deletion templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<div id="footer" style="text-align: center;">
<a href="https://rsstranslator.com" title="RSS Translator">RSS Translator</a> ·
<a href="https://afdian.com/a/versun" title="Donate">Donate</a> ·
Version: <span data-version="2024.8.30">2024.8.30</span>
Version: <span data-version="2024.9.28">2024.9.28</span>
<svg id="update-button" class="svg-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" onclick="checkUpdate()">
<title>Check Updates</title>
<path d="M783.568 328C724.571 241.095 624.953 184 512 184c-181.15 0-328 146.85-328 328h-72c0-220.914 179.086-400 400-400 135.738 0 255.685 67.611 328 170.994V128c0-8.837 7.163-16 16-16h48a8 8 0 0 1 8 8v248c0 17.673-14.327 32-32 32H632a8 8 0 0 1-8-8v-48c0-8.837 7.163-16 16-16h143.568zM240.432 696C299.429 782.905 399.047 840 512 840c181.15 0 328-146.85 328-328h72c0 220.914-179.086 400-400 400-135.738 0-255.685-67.611-328-170.994V896c0 8.837-7.163 16-16 16h-48a8 8 0 0 1-8-8V656c0-17.673 14.327-32 32-32h248a8 8 0 0 1 8 8v48c0 8.837-7.163 16-16 16H240.432z"/>
Expand Down

0 comments on commit 697cf2f

Please sign in to comment.