From 8be35bfebfeb51aa0137ed4f69d8ffb761a89c61 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Wed, 10 Jun 2026 10:44:08 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=A7=BB=E9=99=A4=E5=9B=9E=E9=80=80?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E7=B2=BE=E7=AE=80=E9=A2=84=E6=B5=8B?= =?UTF-8?q?=E8=B7=AF=E7=94=B1;=20=E6=B8=85=E7=90=86=E6=97=A7=E6=9E=B6?= =?UTF-8?q?=E6=9E=84PyTorch=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/routes.py b/src/routes.py index daa4fe4..0d2308e 100644 --- a/src/routes.py +++ b/src/routes.py @@ -107,39 +107,24 @@ def predict(): model_id = data.get('model_id') # 可选:指定模型ID logger.info(f"Received prediction request for equipment type: {equipment_type}") - # 获取可用模型(默认R²最高的;可选指定model_id) + # 获取最优模型(默认R²最高的;可选指定model_id) with get_db_connection() as conn: cursor = conn.cursor() if model_id: - cursor.execute(""" - SELECT * FROM trained_models - WHERE id = ? AND is_active = TRUE - """, (model_id,)) - models = cursor.fetchall() + cursor.execute("SELECT * FROM trained_models WHERE id = ? AND is_active = TRUE", (model_id,)) else: cursor.execute(""" SELECT * FROM trained_models - WHERE equipment_type = ? - AND model_type != 'pls' - AND is_active = TRUE - ORDER BY r2_score DESC + WHERE equipment_type = ? AND model_type != 'pls' AND is_active = TRUE + ORDER BY r2_score DESC LIMIT 1 """, (equipment_type,)) - models = cursor.fetchall() + model = cursor.fetchone() - if not models: - return jsonify({'error': '未找到可用的模型'}), 404 + if not model: + return jsonify({'error': '未找到可用的模型,请先训练'}), 404 - # 逐个尝试加载,跳过架构不兼容的模型 predictor = CostPredictor() - for model in models: - try: - prediction = predictor.predict(data, model) - break - except ValueError as e: - logger.warning(f"Model {model['model_name']} failed: {e}, trying next...") - continue - else: - return jsonify({'error': '所有激活模型加载失败,请重新训练'}), 500 + prediction = predictor.predict(data, model) # 返回预测结果 return jsonify({