lelafav502 commited on
Commit
b321c35
·
1 Parent(s): 49c5b35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -1,16 +1,33 @@
 
1
  import torch
2
  from diffusers import StableDiffusionPipeline
 
 
3
 
 
4
  pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
5
-
6
  pipe = pipe.to("cuda")
7
 
8
- prompt = "a photograph of an astronaut riding a horse"
9
-
10
- image = pipe(prompt).images[0] # image here is in [PIL format](https://pillow.readthedocs.io/en/stable/)
 
 
 
 
11
 
12
- # Now to display an image you can either save it such as:
13
- image.save(f"astronaut_rides_horse.png")
 
 
 
 
 
 
 
 
 
 
14
 
15
- # or if you're in a google colab you can directly display it with
16
- image
 
1
+ import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline
4
+ from PIL import Image
5
+ import io
6
 
7
+ # Load the model
8
  pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
 
9
  pipe = pipe.to("cuda")
10
 
11
+ def generate_image(prompt):
12
+ image = pipe(prompt).images[0]
13
+ # Save the image to a bytes buffer
14
+ img_buffer = io.BytesIO()
15
+ image.save(img_buffer, format="PNG")
16
+ img_buffer.seek(0)
17
+ return img_buffer
18
 
19
+ iface = gr.Interface(
20
+ fn=generate_image,
21
+ inputs="text",
22
+ outputs="image",
23
+ layout="vertical",
24
+ inputs_label="Enter Prompt",
25
+ outputs_label="Generated Image",
26
+ examples=[
27
+ ["a photograph of an astronaut riding a horse"],
28
+ ["a beautiful sunset over the mountains"]
29
+ ]
30
+ )
31
 
32
+ if __name__ == "__main__":
33
+ iface.launch()