| import io, os |
| from typing import Optional |
| from fastapi import FastAPI |
| from fastapi.responses import StreamingResponse, JSONResponse |
| from pydantic import BaseModel |
| from huggingface_hub import InferenceClient |
|
|
| app = FastAPI(title="ImageGen API") |
|
|
| |
| |
| MODEL_ID = os.getenv("MODEL", "stabilityai/stable-diffusion-xl-base-1.0") |
| HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
| |
| if HF_TOKEN: |
| client = InferenceClient(model=MODEL_ID, token=HF_TOKEN) |
| else: |
| |
| client = InferenceClient(model=MODEL_ID) |
|
|
| class GenReq(BaseModel): |
| prompt: str |
| width: int = 768 |
| height: int = 768 |
| seed: Optional[int] = None |
| num_inference_steps: Optional[int] = None |
|
|
| @app.get("/") |
| def root(): |
| return {"ok": True, "message": "API is live", "model": MODEL_ID} |
|
|
| @app.post("/generate") |
| def generate(req: GenReq): |
| try: |
| |
| if "sd-turbo" in MODEL_ID.lower(): |
| |
| img = client.text_to_image( |
| prompt=req.prompt, |
| num_inference_steps=1, |
| guidance_scale=0.0, |
| width=req.width, |
| height=req.height |
| ) |
| else: |
| |
| img = client.text_to_image( |
| prompt=req.prompt, |
| width=req.width, |
| height=req.height, |
| seed=req.seed, |
| num_inference_steps=req.num_inference_steps or 20 |
| ) |
| |
| buf = io.BytesIO() |
| img.save(buf, format="PNG") |
| buf.seek(0) |
| return StreamingResponse(buf, media_type="image/png") |
| except Exception as e: |
| return JSONResponse({"error": str(e)}, status_code=500) |
|
|
| |
| class Prompt(BaseModel): |
| text: str |
|
|
| @app.post("/echo") |
| def echo(p: Prompt): |
| return {"length": len(p.text), "upper": p.text.upper()} |
|
|