v1.8.0: 模块化重构 + 后台登录认证
This commit is contained in:
90
modules/routes/api_models.py
Normal file
90
modules/routes/api_models.py
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
"""
|
||||||
|
AI模型 CRUD API
|
||||||
|
"""
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from flask import Blueprint, request, jsonify
|
||||||
|
from config import MODELS_FILE
|
||||||
|
from utils import load_data, save_data, parse_date_to_timestamp, safe_sort_key
|
||||||
|
|
||||||
|
models_bp = Blueprint('api_models', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models')
|
||||||
|
def api_models():
|
||||||
|
models = load_data(MODELS_FILE)
|
||||||
|
hide_hidden = request.args.get('all', '0') == '0'
|
||||||
|
if hide_hidden:
|
||||||
|
models = [m for m in models if m.get('visible', True)]
|
||||||
|
keyword = request.args.get('q', '').strip().lower()
|
||||||
|
if keyword:
|
||||||
|
models = [m for m in models if keyword in m.get('name', '').lower() or
|
||||||
|
keyword in m.get('organization', '').lower()]
|
||||||
|
sort_by = request.args.get('sort', 'default')
|
||||||
|
reverse = request.args.get('order', 'desc') == 'desc'
|
||||||
|
if sort_by == 'default':
|
||||||
|
models = sorted(models, key=lambda x: (
|
||||||
|
not x.get('is_pinned', False),
|
||||||
|
-(parse_date_to_timestamp(x.get('publish_date', '')) or
|
||||||
|
parse_date_to_timestamp(x.get('created_at', '')) or 0)
|
||||||
|
))
|
||||||
|
elif sort_by in ['name', 'parameters', 'context_length', 'mmlu', 'created_at',
|
||||||
|
'publish_date', 'views', 'updated_at']:
|
||||||
|
models = sorted(models, key=lambda x: safe_sort_key(x, sort_by), reverse=reverse)
|
||||||
|
return jsonify(models)
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models/<model_id>')
|
||||||
|
def api_model_detail(model_id):
|
||||||
|
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
|
||||||
|
return jsonify(model)
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models', methods=['POST'])
|
||||||
|
def api_create_model():
|
||||||
|
data = request.get_json()
|
||||||
|
models = load_data(MODELS_FILE)
|
||||||
|
data['id'] = uuid.uuid4().hex[:12]
|
||||||
|
data['created_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
data['visible'] = data.get('visible', True)
|
||||||
|
data['publish_date'] = data.get('publish_date', '')
|
||||||
|
data['views'] = data.get('views', 0)
|
||||||
|
data['is_pinned'] = data.get('is_pinned', False)
|
||||||
|
models.append(data)
|
||||||
|
save_data(MODELS_FILE, models)
|
||||||
|
return jsonify(data)
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models/<model_id>', methods=['PUT'])
|
||||||
|
def api_update_model(model_id):
|
||||||
|
data = request.get_json()
|
||||||
|
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
|
||||||
|
model.update(data)
|
||||||
|
model['updated_at'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
save_data(MODELS_FILE, models)
|
||||||
|
return jsonify(model)
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models/<model_id>', methods=['DELETE'])
|
||||||
|
def api_delete_model(model_id):
|
||||||
|
models = load_data(MODELS_FILE)
|
||||||
|
models = [m for m in models if m['id'] != model_id]
|
||||||
|
save_data(MODELS_FILE, models)
|
||||||
|
return jsonify({'success': True})
|
||||||
|
|
||||||
|
|
||||||
|
@models_bp.route('/api/models/<model_id>/visible', methods=['POST'])
|
||||||
|
def api_toggle_model_visible(model_id):
|
||||||
|
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
|
||||||
|
model['visible'] = not model.get('visible', True)
|
||||||
|
save_data(MODELS_FILE, models)
|
||||||
|
return jsonify({'success': True, 'visible': model['visible']})
|
||||||
Reference in New Issue
Block a user