优化了海报生成的效果

This commit is contained in:
jinye_huang 2025-04-18 15:52:31 +08:00
parent 6d09ec65f8
commit 0e16650301
12 changed files with 52 additions and 29 deletions

View File

@ -8,7 +8,7 @@
输出注意点:
1.不需要创作标题和正文,只需要按日期先后顺序罗列选题即可
2.千万不要虚构景区信息没有的设施设备和活动信息,不要虚构活动赠送信息任何活动信息以用户所给明确资料为准
2.千万不要虚构景区信息没有的设施设备和活动信息,不要虚构活动赠送信息,如果材料中没有提到,不要创造价格信息,任何活动信息以用户所给明确资料为准
3.输出内容中,场景要符合自然规律,不要出现不合理的场景;也要符合社会道德规范和法律规范
4.策划选题时按长假节日优先的原则进行选题策划春节、寒暑假、国庆、五一这四个假期要提前一个月进行选题策划举例9月初就要对国庆进行预热其他几个以此类推
即对用户影响较大的节日优先选题

View File

@ -3,7 +3,7 @@
选题风格:根据选题标题,根据我给你的各种文案提示词风格,按用户需求有逻辑的选择不同的风格和面对的用户人群即可,注意只可以在给你的资料中选择,并严谨摘取文件名称,不要自己创作
输出注意点:
1.不需要创作标题和正文,只需要按日期先后顺序罗列选题即可
2.千万不要虚构景区信息没有的设施设备和活动信息,不要虚构活动赠送信息任何活动信息以用户所给明确资料为准
2.千万不要虚构景区信息没有的设施设备和活动信息,不要虚构活动赠送信息,如果材料中没有指明,不要创造价格信息,任何活动信息以用户所给明确资料为准
3.输出内容中,场景要符合自然规律,不要出现不合理的场景;也要符合社会道德规范和法律规范
4.策划选题时按长假节日优先的原则进行选题策划春节、寒暑假、国庆、五一这四个假期要提前一个月进行选题策划举例9月初就要对国庆进行预热其他几个以此类推
即对用户影响较大的节日优先选题

View File

@ -37,7 +37,7 @@ class AI_Agent():
print(f"\nAPI Key: {self.api}")
print(f"Base URL: {self.base_url}")
print(f"Model: {self.model_name}")
time.sleep(random.random())
retry_count = 0
max_retry_wait = 10 # 最大重试等待时间(秒)
@ -146,4 +146,9 @@ class AI_Agent():
time_end = time.time()
time_cost = time_end - time_start
return result, system_prompt, user_prompt, file_folder, result_file, tokens, time_cost
return result, system_prompt, user_prompt, file_folder, result_file, tokens, time_cost
def close(self):
self.client.close()
## del self.client
del self

View File

@ -38,7 +38,9 @@ class PosterGenerator:
self.frame_dir = os.path.join(self.base_dir, "frames") # 边框素材目录
self.sticker_dir = os.path.join(self.base_dir, "stickers") # 贴纸素材目录
self.text_bg_dir = os.path.join(self.base_dir, "text_backgrounds") # 文本框底图目录
self.img_frame_posbility = 0.7
self.text_bg_posbility = 0.7
# 设置输出目录
if output_dir:
self.output_dir = output_dir
@ -217,9 +219,9 @@ class PosterGenerator:
f"宽={self.title_area['width']}, 高={self.title_area['height']}")
# 2. 检查是否使用文本框底图
use_text_bg = os.environ.get('USE_TEXT_BG', 'True').lower() in ('true', '1', 't', 'yes')
use_text_bg = random.random() < self.text_bg_posbility
print(f"环境变量USE_TEXT_BG设置为: {os.environ.get('USE_TEXT_BG', 'True')}, 是否使用文本框底图: {use_text_bg}")
if use_text_bg and self.text_bgs:
print("根据环境变量设置,使用文本框底图")
# 随机选择文本背景
@ -591,7 +593,7 @@ class PosterGenerator:
# 使用模10的余数决定是否添加边框
# 每十张中的第1、5、9张添加边框(余数为1,5,9)
add_frame_flag = (self.poster_count % 10) in (1, 5, 9)
add_frame_flag = random.random() < self.img_frame_posbility
if add_frame_flag:
final_image = self.add_frame(final_image, target_size)

View File

@ -18,7 +18,7 @@ class ImageCollageCreator:
"grid_2x2", # 标准2x2网格
"asymmetric", # 非对称布局
"filmstrip", # 胶片条布局
"circles", # 圆形布局
# "circles", # 圆形布局
"overlap", # 重叠风格
"mosaic", # 马赛克风格 3x3
"fullscreen", # 全覆盖拼图样式

View File

@ -17,7 +17,7 @@
输出注意点:
1.按要求输出标签+正文+TAG标签
2.正文直白清楚围绕产品,比喻和过度联想不要出现
3.千万不要虚构景区信息没有的设施设备和活动信息
3.千万不要虚构景区信息没有的设施设备和活动信息,如果材料中没有指明,不要提及价格信息;如果没有明确的产品价格或者优惠活动信息,不要提及价格和优惠
4.输出内容中,场景要符合自然规律,不要出现不合理的场景;也要符合社会道德规范和法律规范
5.TAG标签要按照从景区所在地到景区名称到景点名称到具体特色活动到面向人群的顺序进行罗列

