diff --git a/core/poster_gen.py b/core/poster_gen.py index cfc11b4..e8e9437 100644 --- a/core/poster_gen.py +++ b/core/poster_gen.py @@ -34,15 +34,15 @@ class PosterConfig: except json.JSONDecodeError as e: print(f"JSON解析错误: {e}") print(f"尝试解析的内容: {config_path[:100]}...") # 只打印前100个字符 - # 创建一个默认配置 - self.config = [{"index": 0, "main_title": "", "texts": [""]}] - self.img_list = [[0, "", [""]]] + # 创建一个默认配置,使用空字符串 + self.config = [{"index": 0, "main_title": "", "texts": ["", ""]}] + self.img_list = [[0, "", ["", ""]]] print("使用默认的空配置") except Exception as e: print(f"加载配置时出错: {e}") - # 创建一个默认配置 - self.config = [{"index": 0, "main_title": " ", "texts": [""]}] - self.img_list = [[0, " ", [""]]] + # 创建一个默认配置,使用空字符串 + self.config = [{"index": 0, "main_title": "", "texts": ["", ""]}] + self.img_list = [[0, "", ["", ""]]] print("使用默认的空配置") def get_config(self): diff --git a/utils/tweet_generator.py b/utils/tweet_generator.py index 4c0692b..49333d9 100644 --- a/utils/tweet_generator.py +++ b/utils/tweet_generator.py @@ -63,9 +63,9 @@ class tweetContent: except Exception as e: logging.error(f"Failed to parse AI result for {article_index}_{variant_index}: {e}") logging.debug(f"Raw result: {result[:500]}...") # Log partial raw result - self.title = "[Parsing Error]" - self.content = "[Failed to parse AI content]" - self.json_data = {"title": self.title, "content": self.content, "error": True, "raw_result": result} + self.title = "" # 改为空字符串,而不是 "[Parsing Error]" + self.content = "" # 改为空字符串,而不是 "[Failed to parse AI content]" + self.json_data = {"title": self.title, "content": self.content, "error": True} # 不再包含raw_result def split_content(self, result): # Assuming split logic might still fail, keep it simple or improve with regex/json @@ -75,18 +75,22 @@ class tweetContent: # Optional: Add basic check before splitting if not result or "" not in result or "title>" not in result or "content>" not in result: logging.warning(f"AI result format unexpected: {result[:200]}...") - # Raise error to be caught in __init__ - raise ValueError("AI result missing expected tags or is empty") + # 返回空字符串而不是抛出异常,这样可以在主函数继续处理 + return "", "" # --- Existing Logic (prone to errors) --- - processed_result = result - if "" in result: - processed_result = result.split("", 1)[1] # Take part after - - title = processed_result.split("title>", 1)[1].split("", 1)[0] - content = processed_result.split("content>", 1)[1].split("", 1)[0] - # --- End Existing Logic --- - return title.strip(), content.strip() + try: + processed_result = result + if "" in result: + processed_result = result.split("", 1)[1] # Take part after + + title = processed_result.split("title>", 1)[1].split("", 1)[0] + content = processed_result.split("content>", 1)[1].split("", 1)[0] + # --- End Existing Logic --- + return title.strip(), content.strip() + except Exception as e: + logging.warning(f"解析内容时出错: {e}, 返回空字符串") + return "", "" def gen_result_json(self): json_file = { @@ -161,7 +165,7 @@ def generate_single_content(ai_agent, system_prompt, user_prompt, item, run_id, if result is None: # Check if AI call failed logging.error(f"AI agent work failed for {article_index}_{variant_index}. No result returned.") - return None, None + return {"title": "", "content": "", "error": True}, user_prompt # 返回空字段而不是None logging.info(f"Content generation for {article_index}_{variant_index} completed in {time_cost:.2f}s. Estimated tokens: {tokens}") @@ -187,7 +191,7 @@ def generate_single_content(ai_agent, system_prompt, user_prompt, item, run_id, except Exception as e: logging.exception(f"Error generating single content for {article_index}_{variant_index}:") - return None, None + return {"title": "", "content": "", "error": True}, user_prompt # 返回空字段而不是None def generate_content(ai_agent, system_prompt, topics, output_dir, run_id, prompts_dir, resource_dir, variants=2, temperature=0.3, start_index=0, end_index=None): @@ -417,17 +421,18 @@ def generate_content_for_topic(ai_agent: AI_Agent, presence_penalty # 使用传入的参数 ) - # Check if generation succeeded and parsing was okay (or error handled within json) - if content_json is not None and prompt_data is not None: + # 简化检查,只要content_json不是None就处理它 + # 即使是空标题和内容也是有效的结果 + if content_json is not None: # Use the output handler to process/save the result output_handler.handle_content_variant( - run_id, topic_index, variant_index, content_json, prompt_data + run_id, topic_index, variant_index, content_json, prompt_data or "" ) success_flag = True # Mark success for this topic # Check specifically if the AI result itself indicated a parsing error internally if content_json.get("error"): - logging.error(f" Content generation for Topic {topic_index}, Variant {variant_index} succeeded but response parsing failed (error flag set in content). Raw data logged by handler.") + logging.warning(f" Content generation for Topic {topic_index}, Variant {variant_index} succeeded but response parsing had issues. Using empty content values.") else: logging.info(f" Successfully generated and handled content for Topic {topic_index}, Variant {variant_index}.") else: @@ -437,7 +442,7 @@ def generate_content_for_topic(ai_agent: AI_Agent, logging.exception(f" Error during content generation call or handling for Topic {topic_index}, Variant {variant_index}:") # Return the success flag for this topic - return success_flag + return success_flag def generate_posters_for_topic(topic_item: dict, output_dir: str, @@ -761,7 +766,7 @@ def generate_posters_for_topic(topic_item: dict, # --- Create Poster --- if random.random() < title_possibility: text_data = { - "title": poster_config.get('main_title', 'Default Title'), + "title": poster_config.get('main_title', ''), "subtitle": "", "additional_texts": [] }