1 .修改了新建评估指标删除的bug
This commit is contained in:
@@ -4,6 +4,7 @@ API 路由包
|
||||
from .datasets import datasets_bp
|
||||
from .model_manage import model_manage_bp
|
||||
from .model_chat import model_chat_bp
|
||||
from .dimension import dimension_bp
|
||||
|
||||
# 注册所有蓝图
|
||||
def register_blueprints(app):
|
||||
@@ -11,3 +12,4 @@ def register_blueprints(app):
|
||||
app.register_blueprint(datasets_bp)
|
||||
app.register_blueprint(model_manage_bp)
|
||||
app.register_blueprint(model_chat_bp)
|
||||
app.register_blueprint(dimension_bp)
|
||||
|
||||
144
src/api/dimension.py
Normal file
144
src/api/dimension.py
Normal file
@@ -0,0 +1,144 @@
|
||||
"""
|
||||
评测维度管理 API 路由
|
||||
"""
|
||||
import os
|
||||
import pymysql
|
||||
import yaml
|
||||
from flask import Blueprint, request, jsonify
|
||||
|
||||
# 获取项目根目录
|
||||
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
# 创建蓝图
|
||||
dimension_bp = Blueprint('dimension', __name__, url_prefix='/api/dimension')
|
||||
|
||||
|
||||
def get_db_connection():
|
||||
"""获取数据库连接"""
|
||||
CONFIG_PATH = os.path.join(PROJECT_ROOT, 'config.yaml')
|
||||
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
|
||||
CONFIG = yaml.safe_load(f)
|
||||
db_config = CONFIG['database']
|
||||
return pymysql.connect(
|
||||
host=db_config['host'],
|
||||
port=db_config['port'],
|
||||
user=db_config['username'],
|
||||
password=db_config['password'],
|
||||
database=db_config['name'],
|
||||
charset=db_config.get('charset', 'utf8mb4'),
|
||||
cursorclass=pymysql.cursors.DictCursor
|
||||
)
|
||||
|
||||
|
||||
def generic_get_all(table_name, order_by='create_time DESC'):
|
||||
"""通用查询所有"""
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT * FROM {table_name} ORDER BY {order_by}")
|
||||
result = cursor.fetchall()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return result
|
||||
|
||||
|
||||
def generic_create(table_name, data):
|
||||
"""通用创建"""
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
columns = ', '.join(data.keys())
|
||||
placeholders = ', '.join(['%s'] * len(data))
|
||||
sql = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})"
|
||||
cursor.execute(sql, list(data.values()))
|
||||
conn.commit()
|
||||
new_id = cursor.lastrowid
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return new_id
|
||||
|
||||
|
||||
def generic_update(table_name, id_val, data):
|
||||
"""通用更新"""
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
set_clause = ', '.join([f"{k} = %s" for k in data.keys()])
|
||||
sql = f"UPDATE {table_name} SET {set_clause} WHERE id = %s"
|
||||
values = list(data.values()) + [id_val]
|
||||
cursor.execute(sql, values)
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
def generic_delete(table_name, id_val):
|
||||
"""通用删除"""
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"DELETE FROM {table_name} WHERE id = %s", (id_val,))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
|
||||
def generic_get_by_id(table_name, id_val):
|
||||
"""通用按ID查询"""
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT * FROM {table_name} WHERE id = %s", (id_val,))
|
||||
result = cursor.fetchone()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return result
|
||||
|
||||
|
||||
# ============ 评测维度 CRUD ============
|
||||
|
||||
@dimension_bp.route('', methods=['GET'])
|
||||
def get_dimensions():
|
||||
"""获取所有评测维度"""
|
||||
return jsonify({'code': 0, 'data': generic_get_all('model_dimension')})
|
||||
|
||||
|
||||
@dimension_bp.route('/<int:id>', methods=['GET'])
|
||||
def get_dimension_by_id(id):
|
||||
"""获取单个评测维度"""
|
||||
dimension = generic_get_by_id('model_dimension', id)
|
||||
if dimension:
|
||||
return jsonify({'code': 0, 'data': dimension})
|
||||
return jsonify({'code': 1, 'message': '维度不存在'})
|
||||
|
||||
|
||||
@dimension_bp.route('', methods=['POST'])
|
||||
def create_dimension():
|
||||
"""创建评测维度"""
|
||||
data = request.json
|
||||
insert_data = {
|
||||
'name': data.get('name'),
|
||||
'type': data.get('type'),
|
||||
'description': data.get('description', '')
|
||||
}
|
||||
new_id = generic_create('model_dimension', insert_data)
|
||||
return jsonify({'code': 0, 'message': '创建成功', 'id': new_id})
|
||||
|
||||
|
||||
@dimension_bp.route('/<int:id>', methods=['PUT'])
|
||||
def update_dimension(id):
|
||||
"""更新评测维度"""
|
||||
data = request.json
|
||||
update_data = {}
|
||||
if 'name' in data:
|
||||
update_data['name'] = data['name']
|
||||
if 'type' in data:
|
||||
update_data['type'] = data['type']
|
||||
if 'description' in data:
|
||||
update_data['description'] = data['description']
|
||||
|
||||
if update_data:
|
||||
generic_update('model_dimension', id, update_data)
|
||||
return jsonify({'code': 0, 'message': '更新成功'})
|
||||
|
||||
|
||||
@dimension_bp.route('/<int:id>', methods=['DELETE'])
|
||||
def delete_dimension(id):
|
||||
"""删除评测维度"""
|
||||
generic_delete('model_dimension', id)
|
||||
return jsonify({'code': 0, 'message': '删除成功'})
|
||||
@@ -116,7 +116,8 @@ def create_model_manage():
|
||||
'name': data.get('name'),
|
||||
'type': data.get('type'),
|
||||
'model_source': data.get('model_source', 'local'),
|
||||
'description': data.get('description')
|
||||
'description': data.get('description'),
|
||||
'purpose': data.get('purpose', 'inference') # 默认推理用途
|
||||
}
|
||||
|
||||
if data.get('model_source') == 'local':
|
||||
@@ -138,6 +139,17 @@ def update_model_manage(id):
|
||||
return jsonify({'code': 0, 'message': '更新成功'})
|
||||
|
||||
|
||||
@model_manage_bp.route('/<int:id>/purpose', methods=['PUT'])
|
||||
def update_model_purpose(id):
|
||||
"""更新模型用途"""
|
||||
data = request.json
|
||||
purpose = data.get('purpose')
|
||||
if purpose not in ['training', 'inference', 'evaluation']:
|
||||
return jsonify({'code': 1, 'message': '无效的用途类型'})
|
||||
generic_update('model_manage', id, {'purpose': purpose})
|
||||
return jsonify({'code': 0, 'message': '更新成功'})
|
||||
|
||||
|
||||
@model_manage_bp.route('/<int:id>', methods=['DELETE'])
|
||||
def delete_model_manage(id):
|
||||
"""删除模型"""
|
||||
|
||||
27
src/main.py
27
src/main.py
@@ -174,6 +174,17 @@ def init_database():
|
||||
api_key VARCHAR(500),
|
||||
model_name VARCHAR(255),
|
||||
description TEXT,
|
||||
purpose VARCHAR(50) DEFAULT 'inference',
|
||||
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4""",
|
||||
|
||||
# 评测维度表
|
||||
"""CREATE TABLE IF NOT EXISTS model_dimension (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(100),
|
||||
description TEXT,
|
||||
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4""",
|
||||
@@ -204,6 +215,13 @@ def init_database():
|
||||
except Exception as e:
|
||||
print(f" 表 {i+1} 创建失败: {e}")
|
||||
|
||||
# 为已存在的 model_manage 表添加 purpose 列
|
||||
try:
|
||||
cursor.execute("ALTER TABLE model_manage ADD COLUMN purpose VARCHAR(50) DEFAULT 'inference'")
|
||||
print(" model_manage 表添加 purpose 列成功")
|
||||
except Exception as e:
|
||||
print(f" model_manage 表 purpose 列处理: {e}")
|
||||
|
||||
# 插入默认管理员用户
|
||||
cursor.execute("SELECT * FROM users WHERE username = 'admin'")
|
||||
if not cursor.fetchone():
|
||||
@@ -221,7 +239,14 @@ def init_database():
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = CONFIG['secret_key']
|
||||
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
||||
app.config['CORS_HEADERS'] = 'Content-Type'
|
||||
CORS(app, resources={
|
||||
r"/api/*": {
|
||||
"origins": "*",
|
||||
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
"allow_headers": ["Content-Type", "Authorization"]
|
||||
}
|
||||
}, supports_credentials=False)
|
||||
|
||||
# 注册蓝图
|
||||
register_blueprints(app)
|
||||
|
||||
Reference in New Issue
Block a user