54
main.py
View File

@ -3,6 +3,7 @@ import time
from datetime import datetime
import argparse
import sys
import traceback
from core.ai_agent import AI_Agent
from core.topic_parser import TopicParser
@ -12,10 +13,13 @@ import core.simple_collage as simple_collage
from utils.resource_loader import ResourceLoader
from utils.tweet_generator import prepare_topic_generation, generate_topics, generate_single_content
import random
TEXT_POSBILITY = 0.3
def main():
config_file = {
"date": "4月17日",
"num": 10,
"num": 5,
"model": "qwenQWQ",
"api_url": "vllm",
"api_key": "EMPTY",
@ -59,6 +63,7 @@ def main():
os.makedirs(output_dir, exist_ok=True)
tweet_topic_record.save_topics(os.path.join(output_dir, "tweet_topic.json"))
tweet_topic_record.save_prompt(os.path.join(output_dir, "tweet_prompt.txt"))
ai_agent.close()
# raise Exception("选题生成失败,退出程序")
if not run_id or not tweet_topic_record:
print("选题生成失败,退出程序")
@ -79,12 +84,15 @@ def main():
for i in range(len(tweet_topic_record.topics_list)):
tweet_content_list = []
for j in range(config_file["variants"]):
time.sleep(random.random())
ai_agent = AI_Agent(config_file["api_url"], config_file["model"], config_file["api_key"])
tweet_content, gen_result = generate_single_content(
ai_agent, content_system_prompt, tweet_topic_record.topics_list[i],
config_file["prompts_dir"], config_file["resource_dir"],
output_dir, run_id, i+1, j+1, config_file["content_temperature"]
)
tweet_content_list.append(tweet_content.get_json_file())
ai_agent.close()
if not tweet_content:
print(f"生成第{i+1}篇文章的第{j+1}个变体失败,跳过")
continue
@ -108,7 +116,6 @@ def main():
if not os.path.exists(img_dir_path):
print(f"图片目录不存在:{img_dir_path},跳过该对象")
continue
img_dir = os.path.join(output_dir, f"{i+1}_{j_index+1}")
info_directory = [
f"/root/autodl-tmp/sanming_img/相机/{object_name}/description.txt"
]
@ -126,7 +133,10 @@ def main():
content_gen = contentGen.ContentGenerator()
response = content_gen.run(info_directory, poster_num, tweet_content_list)
print(response)
poster_config_summary = posterGen.PosterConfig(response)
for j_index in range(config_file["variants"]):
poster_config = poster_config_summary.get_config_by_index(j_index)
img_dir = os.path.join(output_dir, f"{i+1}_{j_index+1}")
try:
# 创建输出目录
collage_output_dir = os.path.join(img_dir, "collage_img")
@ -138,7 +148,7 @@ def main():
img_list = simple_collage.process_directory(
input_dir,
target_size=target_size,
output_count=poster_num,
output_count=1,
output_dir=collage_output_dir
)
print(img_list)
@ -149,27 +159,33 @@ def main():
# 生成海报
poster_gen = posterGen.PosterGenerator()
poster_config = posterGen.PosterConfig(response)
for index, item in enumerate(poster_config.get_config()):
if index >= len(img_list):
print(f"配置项数量大于图片数量,跳过后续配置")
break
if random.random() < TEXT_POSBILITY:
text_data = {
"title": f"{item['main_title']}",
"subtitle": "",
"additional_texts": [
{"text": f"{item['texts'][0]}", "position": "bottom", "size_factor": 0.5},
{"text": f"{item['texts'][1]}", "position": "bottom", "size_factor": 0.5}
]
"title": f"{poster_config['main_title']}",
"subtitle": "",
"additional_texts": [
{"text": f"{poster_config['texts'][0]}", "position": "bottom", "size_factor": 0.5},
{"text": f"{poster_config['texts'][1]}", "position": "bottom", "size_factor": 0.5}
]
}
img_path = img_list[index]['path']
print(f"使用图片路径: {img_path}")
output_path = os.path.join(poster_output_dir, f"img_{i+1}_{j_index+1}_{index}.jpg")
result_path.append(poster_gen.create_poster(img_path, text_data, output_path))
else:
text_data = {
"title": f"{poster_config['main_title']}",
"subtitle": "",
"additional_texts": [
{"text": f"{poster_config['texts'][0]}", "position": "bottom", "size_factor": 0.5},
# {"text": f"{poster_config['texts'][1]}", "position": "bottom", "size_factor": 0.5}
]
}
print(text_data)
img_path = img_list[0]['path']
print(f"使用图片路径: {img_path}")
output_path = os.path.join(poster_output_dir, f"poster.jpg")
result_path.append(poster_gen.create_poster(img_path, text_data, output_path))
except Exception as e:
print(f"海报生成过程中出错: {e}")
traceback.print_exc() # 打印完整的堆栈跟踪信息
continue
if __name__ == "__main__":