Spaces:
Sleeping
Sleeping
| 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() | |
| def home(): | |
| return {"message": "Segmind SSD-1B API running"} | |
| # Define function to generate image from text prompt | |
| 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}) | |