1. 增加了模型管理页面的功能
2. 修改了若干的bug
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
API 路由包
|
||||
"""
|
||||
from .datasets import datasets_bp
|
||||
from .model_manage import model_manage_bp
|
||||
|
||||
# 注册所有蓝图
|
||||
def register_blueprints(app):
|
||||
"""注册所有蓝图"""
|
||||
app.register_blueprint(datasets_bp)
|
||||
app.register_blueprint(model_manage_bp)
|
||||
|
||||
145
src/api/model_manage.py
Normal file
145
src/api/model_manage.py
Normal file
@@ -0,0 +1,145 @@
|
||||
"""
|
||||
模型管理 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__))))
|
||||
|
||||
# 创建蓝图
|
||||
model_manage_bp = Blueprint('model_manage', __name__, url_prefix='/api/model-manage')
|
||||
|
||||
|
||||
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 ============
|
||||
|
||||
@model_manage_bp.route('', methods=['GET'])
|
||||
def get_model_manage():
|
||||
"""获取所有模型"""
|
||||
return jsonify({'code': 0, 'data': generic_get_all('model_manage')})
|
||||
|
||||
|
||||
@model_manage_bp.route('/<int:id>', methods=['GET'])
|
||||
def get_model_manage_by_id(id):
|
||||
"""获取单个模型"""
|
||||
model = generic_get_by_id('model_manage', id)
|
||||
if model:
|
||||
return jsonify({'code': 0, 'data': model})
|
||||
return jsonify({'code': 1, 'message': '模型不存在'})
|
||||
|
||||
|
||||
@model_manage_bp.route('', methods=['POST'])
|
||||
def create_model_manage():
|
||||
"""创建模型"""
|
||||
data = request.json
|
||||
# 构建插入数据
|
||||
insert_data = {
|
||||
'name': data.get('name'),
|
||||
'type': data.get('type'),
|
||||
'model_source': data.get('model_source', 'local'),
|
||||
'description': data.get('description')
|
||||
}
|
||||
|
||||
if data.get('model_source') == 'local':
|
||||
insert_data['path'] = data.get('path', '')
|
||||
else:
|
||||
insert_data['api_url'] = data.get('api_url', '')
|
||||
insert_data['api_key'] = data.get('api_key', '')
|
||||
insert_data['model_name'] = data.get('model_name', '')
|
||||
|
||||
new_id = generic_create('model_manage', insert_data)
|
||||
return jsonify({'code': 0, 'message': '创建成功', 'id': new_id})
|
||||
|
||||
|
||||
@model_manage_bp.route('/<int:id>', methods=['PUT'])
|
||||
def update_model_manage(id):
|
||||
"""更新模型"""
|
||||
data = request.json
|
||||
generic_update('model_manage', id, data)
|
||||
return jsonify({'code': 0, 'message': '更新成功'})
|
||||
|
||||
|
||||
@model_manage_bp.route('/<int:id>', methods=['DELETE'])
|
||||
def delete_model_manage(id):
|
||||
"""删除模型"""
|
||||
generic_delete('model_manage', id)
|
||||
return jsonify({'code': 0, 'message': '删除成功'})
|
||||
31
src/main.py
31
src/main.py
@@ -157,8 +157,11 @@ def init_database():
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
type VARCHAR(100),
|
||||
version VARCHAR(50),
|
||||
model_source VARCHAR(20) DEFAULT 'local',
|
||||
path VARCHAR(500),
|
||||
api_url VARCHAR(500),
|
||||
api_key VARCHAR(500),
|
||||
model_name VARCHAR(255),
|
||||
description TEXT,
|
||||
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
@@ -456,32 +459,6 @@ def delete_permission(id):
|
||||
return jsonify({'code': 0, 'message': '删除成功'})
|
||||
|
||||
|
||||
# ============ 模型管理接口 ============
|
||||
@app.route('/api/model-manage', methods=['GET'])
|
||||
def get_model_manage():
|
||||
return jsonify({'code': 0, 'data': generic_get_all('model_manage')})
|
||||
|
||||
|
||||
@app.route('/api/model-manage', methods=['POST'])
|
||||
def create_model_manage():
|
||||
data = request.json
|
||||
new_id = generic_create('model_manage', data)
|
||||
return jsonify({'code': 0, 'message': '创建成功', 'id': new_id})
|
||||
|
||||
|
||||
@app.route('/api/model-manage/<int:id>', methods=['PUT'])
|
||||
def update_model_manage(id):
|
||||
data = request.json
|
||||
generic_update('model_manage', id, data)
|
||||
return jsonify({'code': 0, 'message': '更新成功'})
|
||||
|
||||
|
||||
@app.route('/api/model-manage/<int:id>', methods=['DELETE'])
|
||||
def delete_model_manage(id):
|
||||
generic_delete('model_manage', id)
|
||||
return jsonify({'code': 0, 'message': '删除成功'})
|
||||
|
||||
|
||||
# ============ 系统配置接口 ============
|
||||
@app.route('/api/sys-config', methods=['GET'])
|
||||
def get_sys_config():
|
||||
|
||||
Reference in New Issue
Block a user