speedartificialintelligence1122 commited on
Commit
356ca95
·
verified ·
1 Parent(s): a3a772f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -3
app.py CHANGED
@@ -1,7 +1,23 @@
1
  from fastapi import FastAPI
 
 
 
2
 
3
  app = FastAPI()
4
 
5
- @app.get("/")
6
- def root():
7
- return {"message": "Welcome to SpeedAI Image Generator 🚀"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}