Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
|
| 5 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# load once on startup
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 10 |
+
pipe = pipe.to(device)
|
| 11 |
+
pipe.enable_attention_slicing()
|
| 12 |
+
|
| 13 |
+
def generate(prompt, steps, guidance, seed):
|
| 14 |
+
generator = None
|
| 15 |
+
if seed not in (None, "", "none"):
|
| 16 |
+
generator = torch.Generator(device).manual_seed(int(seed))
|
| 17 |
+
image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=float(guidance), generator=generator).images[0]
|
| 18 |
+
return image
|
| 19 |
+
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("# Stable Diffusion — Space")
|
| 22 |
+
with gr.Row():
|
| 23 |
+
prompt = gr.Textbox(label="Prompt", lines=2, value="A cinematic portrait of a Muslim scholar reading under a lamp, warm tones, detailed, realistic")
|
| 24 |
+
with gr.Row():
|
| 25 |
+
steps = gr.Slider(10, 50, value=30, step=1, label="Steps")
|
| 26 |
+
guidance = gr.Slider(1.0, 12.0, value=7.5, step=0.5, label="Guidance")
|
| 27 |
+
seed = gr.Textbox(label="Seed (optional)")
|
| 28 |
+
btn = gr.Button("Generate")
|
| 29 |
+
output = gr.Image(label="Generated image")
|
| 30 |
+
|
| 31 |
+
btn.click(generate, inputs=[prompt, steps, guidance, seed], outputs=[output])
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch()
|