main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from fastapi import FastAPI, HTTPException
  2. from pydantic import BaseModel
  3. from typing import Optional
  4. import uvicorn
  5. import sys
  6. from liblibai_client import LibLibAIClient
  7. app = FastAPI(title="LibLib ControlNet API")
  8. class GenerateRequest(BaseModel):
  9. image: str
  10. prompt: str
  11. negative_prompt: str = "lowres, bad anatomy, text, error"
  12. width: int = 512
  13. height: int = 512
  14. steps: int = 20
  15. cfg_scale: float = 7
  16. img_count: int = 1
  17. control_weight: float = 1.0
  18. preprocessor: int = 1
  19. canny_low: int = 100
  20. canny_high: int = 200
  21. @app.post("/generate")
  22. async def generate(req: GenerateRequest):
  23. try:
  24. client = LibLibAIClient()
  25. result = client.generate_image(
  26. image=req.image,
  27. prompt=req.prompt,
  28. negative_prompt=req.negative_prompt,
  29. width=req.width,
  30. height=req.height,
  31. steps=req.steps,
  32. cfg_scale=req.cfg_scale,
  33. img_count=req.img_count,
  34. control_weight=req.control_weight,
  35. preprocessor=req.preprocessor,
  36. canny_low=req.canny_low,
  37. canny_high=req.canny_high
  38. )
  39. return result
  40. except Exception as e:
  41. raise HTTPException(status_code=500, detail=str(e))
  42. @app.get("/health")
  43. async def health():
  44. return {"status": "ok"}
  45. if __name__ == "__main__":
  46. port = int(sys.argv[sys.argv.index("--port") + 1]) if "--port" in sys.argv else 8001
  47. uvicorn.run(app, host="0.0.0.0", port=port)