- 新增DeepSeek照片建议生成器模块 - 在照片质量评分和美学评分中集成DeepSeek智能建议 - 支持个性化改进建议、美学指导和综合评估 - 添加DeepSeek配置状态显示和错误处理机制
313 lines
12 KiB
Python
313 lines
12 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
DeepSeek照片建议生成器
|
||
使用DeepSeek大模型为照片评分结果生成具体、个性化的改进建议
|
||
"""
|
||
|
||
import os
|
||
import json
|
||
import requests
|
||
from dotenv import load_dotenv
|
||
|
||
# 加载环境变量
|
||
load_dotenv()
|
||
|
||
class DeepSeekPhotoAdvisor:
|
||
"""DeepSeek照片建议生成器类"""
|
||
|
||
def __init__(self):
|
||
"""初始化DeepSeek客户端"""
|
||
self.api_key = os.getenv('DEEPSEEK_API_KEY')
|
||
if not self.api_key:
|
||
raise Exception("DeepSeek API密钥未配置,请在.env文件中设置DEEPSEEK_API_KEY")
|
||
|
||
self.base_url = "https://api.deepseek.com/v1/chat/completions"
|
||
|
||
def generate_quality_advice(self, quality_scores, photo_description=""):
|
||
"""
|
||
生成照片质量改进建议
|
||
|
||
Args:
|
||
quality_scores: 质量评分字典,包含各维度分数
|
||
photo_description: 照片内容描述
|
||
|
||
Returns:
|
||
dict: 包含详细建议的字典
|
||
"""
|
||
try:
|
||
# 构建提示词
|
||
prompt = self._build_quality_advice_prompt(quality_scores, photo_description)
|
||
|
||
# 调用DeepSeek API
|
||
response = self._call_deepseek_api(prompt)
|
||
|
||
# 解析响应
|
||
advice_data = self._parse_advice_response(response)
|
||
|
||
return advice_data
|
||
|
||
except Exception as e:
|
||
# 如果API调用失败,返回备用建议
|
||
return self._generate_fallback_quality_advice(quality_scores)
|
||
|
||
def generate_aesthetic_advice(self, aesthetic_scores, photo_description=""):
|
||
"""
|
||
生成照片美学改进建议
|
||
|
||
Args:
|
||
aesthetic_scores: 美学评分字典
|
||
photo_description: 照片内容描述
|
||
|
||
Returns:
|
||
dict: 包含详细美学建议的字典
|
||
"""
|
||
try:
|
||
prompt = self._build_aesthetic_advice_prompt(aesthetic_scores, photo_description)
|
||
response = self._call_deepseek_api(prompt)
|
||
advice_data = self._parse_advice_response(response)
|
||
return advice_data
|
||
|
||
except Exception as e:
|
||
return self._generate_fallback_aesthetic_advice(aesthetic_scores)
|
||
|
||
def generate_comprehensive_advice(self, quality_scores, aesthetic_scores, photo_description):
|
||
"""
|
||
生成综合改进建议
|
||
|
||
Args:
|
||
quality_scores: 质量评分
|
||
aesthetic_scores: 美学评分
|
||
photo_description: 照片描述
|
||
|
||
Returns:
|
||
dict: 综合建议
|
||
"""
|
||
try:
|
||
prompt = self._build_comprehensive_prompt(quality_scores, aesthetic_scores, photo_description)
|
||
response = self._call_deepseek_api(prompt)
|
||
advice_data = self._parse_advice_response(response)
|
||
return advice_data
|
||
|
||
except Exception as e:
|
||
return self._generate_fallback_comprehensive_advice(quality_scores, aesthetic_scores)
|
||
|
||
def _build_quality_advice_prompt(self, quality_scores, photo_description):
|
||
"""构建质量建议提示词"""
|
||
prompt = f"""
|
||
你是一名专业的摄影指导专家,请根据以下照片质量评分结果,为摄影师提供具体、可操作的改进建议。
|
||
|
||
照片描述:{photo_description if photo_description else "未提供具体描述"}
|
||
|
||
质量评分结果:
|
||
{json.dumps(quality_scores, indent=2, ensure_ascii=False)}
|
||
|
||
请按照以下结构提供建议:
|
||
1. 总体评价:简要总结照片质量状况
|
||
2. 优势分析:指出表现较好的方面
|
||
3. 改进重点:按优先级列出需要改进的维度
|
||
4. 具体建议:为每个需要改进的维度提供3-5条具体、可操作的建议
|
||
5. 学习资源:推荐相关学习内容
|
||
|
||
请用JSON格式返回,结构如下:
|
||
{{
|
||
"overall_evaluation": "总体评价",
|
||
"strengths": ["优势1", "优势2"],
|
||
"priority_improvements": ["优先级1", "优先级2"],
|
||
"specific_advice": {{
|
||
"维度名称": ["建议1", "建议2", "建议3"]
|
||
}},
|
||
"learning_resources": ["资源1", "资源2"]
|
||
}}
|
||
|
||
请确保建议具体、实用,适合摄影爱好者理解。
|
||
"""
|
||
return prompt.strip()
|
||
|
||
def _build_aesthetic_advice_prompt(self, aesthetic_scores, photo_description):
|
||
"""构建美学建议提示词"""
|
||
prompt = f"""
|
||
你是一名专业的摄影美学指导专家,请根据以下照片美学评分结果,为摄影师提供具体的美学改进建议。
|
||
|
||
照片描述:{photo_description if photo_description else "未提供具体描述"}
|
||
|
||
美学评分结果:
|
||
{json.dumps(aesthetic_scores, indent=2, ensure_ascii=False)}
|
||
|
||
请按照以下结构提供建议:
|
||
1. 美学评价:从艺术角度评价照片
|
||
2. 构图建议:针对构图提供具体改进方案
|
||
3. 色彩建议:针对色彩和谐度提供建议
|
||
4. 光线建议:针对光线运用提供指导
|
||
5. 创意提升:提供创意性建议
|
||
|
||
请用JSON格式返回,结构如下:
|
||
{{
|
||
"aesthetic_evaluation": "美学评价",
|
||
"composition_advice": ["构图建议1", "构图建议2"],
|
||
"color_advice": ["色彩建议1", "色彩建议2"],
|
||
"lighting_advice": ["光线建议1", "光线建议2"],
|
||
"creative_suggestions": ["创意建议1", "创意建议2"]
|
||
}}
|
||
|
||
请确保建议具有艺术性和实用性。
|
||
"""
|
||
return prompt.strip()
|
||
|
||
def _build_comprehensive_prompt(self, quality_scores, aesthetic_scores, photo_description):
|
||
"""构建综合建议提示词"""
|
||
prompt = f"""
|
||
你是一名资深的摄影导师,请根据以下照片的全面评估结果,为摄影师提供综合性的改进建议和学习计划。
|
||
|
||
照片描述:{photo_description}
|
||
|
||
质量评分:
|
||
{json.dumps(quality_scores, indent=2, ensure_ascii=False)}
|
||
|
||
美学评分:
|
||
{json.dumps(aesthetic_scores, indent=2, ensure_ascii=False)}
|
||
|
||
请提供:
|
||
1. 综合评估:结合技术和艺术角度全面评价
|
||
2. 个性化学习计划:根据评分结果制定针对性学习路径
|
||
3. 短期目标:可立即实施的改进措施
|
||
4. 长期规划:提升摄影水平的长期建议
|
||
5. 练习建议:具体的拍摄练习方案
|
||
|
||
请用JSON格式返回,结构如下:
|
||
{{
|
||
"comprehensive_evaluation": "综合评估",
|
||
"learning_plan": {{
|
||
"short_term": ["短期目标1", "短期目标2"],
|
||
"long_term": ["长期规划1", "长期规划2"]
|
||
}},
|
||
"practice_suggestions": ["练习建议1", "练习建议2"],
|
||
"immediate_actions": ["立即行动1", "立即行动2"]
|
||
}}
|
||
|
||
请确保建议系统、实用,适合不同水平的摄影爱好者。
|
||
"""
|
||
return prompt.strip()
|
||
|
||
def _call_deepseek_api(self, prompt):
|
||
"""调用DeepSeek API"""
|
||
headers = {
|
||
'Authorization': f'Bearer {self.api_key}',
|
||
'Content-Type': 'application/json'
|
||
}
|
||
|
||
data = {
|
||
'model': 'deepseek-chat',
|
||
'messages': [
|
||
{
|
||
'role': 'system',
|
||
'content': '你是一名专业的摄影指导专家,具有丰富的摄影教学经验和艺术审美能力。你擅长为不同水平的摄影爱好者提供具体、实用的改进建议。'
|
||
},
|
||
{
|
||
'role': 'user',
|
||
'content': prompt
|
||
}
|
||
],
|
||
'max_tokens': 1500,
|
||
'temperature': 0.7,
|
||
'top_p': 0.9
|
||
}
|
||
|
||
response = requests.post(self.base_url, headers=headers, json=data, timeout=30)
|
||
response.raise_for_status()
|
||
|
||
return response.json()
|
||
|
||
def _parse_advice_response(self, api_response):
|
||
"""解析API响应"""
|
||
try:
|
||
if 'choices' in api_response and len(api_response['choices']) > 0:
|
||
content = api_response['choices'][0]['message']['content'].strip()
|
||
|
||
# 尝试解析JSON格式的响应
|
||
try:
|
||
# 提取JSON部分(可能包含在代码块中)
|
||
if '```json' in content:
|
||
json_str = content.split('```json')[1].split('```')[0].strip()
|
||
elif '```' in content:
|
||
json_str = content.split('```')[1].strip()
|
||
else:
|
||
json_str = content
|
||
|
||
advice_data = json.loads(json_str)
|
||
return advice_data
|
||
|
||
except json.JSONDecodeError:
|
||
# 如果不是JSON格式,返回原始文本
|
||
return {'raw_advice': content}
|
||
else:
|
||
raise Exception("API响应格式错误")
|
||
|
||
except Exception as e:
|
||
raise Exception(f"解析API响应失败: {str(e)}")
|
||
|
||
def _generate_fallback_quality_advice(self, quality_scores):
|
||
"""生成备用质量建议"""
|
||
return {
|
||
"overall_evaluation": "基于评分结果生成的改进建议",
|
||
"strengths": ["照片基础质量良好"],
|
||
"priority_improvements": ["根据评分结果确定重点改进维度"],
|
||
"specific_advice": {
|
||
"通用建议": [
|
||
"建议使用三脚架提高稳定性",
|
||
"注意光线条件,避免过暗或过亮",
|
||
"后期适当调整对比度和色彩平衡"
|
||
]
|
||
},
|
||
"learning_resources": ["推荐学习基础摄影知识和后期处理技巧"]
|
||
}
|
||
|
||
def _generate_fallback_aesthetic_advice(self, aesthetic_scores):
|
||
"""生成备用美学建议"""
|
||
return {
|
||
"aesthetic_evaluation": "照片具有基本的审美价值",
|
||
"composition_advice": ["学习三分法则构图", "注意主体位置安排"],
|
||
"color_advice": ["保持色彩和谐", "避免过度饱和"],
|
||
"lighting_advice": ["选择合适的光线条件", "注意光影效果"],
|
||
"creative_suggestions": ["尝试不同的拍摄角度", "探索创意构图方式"]
|
||
}
|
||
|
||
def _generate_fallback_comprehensive_advice(self, quality_scores, aesthetic_scores):
|
||
"""生成备用综合建议"""
|
||
return {
|
||
"comprehensive_evaluation": "照片整体表现良好,有提升空间",
|
||
"learning_plan": {
|
||
"short_term": ["重点改进技术基础", "练习基本构图技巧"],
|
||
"long_term": ["系统学习摄影理论", "培养艺术审美能力"]
|
||
},
|
||
"practice_suggestions": ["多拍多练", "分析优秀作品"],
|
||
"immediate_actions": ["检查相机设置", "注意拍摄环境"]
|
||
}
|
||
|
||
# 便捷函数
|
||
def get_deepseek_quality_advice(quality_scores, photo_description=""):
|
||
"""获取DeepSeek质量建议(便捷函数)"""
|
||
try:
|
||
advisor = DeepSeekPhotoAdvisor()
|
||
return advisor.generate_quality_advice(quality_scores, photo_description)
|
||
except Exception as e:
|
||
# 如果DeepSeek不可用,返回空建议
|
||
return {}
|
||
|
||
def get_deepseek_aesthetic_advice(aesthetic_scores, photo_description=""):
|
||
"""获取DeepSeek美学建议(便捷函数)"""
|
||
try:
|
||
advisor = DeepSeekPhotoAdvisor()
|
||
return advisor.generate_aesthetic_advice(aesthetic_scores, photo_description)
|
||
except Exception as e:
|
||
return {}
|
||
|
||
def check_deepseek_config():
|
||
"""检查DeepSeek配置状态"""
|
||
try:
|
||
api_key = os.getenv('DEEPSEEK_API_KEY')
|
||
if api_key:
|
||
return True, "DeepSeek照片建议生成器已配置"
|
||
else:
|
||
return False, "DeepSeek API密钥未配置,请在.env文件中设置DEEPSEEK_API_KEY"
|
||
except Exception as e:
|
||
return False, f"DeepSeek配置检查失败: {str(e)}" |