ShopTRAINING/test/create_new_endpoint.py
2025-07-02 11:05:23 +08:00

74 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
为API添加一个新的测试端点用于验证修复
"""
def add_test_endpoint():
"""在API文件末尾添加一个测试端点"""
endpoint_code = '''
# 测试端点 - 用于验证ModelManager修复
@app.route('/api/models/test', methods=['GET'])
def test_models_fix():
"""
测试端点 - 验证ModelManager修复是否生效
"""
try:
from utils.model_manager import ModelManager
import os
# 强制创建新的ModelManager实例
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(current_dir)
model_dir = os.path.join(project_root, 'saved_models')
manager = ModelManager(model_dir)
models = manager.list_models()
# 简化的响应格式
test_result = {
"status": "success",
"test_name": "ModelManager修复测试",
"model_dir": manager.model_dir,
"dir_exists": os.path.exists(manager.model_dir),
"models_found": len(models),
"models": []
}
for model in models:
test_result["models"].append({
"filename": model.get('filename', 'MISSING'),
"model_id": model.get('filename', '').replace('.pth', '') if model.get('filename') else 'GENERATED_MISSING',
"product_id": model.get('product_id', 'MISSING'),
"model_type": model.get('model_type', 'MISSING')
})
return jsonify(test_result)
except Exception as e:
return jsonify({
"status": "error",
"message": str(e),
"test_name": "ModelManager修复测试"
}), 500
'''
# 读取当前API文件
with open('server/api.py', 'r', encoding='utf-8') as f:
content = f.read()
# 检查是否已经添加了测试端点
if '/api/models/test' in content:
print("测试端点已存在")
return
# 在文件末尾添加测试端点
with open('server/api.py', 'a', encoding='utf-8') as f:
f.write(endpoint_code)
print("已添加测试端点: /api/models/test")
print("重启API服务器后访问: http://localhost:5000/api/models/test")
if __name__ == "__main__":
add_test_endpoint()