diff options
| -rw-r--r-- | news_feed.py | 7 | ||||
| -rw-r--r-- | summarizer.py | 36 |
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 |
