v1.8.0: 模块化重构 + 后台登录认证
This commit is contained in:
66
modules/routes/api_search.py
Normal file
66
modules/routes/api_search.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
搜索/计算/统计 API
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from config import MODELS_FILE, GPUS_FILE, CPUS_FILE, CATEGORIES_FILE, KNOWLEDGE_FILE
|
||||
from utils import load_data
|
||||
|
||||
search_bp = Blueprint('api_search', __name__)
|
||||
|
||||
|
||||
@search_bp.route('/api/search')
|
||||
def api_search():
|
||||
keyword = request.args.get('q', '').strip().lower()
|
||||
if not keyword:
|
||||
return jsonify({'models': [], 'gpus': [], 'cpus': []})
|
||||
models = load_data(MODELS_FILE)
|
||||
gpus = load_data(GPUS_FILE)
|
||||
cpus = load_data(CPUS_FILE)
|
||||
return jsonify({
|
||||
'models': [m for m in models if m.get('visible', True) and
|
||||
(keyword in m.get('name', '').lower() or
|
||||
keyword in m.get('organization', '').lower())],
|
||||
'gpus': [g for g in gpus if g.get('visible', True) and
|
||||
(keyword in g.get('name', '').lower() or
|
||||
keyword in g.get('manufacturer', '').lower())],
|
||||
'cpus': [c for c in cpus if c.get('visible', True) and
|
||||
(keyword in c.get('name', '').lower() or
|
||||
keyword in c.get('manufacturer', '').lower())]
|
||||
})
|
||||
|
||||
|
||||
@search_bp.route('/api/calculate/vram')
|
||||
def api_calculate_vram():
|
||||
params = request.args.get('params', '7', type=float)
|
||||
precision = request.args.get('precision', 'fp16', type=str)
|
||||
bytes_per_param = {'fp32': 4, 'fp16': 2, 'int8': 1, 'int4': 0.5}
|
||||
multiplier = bytes_per_param.get(precision, 2)
|
||||
vram_gb = params * multiplier * 1e9 / (1024**3)
|
||||
total_vram = vram_gb * 1.3
|
||||
gpus = load_data(GPUS_FILE)
|
||||
suitable_gpus = [g for g in gpus if g.get('visible', True) and
|
||||
g.get('memory_gb', 0) >= total_vram]
|
||||
return jsonify({
|
||||
'model_vram': round(vram_gb, 2),
|
||||
'total_vram': round(total_vram, 2),
|
||||
'suitable_gpus': suitable_gpus
|
||||
})
|
||||
|
||||
|
||||
@search_bp.route('/api/stats')
|
||||
def api_stats():
|
||||
models = load_data(MODELS_FILE)
|
||||
gpus = load_data(GPUS_FILE)
|
||||
cpus = load_data(CPUS_FILE)
|
||||
categories = load_data(CATEGORIES_FILE)
|
||||
knowledge = load_data(KNOWLEDGE_FILE)
|
||||
visible_models = [m for m in models if m.get('visible', True)]
|
||||
return jsonify({
|
||||
'models_count': len(visible_models),
|
||||
'gpus_count': len([g for g in gpus if g.get('visible', True)]),
|
||||
'cpus_count': len([c for c in cpus if c.get('visible', True)]),
|
||||
'categories_count': len([c for c in categories if c.get('visible', True)]),
|
||||
'knowledge_count': len(knowledge),
|
||||
'latest_models': sorted(visible_models,
|
||||
key=lambda x: x.get('created_at', ''), reverse=True)[:5]
|
||||
})
|
||||
Reference in New Issue
Block a user