Update app.py
Browse files
app.py
CHANGED
|
@@ -26,21 +26,24 @@ if __name__ == "__main__":
|
|
| 26 |
|
| 27 |
from diffusers import StableDiffusionPipeline
|
| 28 |
import torch
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 32 |
-
"runwayml/stable-diffusion-v1-5",
|
| 33 |
torch_dtype=torch.float16,
|
| 34 |
use_safetensors=True,
|
| 35 |
revision="fp16"
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
# Move to GPU if available
|
| 39 |
-
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 40 |
-
|
| 41 |
-
# Generate an image
|
| 42 |
-
prompt = "A Minecraft player standing on a hill during sunset, pixel art style"
|
| 43 |
-
image = pipe(prompt).images[0]
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
from diffusers import StableDiffusionPipeline
|
| 28 |
import torch
|
| 29 |
+
from fastapi import FastAPI
|
| 30 |
+
from pydantic import BaseModel
|
| 31 |
|
| 32 |
+
app = FastAPI()
|
| 33 |
+
|
| 34 |
+
class Prompt(BaseModel):
|
| 35 |
+
prompt: str
|
| 36 |
+
|
| 37 |
+
# Load once
|
| 38 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 39 |
+
"runwayml/stable-diffusion-v1-5",
|
| 40 |
torch_dtype=torch.float16,
|
| 41 |
use_safetensors=True,
|
| 42 |
revision="fp16"
|
| 43 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
@app.post("/generate")
|
| 46 |
+
def generate_image(data: Prompt):
|
| 47 |
+
image = pipe(data.prompt).images[0]
|
| 48 |
+
image.save("output.png")
|
| 49 |
+
return {"message": "Image saved as output.png"}
|