Spaces:
Build error
Build error
Create api.py
Browse files
api.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load model
|
| 8 |
+
model_path = "/app/models/Stable-diffusion/model.safetensors"
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 10 |
+
pipe.to("cuda") # Gunakan GPU jika tersedia
|
| 11 |
+
|
| 12 |
+
@app.post("/generate")
|
| 13 |
+
async def generate_image(prompt: str):
|
| 14 |
+
image = pipe(prompt).images[0]
|
| 15 |
+
image_path = "/app/generated_image.png"
|
| 16 |
+
image.save(image_path)
|
| 17 |
+
return {"image_url": f"/generated_image.png"}
|
| 18 |
+
|
| 19 |
+
@app.get("/generated_image.png")
|
| 20 |
+
async def get_image():
|
| 21 |
+
return FileResponse("/app/generated_image.png")
|