Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model (takes a few seconds on start)
|
| 6 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 8 |
+
model_id,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
revision="fp16",
|
| 11 |
+
use_auth_token=True
|
| 12 |
+
)
|
| 13 |
+
pipe = pipe.to("cuda")
|
| 14 |
+
|
| 15 |
+
# Define inference function
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
image = pipe(prompt).images[0]
|
| 18 |
+
return image
|
| 19 |
+
|
| 20 |
+
# Create Gradio interface
|
| 21 |
+
description = "Enter a description below and the app will generate an image based on it."
|
| 22 |
+
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=generate_image,
|
| 25 |
+
inputs=gr.Textbox(label="Enter image description", placeholder="e.g. A tree on a mountain during sunset"),
|
| 26 |
+
outputs=gr.Image(type="pil"),
|
| 27 |
+
title="Text to Image Generator",
|
| 28 |
+
description=description,
|
| 29 |
+
theme="default"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|