Bagda commited on
Commit
9e2f4cd
·
verified ·
1 Parent(s): d317a6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -26,21 +26,24 @@ if __name__ == "__main__":
26
 
27
  from diffusers import StableDiffusionPipeline
28
  import torch
 
 
29
 
30
- # Load model from Hugging Face Hub
 
 
 
 
 
31
  pipe = StableDiffusionPipeline.from_pretrained(
32
- "runwayml/stable-diffusion-v1-5", # You can use another model if needed
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
- # Save the image
46
- image.save("minecraft_thumbnail.png")
 
 
 
 
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"}