Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install --upgrade diffusers transformers
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers import StableDiffusionPipeline
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class CFG:
|
| 8 |
+
device = "cuda"
|
| 9 |
+
seed = 42
|
| 10 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 11 |
+
image_gen_steps = 35
|
| 12 |
+
image_gen_model_id = "stabilityai/stable-diffusion-2"
|
| 13 |
+
image_gen_size = (400, 400)
|
| 14 |
+
image_gen_guidance_scale = 9
|
| 15 |
+
|
| 16 |
+
image_gen_model = StableDiffusionPipeline.from_pretrained(
|
| 17 |
+
CFG.image_gen_model_id, torch_dtype=torch.float16,
|
| 18 |
+
revision="fp16", use_auth_token='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', guidance_scale=9
|
| 19 |
+
)
|
| 20 |
+
apply = image_gen_model.to(CFG.device)
|
| 21 |
+
|
| 22 |
+
def generate_image(prompt):
|
| 23 |
+
image = apply(
|
| 24 |
+
prompt, num_inference_steps=CFG.image_gen_steps,
|
| 25 |
+
generator=CFG.generator,
|
| 26 |
+
guidance_scale=CFG.image_gen_guidance_scale
|
| 27 |
+
).images[0]
|
| 28 |
+
image = image.resize(CFG.image_gen_size)
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
title = "Stable Diffusion Image Generator"
|
| 33 |
+
description = "Enter a prompt to generate an image using the Stable Diffusion model."
|
| 34 |
+
|
| 35 |
+
iface = gr.Interface(fn=generate_image, inputs="text", outputs="image", title=title, description=description)
|
| 36 |
+
iface.launch()
|