|
12 | 12 | from main_helper import core as core, cross_server as cross_server |
13 | 13 | from fastapi.templating import Jinja2Templates |
14 | 14 | from fastapi.responses import HTMLResponse |
| 15 | +from utils.preferences import load_user_preferences, update_model_preferences, validate_model_preferences, get_model_preferences, get_preferred_model_path, move_model_to_top |
| 16 | +from utils.frontend_utils import find_models |
15 | 17 | templates = Jinja2Templates(directory="./") |
16 | 18 | from config import LANLAN_PROMPT, MASTER_NAME, her_name, MAIN_SERVER_PORT |
17 | 19 |
|
@@ -74,17 +76,61 @@ async def get_default_index(request: Request): # 这个接口在直播版代码 |
74 | 76 | return templates.TemplateResponse("templates/index.html", { |
75 | 77 | "request": request, |
76 | 78 | "lanlan_name": her_name, |
77 | | - "model_path": f"/static/live2d/mao_pro.model3.json" |
| 79 | + "model_path": f"/static/mao_pro/mao_pro.model3.json" |
78 | 80 | }) |
79 | 81 |
|
80 | | -@app.get("/{lanlan_name}", response_class=HTMLResponse) |
81 | | -async def get_index(request: Request, lanlan_name: str): |
82 | | - # Point FileResponse to the correct path relative to where server.py is run |
83 | | - return templates.TemplateResponse("templates/index.html", { |
84 | | - "request": request, |
85 | | - "lanlan_name": lanlan_name, |
86 | | - "model_path": f"/static/live2d/mao_pro.model3.json" # TODO: 根据lanlan_name动态加载模型. 实现起来很简单,但是用户需要手动配置、还需要调整大小和位置,当前版本先不增加复杂度 |
87 | | - }) |
| 82 | +@app.get("/api/preferences") |
| 83 | +async def get_preferences(): |
| 84 | + """获取用户偏好设置""" |
| 85 | + preferences = load_user_preferences() |
| 86 | + return preferences |
| 87 | + |
| 88 | +@app.post("/api/preferences") |
| 89 | +async def save_preferences(request: Request): |
| 90 | + """保存用户偏好设置""" |
| 91 | + try: |
| 92 | + data = await request.json() |
| 93 | + if not data: |
| 94 | + return {"success": False, "error": "无效的数据"} |
| 95 | + |
| 96 | + # 验证偏好数据 |
| 97 | + if not validate_model_preferences(data): |
| 98 | + return {"success": False, "error": "偏好数据格式无效"} |
| 99 | + |
| 100 | + # 更新偏好 |
| 101 | + if update_model_preferences(data['model_path'], data['position'], data['scale']): |
| 102 | + return {"success": True, "message": "偏好设置已保存"} |
| 103 | + else: |
| 104 | + return {"success": False, "error": "保存失败"} |
| 105 | + |
| 106 | + except Exception as e: |
| 107 | + return {"success": False, "error": str(e)} |
| 108 | + |
| 109 | + |
| 110 | +@app.get("/api/models") |
| 111 | +async def get_models(): |
| 112 | + """ |
| 113 | + API接口,调用扫描函数并以JSON格式返回找到的模型列表。 |
| 114 | + """ |
| 115 | + models = find_models() |
| 116 | + return models |
| 117 | + |
| 118 | +@app.post("/api/preferences/set-preferred") |
| 119 | +async def set_preferred_model(request: Request): |
| 120 | + """设置首选模型""" |
| 121 | + try: |
| 122 | + data = await request.json() |
| 123 | + if not data or 'model_path' not in data: |
| 124 | + return {"success": False, "error": "无效的数据"} |
| 125 | + |
| 126 | + if move_model_to_top(data['model_path']): |
| 127 | + return {"success": True, "message": "首选模型已更新"} |
| 128 | + else: |
| 129 | + return {"success": False, "error": "模型不存在或更新失败"} |
| 130 | + |
| 131 | + except Exception as e: |
| 132 | + return {"success": False, "error": str(e)} |
| 133 | + |
88 | 134 |
|
89 | 135 | @app.on_event("startup") |
90 | 136 | async def startup_event(): |
@@ -171,6 +217,26 @@ async def websocket_endpoint(websocket: WebSocket, lanlan_name: str): |
171 | 217 | logger.info(f"Cleaning up WebSocket resources: {websocket.client}") |
172 | 218 | await session_manager[lanlan_name].cleanup() |
173 | 219 |
|
| 220 | +@app.get("/l2d", response_class=HTMLResponse) |
| 221 | +async def get_l2d_manager(request: Request): |
| 222 | + """渲染Live2D模型管理器页面""" |
| 223 | + return templates.TemplateResponse("templates/l2d_manager.html", { |
| 224 | + "request": request |
| 225 | + }) |
| 226 | + |
| 227 | +@app.get("/{lanlan_name}", response_class=HTMLResponse) |
| 228 | +async def get_index(request: Request, lanlan_name: str): |
| 229 | + # 获取首选模型路径 |
| 230 | + model_path = get_preferred_model_path() or f"/static/mao_pro/mao_pro.model3.json" |
| 231 | + |
| 232 | + # Point FileResponse to the correct path relative to where server.py is run |
| 233 | + return templates.TemplateResponse("templates/index.html", { |
| 234 | + "request": request, |
| 235 | + "lanlan_name": lanlan_name, |
| 236 | + "model_path": model_path |
| 237 | + }) |
| 238 | + |
| 239 | + |
174 | 240 | # --- Run the Server --- |
175 | 241 | # (Keep your existing __main__ block) |
176 | 242 | if __name__ == "__main__": |
|
0 commit comments