Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers import StableDiffusionControlNetPipeline
|
| 5 |
+
from annotator.util import resize_image, HWC3
|
| 6 |
+
|
| 7 |
+
# Load the pipeline
|
| 8 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained("CompVis/stable-diffusion-v1-4").to("cuda")
|
| 9 |
+
|
| 10 |
+
def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, low_threshold, high_threshold):
|
| 11 |
+
with torch.no_grad():
|
| 12 |
+
img = resize_image(HWC3(input_image), image_resolution)
|
| 13 |
+
|
| 14 |
+
if seed == -1:
|
| 15 |
+
seed = random.randint(0, 65535)
|
| 16 |
+
generator = torch.manual_seed(seed)
|
| 17 |
+
|
| 18 |
+
# Generate images using the pipeline
|
| 19 |
+
images = pipe(prompt=prompt + ', ' + a_prompt, num_inference_steps=ddim_steps, guidance_scale=scale, generator=generator, num_images_per_prompt=num_samples).images
|
| 20 |
+
|
| 21 |
+
results = [np.array(image) for image in images]
|
| 22 |
+
return results
|
| 23 |
+
|
| 24 |
+
block = gr.Blocks().queue()
|
| 25 |
+
with block:
|
| 26 |
+
with gr.Row():
|
| 27 |
+
gr.Markdown("## Scene Diffusion with ControlNet")
|
| 28 |
+
with gr.Row():
|
| 29 |
+
with gr.Column():
|
| 30 |
+
input_image = gr.Image(source='upload', type="numpy")
|
| 31 |
+
prompt = gr.Textbox(label="Prompt")
|
| 32 |
+
a_prompt = gr.Textbox(label="Additional Prompt")
|
| 33 |
+
n_prompt = gr.Textbox(label="Negative Prompt")
|
| 34 |
+
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
|
| 35 |
+
image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
|
| 36 |
+
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
|
| 37 |
+
guess_mode = gr.Checkbox(label='Guess Mode', value=False)
|
| 38 |
+
strength = gr.Slider(label="Strength", minimum=0.0, maximum=1.0, value=0.5, step=0.1)
|
| 39 |
+
scale = gr.Slider(label="Scale", minimum=0.1, maximum=30.0, value=10.0, step=0.1)
|
| 40 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=10000, value=42, step=1)
|
| 41 |
+
eta = gr.Slider(label="ETA", minimum=0.0, maximum=1.0, value=0.0, step=0.1)
|
| 42 |
+
low_threshold = gr.Slider(label="Canny Low Threshold", minimum=1, maximum=255, value=100, step=1)
|
| 43 |
+
high_threshold = gr.Slider(label="Canny High Threshold", minimum=1, maximum=255, value=200, step=1)
|
| 44 |
+
submit = gr.Button("Generate")
|
| 45 |
+
with gr.Column():
|
| 46 |
+
output_image = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
| 47 |
+
submit.click(fn=process, inputs=[input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta, low_threshold, high_threshold], outputs=output_image)
|
| 48 |
+
|
| 49 |
+
demo = block
|
| 50 |
+
demo.launch(share=True)
|