Spaces:
Sleeping
Sleeping
File size: 1,143 Bytes
c1f97cf b6f37ab c1f97cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | from fastapi import FastAPI
from fastapi.responses import JSONResponse
from diffusers import DiffusionPipeline
import torch
import base64
from io import BytesIO
# create fastapi instance
app = FastAPI()
# Detect device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load the correct pipeline
pipe = DiffusionPipeline.from_pretrained(
"segmind/SSD-1B",
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
use_safetensors=True,
)
pipe = pipe.to(device)
# Optional: reduce VRAM usage
pipe.enable_attention_slicing()
@app.get("/")
def home():
return {"message": "Segmind SSD-1B API running"}
# Define function to generate image from text prompt
@app.post("/generate-image/")
def generate_image(prompt: str, negative_prompt: str = None):
# Run inference
image = pipe(prompt=prompt, negative_prompt=negative_prompt).images[0]
# Convert to base64 string
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return JSONResponse(content={"image_base64": img_str})
|