diff --git a/src/api/model_manage.py b/src/api/model_manage.py index 55830b3..f1ad21a 100644 --- a/src/api/model_manage.py +++ b/src/api/model_manage.py @@ -155,3 +155,37 @@ def delete_model_manage(id): """删除模型""" generic_delete('model_manage', id) return jsonify({'code': 0, 'message': '删除成功'}) + + +# ============ 本地模型列表接口 ============ + +@model_manage_bp.route('/local-models', methods=['GET']) +def get_local_models(): + """获取本地模型列表(从YG_FT_Base/local_models目录)""" + import logging + logger = logging.getLogger(__name__) + + try: + # 使用 YG_FT_Base/local_models 目录 + base_path = os.path.join(PROJECT_ROOT, 'local_models') + + models = [] + if os.path.exists(base_path): + for item in os.listdir(base_path): + item_path = os.path.join(base_path, item) + if os.path.isdir(item_path): + models.append({ + 'name': item, + 'path': item_path + }) + + return jsonify({ + 'code': 0, + 'data': { + 'models': models, + 'base_path': base_path + } + }) + except Exception as e: + logger.error(f"获取本地模型列表失败: {e}") + return jsonify({'code': 1, 'message': str(e)}) diff --git a/web/pages/model-manage-create.html b/web/pages/model-manage-create.html index cc4673b..6171b3a 100644 --- a/web/pages/model-manage-create.html +++ b/web/pages/model-manage-create.html @@ -275,8 +275,9 @@ - -
支持本地路径或云存储路径(OSS、S3、HDFS等)
+ @@ -395,6 +396,12 @@ }); }); + // 如果默认选中本地模型,立即加载模型列表 + const localRadio = document.querySelector('input[name="model_source"][value="local"]'); + if (localRadio && localRadio.checked) { + loadLocalModels(); + } + // 设置侧边栏当前页高亮 const currentPage = 'model-manage'; document.querySelectorAll('.nav-link').forEach(link => { @@ -445,7 +452,22 @@ // 填充来源相关字段 if (modelSource === 'local') { - document.querySelector('input[name="local_path"]').value = model.path || ''; + const path = model.path || ''; + // 尝试在选择器中找到匹配的模型 + loadLocalModels().then(() => { + const select = document.getElementById('localModelSelect'); + if (select && path) { + // 尝试精确匹配或部分匹配 + for (let i = 0; i < select.options.length; i++) { + if (select.options[i].value === path || + select.options[i].textContent === path || + path.includes(select.options[i].textContent)) { + select.selectedIndex = i; + break; + } + } + } + }); } else { document.querySelector('input[name="api_url"]').value = model.api_url || ''; document.querySelector('input[name="api_key"]').value = model.api_key || ''; @@ -475,6 +497,8 @@ if (source === 'local') { localPanel.classList.remove('hidden'); onlinePanel.classList.add('hidden'); + // 切换到本地模型时加载模型列表 + loadLocalModels(); } else { localPanel.classList.add('hidden'); onlinePanel.classList.remove('hidden'); @@ -484,6 +508,36 @@ // 为了兼容性,保留 online 作为别名 window.toggleModelSource = toggleModelSource; + // 加载本地模型列表 + async function loadLocalModels() { + const select = document.getElementById('localModelSelect'); + if (!select) return; + + // 检查是否已经加载过 + if (select.options.length > 1) return; + + try { + const response = await fetch(`${API_BASE}/local-models`); + const result = await response.json(); + + if (result.code === 0 && result.data && result.data.models) { + result.data.models.forEach(model => { + const option = document.createElement('option'); + option.value = model.path; + option.textContent = model.name; + select.appendChild(option); + }); + } + } catch (error) { + console.warn('加载本地模型列表失败:', error); + } + } + + // 模型选择变化 + function onModelSelectChange() { + // 保留空函数以兼容可能的其他调用 + } + // 提交表单 async function submitForm() { console.log('submitForm called');