Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,23 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Load model at startup
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 10 |
+
"runwayml/stable-diffusion-v1-5",
|
| 11 |
+
torch_dtype=torch.float16,
|
| 12 |
+
revision="fp16"
|
| 13 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
|
| 15 |
+
class Prompt(BaseModel):
|
| 16 |
+
text: str
|
| 17 |
+
|
| 18 |
+
@app.post("/generate")
|
| 19 |
+
def generate_image(prompt: Prompt):
|
| 20 |
+
image = pipe(prompt.text).images[0]
|
| 21 |
+
image_path = f"output.png"
|
| 22 |
+
image.save(image_path)
|
| 23 |
+
return {"message": "Image generated", "image_path": image_path}
|