Spaces:
Sleeping
Sleeping
Add models router
Browse files- app/routers/models.py +73 -0
app/routers/models.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Model repository routes."""
|
| 2 |
+
from fastapi import APIRouter, Depends, HTTPException
|
| 3 |
+
from sqlalchemy.orm import Session
|
| 4 |
+
|
| 5 |
+
from .. import models, schemas
|
| 6 |
+
from ..database import get_db
|
| 7 |
+
from ..deps import get_current_user
|
| 8 |
+
|
| 9 |
+
router = APIRouter(prefix="/models", tags=["models"])
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@router.post("", response_model=schemas.ModelOut, status_code=201)
|
| 13 |
+
def create_model(body: schemas.ModelCreate, user=Depends(get_current_user), db: Session = Depends(get_db)):
|
| 14 |
+
m = models.ModelRepo(owner=user.username, **body.model_dump())
|
| 15 |
+
db.add(m)
|
| 16 |
+
db.commit()
|
| 17 |
+
db.refresh(m)
|
| 18 |
+
return m
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@router.get("", response_model=list[schemas.ModelOut])
|
| 22 |
+
def list_models(owner: str | None = None, search: str | None = None, db: Session = Depends(get_db)):
|
| 23 |
+
q = db.query(models.ModelRepo)
|
| 24 |
+
if owner:
|
| 25 |
+
q = q.filter(models.ModelRepo.owner == owner)
|
| 26 |
+
if search:
|
| 27 |
+
like = f"%{search}%"
|
| 28 |
+
q = q.filter(models.ModelRepo.name.ilike(like) | models.ModelRepo.description.ilike(like))
|
| 29 |
+
return q.order_by(models.ModelRepo.downloads.desc()).all()
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@router.get("/{model_id}", response_model=schemas.ModelOut)
|
| 33 |
+
def get_model(model_id: str, db: Session = Depends(get_db)):
|
| 34 |
+
m = db.get(models.ModelRepo, model_id)
|
| 35 |
+
if m is None:
|
| 36 |
+
raise HTTPException(404, "Model not found")
|
| 37 |
+
return m
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@router.patch("/{model_id}", response_model=schemas.ModelOut)
|
| 41 |
+
def update_model(model_id: str, body: schemas.ModelUpdate, user=Depends(get_current_user), db: Session = Depends(get_db)):
|
| 42 |
+
m = db.get(models.ModelRepo, model_id)
|
| 43 |
+
if m is None:
|
| 44 |
+
raise HTTPException(404, "Model not found")
|
| 45 |
+
if m.owner != user.username:
|
| 46 |
+
raise HTTPException(403, "Not your model")
|
| 47 |
+
for k, v in body.model_dump(exclude_unset=True).items():
|
| 48 |
+
setattr(m, k, v)
|
| 49 |
+
db.commit()
|
| 50 |
+
db.refresh(m)
|
| 51 |
+
return m
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@router.delete("/{model_id}", status_code=204)
|
| 55 |
+
def delete_model(model_id: str, user=Depends(get_current_user), db: Session = Depends(get_db)):
|
| 56 |
+
m = db.get(models.ModelRepo, model_id)
|
| 57 |
+
if m is None:
|
| 58 |
+
raise HTTPException(404, "Model not found")
|
| 59 |
+
if m.owner != user.username:
|
| 60 |
+
raise HTTPException(403, "Not your model")
|
| 61 |
+
db.delete(m)
|
| 62 |
+
db.commit()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@router.post("/{model_id}/like", response_model=schemas.ModelOut)
|
| 66 |
+
def like_model(model_id: str, db: Session = Depends(get_db)):
|
| 67 |
+
m = db.get(models.ModelRepo, model_id)
|
| 68 |
+
if m is None:
|
| 69 |
+
raise HTTPException(404, "Model not found")
|
| 70 |
+
m.likes += 1
|
| 71 |
+
db.commit()
|
| 72 |
+
db.refresh(m)
|
| 73 |
+
return m
|