naonauno commited on
Commit
e29a7a0
·
verified ·
1 Parent(s): e9bef71

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline, ControlNetModel
4
+ from safetensors.torch import load_file
5
+
6
+ # Initialize the pipeline with CPU
7
+ model_id = "runwayml/stable-diffusion-v1-5"
8
+ pipe = StableDiffusionPipeline.from_pretrained(
9
+ model_id,
10
+ torch_dtype=torch.float32,
11
+ )
12
+
13
+ # Load your ControlNet LoRA
14
+ lora_path = "naonauno/40k-half-sd15"
15
+ pipe.load_lora_weights(lora_path)
16
+
17
+ # Merge LoRA weights
18
+ pipe.unet.load_attn_procs(lora_state_dict)
19
+
20
+ def generate_image(prompt, negative_prompt, guidance_scale, steps):
21
+ with torch.no_grad():
22
+ image = pipe(
23
+ prompt=prompt,
24
+ negative_prompt=negative_prompt,
25
+ num_inference_steps=steps,
26
+ guidance_scale=guidance_scale,
27
+ ).images[0]
28
+ return image
29
+
30
+ # Create the Gradio interface
31
+ with gr.Blocks() as demo:
32
+ with gr.Row():
33
+ with gr.Column():
34
+ prompt = gr.Textbox(label="Prompt")
35
+ negative_prompt = gr.Textbox(label="Negative Prompt")
36
+ guidance_scale = gr.Slider(minimum=1, maximum=20, value=7.5, label="Guidance Scale")
37
+ steps = gr.Slider(minimum=1, maximum=100, value=50, label="Steps")
38
+ generate = gr.Button("Generate")
39
+
40
+ with gr.Column():
41
+ result = gr.Image(label="Generated Image")
42
+
43
+ generate.click(
44
+ fn=generate_image,
45
+ inputs=[prompt, negative_prompt, guidance_scale, steps],
46
+ outputs=result
47
+ )
48
+
49
+ demo.launch()