feat: 智能补充参数功能 - 编辑已有产品时上传图片/文本补充缺失字段

This commit is contained in:
2026-04-28 10:09:58 +08:00
parent 438fba347a
commit a647179e72
4 changed files with 544 additions and 35 deletions
+162
View File
@@ -523,6 +523,168 @@ def api_parse_images():
# ============ 智能添加API ============
# ============ 智能补充参数API ============
@app.route('/api/models/<model_id>/smart-update', methods=['POST'])
def api_smart_update_model(model_id):
"""智能补充模型参数(只填充缺失字段)"""
data = request.get_json()
text = data.get('text', '')
images = data.get('images', [])
if not text and not images:
return jsonify({'error': '文本或图片不能都为空'}), 400
# 获取现有数据
models = load_data(MODELS_FILE)
model = next((m for m in models if m['id'] == model_id), None)
if not model:
return jsonify({'error': 'Model not found'}), 404
# 解析新参数
parsed_list = parse_with_llm(text, 'model', images)
if not parsed_list:
return jsonify({'error': '解析失败'}), 500
parsed = parsed_list[0] # 补充只取第一个
# 只填充缺失或为空的字段
updated_fields = []
for key, value in parsed.items():
if value is not None and value != '' and value != 0:
existing = model.get(key)
if existing is None or existing == '' or existing == 0:
model[key] = value
updated_fields.append(key)
model['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
model['raw_text'] = model.get('raw_text', '') + '\n' + text if text else model.get('raw_text', '')
if images:
existing_images = model.get('images', [])
model['images'] = existing_images + images
save_data(MODELS_FILE, models)
return jsonify({'success': True, 'updated_fields': updated_fields, 'model': model})
@app.route('/api/gpus/<gpu_id>/smart-update', methods=['POST'])
def api_smart_update_gpu(gpu_id):
"""智能补充GPU参数(只填充缺失字段)"""
data = request.get_json()
text = data.get('text', '')
images = data.get('images', [])
if not text and not images:
return jsonify({'error': '文本或图片不能都为空'}), 400
gpus = load_data(GPUS_FILE)
gpu = next((g for g in gpus if g['id'] == gpu_id), None)
if not gpu:
return jsonify({'error': 'GPU not found'}), 404
parsed_list = parse_with_llm(text, 'gpu', images)
if not parsed_list:
return jsonify({'error': '解析失败'}), 500
parsed = parsed_list[0]
updated_fields = []
for key, value in parsed.items():
if value is not None and value != '' and value != 0:
existing = gpu.get(key)
if existing is None or existing == '' or existing == 0:
gpu[key] = value
updated_fields.append(key)
gpu['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
gpu['raw_text'] = gpu.get('raw_text', '') + '\n' + text if text else gpu.get('raw_text', '')
if images:
existing_images = gpu.get('images', [])
gpu['images'] = existing_images + images
save_data(GPUS_FILE, gpus)
return jsonify({'success': True, 'updated_fields': updated_fields, 'gpu': gpu})
@app.route('/api/cpus/<cpu_id>/smart-update', methods=['POST'])
def api_smart_update_cpu(cpu_id):
"""智能补充CPU参数(只填充缺失字段)"""
data = request.get_json()
text = data.get('text', '')
images = data.get('images', [])
if not text and not images:
return jsonify({'error': '文本或图片不能都为空'}), 400
cpus = load_data(CPUS_FILE)
cpu = next((c for c in cpus if c['id'] == cpu_id), None)
if not cpu:
return jsonify({'error': 'CPU not found'}), 404
parsed_list = parse_with_llm(text, 'cpu', images)
if not parsed_list:
return jsonify({'error': '解析失败'}), 500
parsed = parsed_list[0]
updated_fields = []
for key, value in parsed.items():
if value is not None and value != '' and value != 0:
existing = cpu.get(key)
if existing is None or existing == '' or existing == 0:
cpu[key] = value
updated_fields.append(key)
cpu['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cpu['raw_text'] = cpu.get('raw_text', '') + '\n' + text if text else cpu.get('raw_text', '')
if images:
existing_images = cpu.get('images', [])
cpu['images'] = existing_images + images
save_data(CPUS_FILE, cpus)
return jsonify({'success': True, 'updated_fields': updated_fields, 'cpu': cpu})
@app.route('/api/items/<category_id>/<item_id>/smart-update', methods=['POST'])
def api_smart_update_item(category_id, item_id):
"""智能补充动态分类数据参数(只填充缺失字段)"""
data = request.get_json()
text = data.get('text', '')
images = data.get('images', [])
if not text and not images:
return jsonify({'error': '文本或图片不能都为空'}), 400
items_file = DATA_DIR / f'items_{category_id}.json'
items = load_data(items_file)
item = next((i for i in items if i['id'] == item_id), None)
if not item:
return jsonify({'error': 'Item not found'}), 404
parsed_list = parse_with_llm(text, 'dynamic', images)
if not parsed_list:
return jsonify({'error': '解析失败'}), 500
parsed = parsed_list[0]
updated_fields = []
for key, value in parsed.items():
if value is not None and value != '' and value != 0:
existing = item.get(key)
if existing is None or existing == '' or existing == 0:
item[key] = value
updated_fields.append(key)
item['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
item['raw_text'] = item.get('raw_text', '') + '\n' + text if text else item.get('raw_text', '')
if images:
existing_images = item.get('images', [])
item['images'] = existing_images + images
save_data(items_file, items)
return jsonify({'success': True, 'updated_fields': updated_fields, 'item': item})
@app.route('/api/models/smart-add', methods=['POST'])
def api_smart_add_model():
"""智能添加模型(支持文本和多图解析,可能添加多个产品)"""