| from fastapi import APIRouter, HTTPException |
| from ..schemas import ModelLoadRequest, ModelInfo |
| from ..model_manager import model_manager |
| import os |
| from pathlib import Path |
|
|
| router = APIRouter(prefix="/api/model", tags=["model"]) |
|
|
| @router.post("/load", response_model=ModelInfo) |
| async def load_model(req: ModelLoadRequest): |
| try: |
| info = model_manager.load_model( |
| model_path=req.model_path, |
| dtype=req.dtype, |
| quantization=req.quantization, |
| device_map=req.device_map, |
| offload_folder=req.offload_folder, |
| trust_remote_code=req.trust_remote_code |
| ) |
| if info.status == "error": |
| raise HTTPException(status_code=500, detail=info.error) |
| return info |
| except FileNotFoundError as e: |
| raise HTTPException(status_code=404, detail=str(e)) |
| except NotADirectoryError as e: |
| raise HTTPException(status_code=400, detail=str(e)) |
| except Exception as e: |
| |
| if model_manager.info.status == "error": |
| raise HTTPException(status_code=500, detail=model_manager.info.error) |
| raise HTTPException(status_code=500, detail=str(e)) |
|
|
| @router.get("/status", response_model=ModelInfo) |
| async def get_status(): |
| return model_manager.get_status() |
|
|
| @router.get("/info", response_model=ModelInfo) |
| async def get_info(): |
| return model_manager.get_status() |
|
|
| @router.delete("/unload", response_model=ModelInfo) |
| async def unload_model(): |
| return model_manager.unload() |
|
|
| @router.get("/validate") |
| async def validate_path(path: str): |
| p = Path(path) |
| if not p.exists(): |
| return {"valid": False, "reason": "المسار غير موجود", "exists": False} |
| if not p.is_dir(): |
| return {"valid": False, "reason": "المسار ليس مجلداً", "exists": True} |
| s_files = list(p.glob("*.safetensors")) |
| has_config = (p / "config.json").exists() |
| has_tokenizer = (p / "tokenizer.json").exists() or (p / "tokenizer_config.json").exists() |
| has_model_index = (p / "model.safetensors.index.json").exists() |
| details = { |
| "safetensors_count": len(s_files), |
| "safetensors_files": [f.name for f in s_files], |
| "has_config": has_config, |
| "has_tokenizer": has_tokenizer, |
| "has_model_index": has_model_index, |
| "total_size_mb": round(sum(f.stat().st_size for f in s_files) / (1024*1024), 1) if s_files else 0 |
| } |
| valid = len(s_files) > 0 and has_config |
| reason = "صالح للتحميل" if valid else "يجب أن يحتوي المجلد على ملفات .safetensors و config.json" |
| return {"valid": valid, "reason": reason, "details": details} |
|
|
| @router.get("/list") |
| async def list_local_models(base_path: str = ""): |
| |
| if not base_path or not Path(base_path).exists(): |
| return {"models": []} |
| base = Path(base_path) |
| models = [] |
| for child in base.iterdir(): |
| if child.is_dir(): |
| s_files = list(child.glob("*.safetensors")) |
| has_config = (child / "config.json").exists() |
| if s_files: |
| models.append({ |
| "path": str(child), |
| "name": child.name, |
| "safetensors_count": len(s_files), |
| "has_config": has_config, |
| "size_mb": round(sum(f.stat().st_size for f in s_files)/(1024*1024),1) |
| }) |
| return {"models": models} |
|
|