修改了json配置读取
This commit is contained in:
parent
3037984fbe
commit
202ca01316
@ -14,7 +14,7 @@
|
||||
- `date`: 选题日期 (例如 "2024-06-15")
|
||||
- `logic`: 选定逻辑内容 (描述性文本)
|
||||
- `object`: 选定对象 (例如 "泰宁古城")
|
||||
- `product`: 选定产品内容 (如果没有单独产品,则为景点本身,例如 "尚书第建筑群+古城游览区")
|
||||
- `product`: 选定产品内容 (如果没有提供单独产品,则为"None")
|
||||
- `product_logic`: 选定产品的逻辑内容 (描述性文本)
|
||||
- `style`: 选题风格的文件名。**必须是从 Style 文件夹中选择的完整文件名,例如 "攻略风文案提示词.txt"。**
|
||||
- `style_logic`: 选题风格的逻辑内容 (描述性文本)
|
||||
|
||||
Binary file not shown.
@ -354,17 +354,139 @@ class ContentGenerator:
|
||||
# 生成时间戳
|
||||
print(full_response)
|
||||
date_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
full_response = self.split_content(full_response)
|
||||
# 创建结果文件路径
|
||||
result_path = os.path.join(self.output_dir, f"{date_time}.json")
|
||||
os.makedirs(os.path.dirname(result_path), exist_ok=True)
|
||||
|
||||
# 保存结果到文件
|
||||
with open(result_path, "w", encoding="utf-8") as f:
|
||||
json.dump(full_response, f, ensure_ascii=False)
|
||||
try:
|
||||
# 解析内容为JSON格式
|
||||
parsed_data = self.split_content(full_response)
|
||||
|
||||
# 验证内容格式并修复
|
||||
validated_data = self._validate_and_fix_data(parsed_data)
|
||||
|
||||
# 创建结果文件路径
|
||||
result_path = os.path.join(self.output_dir, f"{date_time}.json")
|
||||
os.makedirs(os.path.dirname(result_path), exist_ok=True)
|
||||
|
||||
# 保存结果到文件
|
||||
with open(result_path, "w", encoding="utf-8") as f:
|
||||
json.dump(validated_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
print(f"结果已保存到: {result_path}")
|
||||
return result_path
|
||||
|
||||
print(f"结果已保存到: {result_path}")
|
||||
return result_path
|
||||
except Exception as e:
|
||||
self.logger.error(f"保存结果到文件时出错: {e}")
|
||||
# 尝试创建一个简单的备用配置
|
||||
fallback_data = [{"main_title": "景点风光", "texts": ["自然美景", "人文体验"], "index": 1}]
|
||||
|
||||
# 保存备用数据
|
||||
result_path = os.path.join(self.output_dir, f"{date_time}_fallback.json")
|
||||
os.makedirs(os.path.dirname(result_path), exist_ok=True)
|
||||
|
||||
with open(result_path, "w", encoding="utf-8") as f:
|
||||
json.dump(fallback_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
print(f"出错后已保存备用数据到: {result_path}")
|
||||
return result_path
|
||||
|
||||
def _validate_and_fix_data(self, data):
|
||||
"""
|
||||
验证并修复数据格式,确保符合预期结构
|
||||
|
||||
参数:
|
||||
data: 需要验证的数据
|
||||
|
||||
返回:
|
||||
修复后的数据
|
||||
"""
|
||||
fixed_data = []
|
||||
|
||||
# 如果数据是列表
|
||||
if isinstance(data, list):
|
||||
for i, item in enumerate(data):
|
||||
# 检查项目是否为字典
|
||||
if isinstance(item, dict):
|
||||
# 确保必需字段存在
|
||||
fixed_item = {
|
||||
"index": item.get("index", i + 1),
|
||||
"main_title": item.get("main_title", f"景点风光 {i+1}"),
|
||||
"texts": item.get("texts", ["自然美景", "人文体验"])
|
||||
}
|
||||
|
||||
# 确保texts是列表格式
|
||||
if not isinstance(fixed_item["texts"], list):
|
||||
if isinstance(fixed_item["texts"], str):
|
||||
fixed_item["texts"] = [fixed_item["texts"], "美景体验"]
|
||||
else:
|
||||
fixed_item["texts"] = ["自然美景", "人文体验"]
|
||||
|
||||
# 限制texts最多包含两个元素
|
||||
if len(fixed_item["texts"]) > 2:
|
||||
fixed_item["texts"] = fixed_item["texts"][:2]
|
||||
elif len(fixed_item["texts"]) < 2:
|
||||
while len(fixed_item["texts"]) < 2:
|
||||
fixed_item["texts"].append("美景体验")
|
||||
|
||||
fixed_data.append(fixed_item)
|
||||
|
||||
# 如果项目是字符串(可能是错误格式的texts值)
|
||||
elif isinstance(item, str):
|
||||
self.logger.warning(f"配置项 {i+1} 是字符串格式,将转换为标准格式")
|
||||
fixed_item = {
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": [item, "美景体验"]
|
||||
}
|
||||
fixed_data.append(fixed_item)
|
||||
else:
|
||||
self.logger.warning(f"配置项 {i+1} 格式不支持: {type(item)},将使用默认值")
|
||||
fixed_data.append({
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
|
||||
# 如果数据是字典
|
||||
elif isinstance(data, dict):
|
||||
fixed_item = {
|
||||
"index": data.get("index", 1),
|
||||
"main_title": data.get("main_title", "景点风光"),
|
||||
"texts": data.get("texts", ["自然美景", "人文体验"])
|
||||
}
|
||||
|
||||
# 确保texts是列表格式
|
||||
if not isinstance(fixed_item["texts"], list):
|
||||
if isinstance(fixed_item["texts"], str):
|
||||
fixed_item["texts"] = [fixed_item["texts"], "美景体验"]
|
||||
else:
|
||||
fixed_item["texts"] = ["自然美景", "人文体验"]
|
||||
|
||||
# 限制texts最多包含两个元素
|
||||
if len(fixed_item["texts"]) > 2:
|
||||
fixed_item["texts"] = fixed_item["texts"][:2]
|
||||
elif len(fixed_item["texts"]) < 2:
|
||||
while len(fixed_item["texts"]) < 2:
|
||||
fixed_item["texts"].append("美景体验")
|
||||
|
||||
fixed_data.append(fixed_item)
|
||||
|
||||
# 如果数据是字符串或其他格式
|
||||
else:
|
||||
self.logger.warning(f"数据格式不支持: {type(data)},将使用默认值")
|
||||
fixed_data.append({
|
||||
"index": 1,
|
||||
"main_title": "景点风光",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
|
||||
# 确保至少有一个配置项
|
||||
if not fixed_data:
|
||||
fixed_data.append({
|
||||
"index": 1,
|
||||
"main_title": "景点风光",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
|
||||
return fixed_data
|
||||
|
||||
def run(self, info_directory, poster_num, tweet_content):
|
||||
"""
|
||||
@ -392,31 +514,90 @@ class ContentGenerator:
|
||||
try:
|
||||
result_data = self.split_content(full_response) # This should return the list/dict
|
||||
|
||||
# 验证结果数据格式
|
||||
# 验证并修复结果数据格式
|
||||
fixed_data = []
|
||||
|
||||
# 如果结果是列表,检查每个项目
|
||||
if isinstance(result_data, list):
|
||||
for i, item in enumerate(result_data):
|
||||
if not isinstance(item, dict):
|
||||
logging.warning(f"配置项 {i+1} 不是字典格式: {item}")
|
||||
continue
|
||||
|
||||
# 检查并确保必需字段存在
|
||||
if 'main_title' not in item:
|
||||
item['main_title'] = f"景点标题 {i+1}"
|
||||
logging.warning(f"配置项 {i+1} 缺少 main_title 字段,已添加默认值")
|
||||
|
||||
if 'texts' not in item:
|
||||
item['texts'] = ["景点特色", "游玩体验"]
|
||||
logging.warning(f"配置项 {i+1} 缺少 texts 字段,已添加默认值")
|
||||
|
||||
logging.info(f"成功生成并解析海报配置数据,包含 {len(result_data)} 个项目")
|
||||
else:
|
||||
logging.warning(f"生成的配置数据不是列表格式: {type(result_data)}")
|
||||
# 如果项目是字典并且有required_fields,按原样添加或修复
|
||||
if isinstance(item, dict):
|
||||
# 检查并确保必需字段存在
|
||||
if 'main_title' not in item:
|
||||
item['main_title'] = f"景点标题 {i+1}"
|
||||
logging.warning(f"配置项 {i+1} 缺少 main_title 字段,已添加默认值")
|
||||
|
||||
if 'texts' not in item:
|
||||
item['texts'] = ["景点特色", "游玩体验"]
|
||||
logging.warning(f"配置项 {i+1} 缺少 texts 字段,已添加默认值")
|
||||
|
||||
if 'index' not in item:
|
||||
item['index'] = i + 1
|
||||
logging.warning(f"配置项 {i+1} 缺少 index 字段,已添加默认值")
|
||||
|
||||
fixed_data.append(item)
|
||||
# 如果项目是字符串(可能是错误格式的texts值)
|
||||
elif isinstance(item, str):
|
||||
logging.warning(f"配置项 {i+1} 是字符串格式,将转换为标准格式")
|
||||
fixed_item = {
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": [item, "美景体验"]
|
||||
}
|
||||
fixed_data.append(fixed_item)
|
||||
else:
|
||||
logging.warning(f"配置项 {i+1} 格式不支持: {type(item)},将使用默认值")
|
||||
fixed_data.append({
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
|
||||
# 如果处理后的列表为空(极端情况),则使用默认值
|
||||
if not fixed_data:
|
||||
logging.warning("处理后的配置列表为空,使用默认值")
|
||||
for i in range(poster_num):
|
||||
fixed_data.append({
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
|
||||
logging.info(f"成功生成并修复海报配置数据,包含 {len(fixed_data)} 个项目")
|
||||
return fixed_data
|
||||
|
||||
# 如果结果是单个字典(不常见但可能),将其转换为列表
|
||||
elif isinstance(result_data, dict):
|
||||
logging.warning(f"生成的配置数据是单个字典格式,将转换为列表")
|
||||
|
||||
# 检查并确保必需字段存在
|
||||
if 'main_title' not in result_data:
|
||||
result_data['main_title'] = "景点风光"
|
||||
|
||||
if 'texts' not in result_data:
|
||||
result_data['texts'] = ["自然美景", "人文体验"]
|
||||
|
||||
if 'index' not in result_data:
|
||||
result_data['index'] = 1
|
||||
|
||||
fixed_data = [result_data]
|
||||
return fixed_data
|
||||
|
||||
# 如果结果是其他格式(如字符串),创建默认配置
|
||||
else:
|
||||
logging.warning(f"生成的配置数据格式不支持: {type(result_data)},将使用默认值")
|
||||
for i in range(poster_num):
|
||||
fixed_data.append({
|
||||
"index": i + 1,
|
||||
"main_title": f"景点风光 {i+1}",
|
||||
"texts": ["自然美景", "人文体验"]
|
||||
})
|
||||
return fixed_data
|
||||
|
||||
return result_data # Return the actual data
|
||||
except Exception as e:
|
||||
logging.exception(f"Failed to parse JSON from AI response in ContentGenerator: {e}\nRaw Response:\n{full_response[:500]}...") # Log error and partial response
|
||||
|
||||
# 失败后尝试创建一个默认配置
|
||||
# 失败后创建一个默认配置
|
||||
logging.info("创建默认海报配置数据")
|
||||
default_configs = []
|
||||
for i in range(poster_num):
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"date": "5月15日, 5月16日, 5月17日, 6月1日",
|
||||
"num": 30,
|
||||
"num": 2,
|
||||
"model": "qwenQWQ",
|
||||
"api_url": "http://localhost:8000/v1/",
|
||||
"api_key": "EMPTY",
|
||||
|
||||
Binary file not shown.
@ -57,7 +57,7 @@ class tweetContent:
|
||||
self.variant_index = variant_index
|
||||
|
||||
try:
|
||||
self.title, self.content = self.split_content(result)
|
||||
self.title, self.content = self.split_content(result)
|
||||
self.json_data = self.gen_result_json()
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to parse AI result for {article_index}_{variant_index}: {e}")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user