aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2025-09-17 16:07:06 -0700
committerPinapelz <yukais@pinapelz.com>2025-09-17 16:07:06 -0700
commita3f00e15f7c1657891787be0b21813c2f4aeaca9 (patch)
treef028dbdaed656b48f7de64ee94590cdfd86f825f
parent74fb827a4b24625d07a7e234dba59d844ba2b155 (diff)
add default entries and handle summarization API errors
-rw-r--r--news_feed.py7
-rw-r--r--summarizer.py36
2 files changed, 27 insertions, 16 deletions
diff --git a/news_feed.py b/news_feed.py
index 3c158d9..2131676 100644
--- a/news_feed.py
+++ b/news_feed.py
@@ -43,11 +43,18 @@ import constants
import translate
import summarizer
+from datetime import datetime
+
def _attach_llm_summaries(news_posts: list, game_name: str):
for post in news_posts:
image_urls = [img["image"] for img in post.get("images", []) if "image" in img]
if image_urls:
headline, content = summarizer.generate_headline_and_content_from_images(image_urls, game_name)
+ if headline is None and content is None:
+ datetime_str = datetime.now().strftime("%H:%M:%S")
+ post["headline"] = f"{game_name} Update"
+ post["content"] = f"573-UPDATES has found a news post for {game_name} at {datetime_str}, please refer to the image for more details!"
+ post["is_ai_summary"] = False
post["headline"] = headline
post["content"] = content
post["is_ai_summary"] = True
diff --git a/summarizer.py b/summarizer.py
index 6e5e064..8fe86ae 100644
--- a/summarizer.py
+++ b/summarizer.py
@@ -33,7 +33,7 @@ def _make_cache_key(game: str, img_urls: list[str]) -> str:
return f"{normalized_game}_{hash_digest}"
-def generate_headline_and_content_from_images(img_urls: list[str], game: str, message_content: str=""):
+def generate_headline_and_content_from_images(img_urls: list[str], game: str, message_content: str="") -> tuple:
"""
Uses LLM to generate the headline and content when none provided by source, based on one or more images.
"""
@@ -85,20 +85,24 @@ def generate_headline_and_content_from_images(img_urls: list[str], game: str, me
}
]
- response = openai.chat.completions.create(
- model="gpt-5",
- messages=messages,
- tools=tools,
- tool_choice={
- "type": "function",
- "function": {"name": "generate_update_text"},
- },
- )
+ try:
+ response = openai.chat.completions.create(
+ model="gpt-5",
+ messages=messages,
+ tools=tools,
+ tool_choice={
+ "type": "function",
+ "function": {"name": "generate_update_text"},
+ },
+ )
- tool_result = response.choices[0].message.tool_calls[0].function.arguments
- parsed_result = json.loads(tool_result)
- headline = parsed_result["headline"]
- content = parsed_result["content"]
- cache[cache_key] = {"headline": headline, "content": content}
- _save_cache(cache)
+ tool_result = response.choices[0].message.tool_calls[0].function.arguments
+ parsed_result = json.loads(tool_result)
+ headline = parsed_result["headline"]
+ content = parsed_result["content"]
+ cache[cache_key] = {"headline": headline, "content": content}
+ _save_cache(cache)
+ except openai.OpenAIError as e:
+ print(f"[ERROR] Function call to OpenAI for summarization failed ERROR -> {e} ")
+ return None, None
return headline, content
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage