Tanut commited on
Commit
184daa2
·
1 Parent(s): 51d9f34

Try stable diffusion

Browse files
Files changed (2) hide show
  1. app.py +28 -51
  2. requirements.txt +2 -4
app.py CHANGED
@@ -1,55 +1,32 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
4
- from PIL import Image
5
- import base64
6
- from io import BytesIO
7
-
8
- # You can change these:
9
- BASE_MODEL = "runwayml/stable-diffusion-v1-5"
10
- CONTROLNET_ID = "lllyasviel/sd-controlnet-canny" # placeholder; change to a QR-focused ControlNet if you have one
11
-
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
-
14
- controlnet = ControlNetModel.from_pretrained(
15
- CONTROLNET_ID, torch_dtype=torch.float16 if device=="cuda" else torch.float32
16
- )
17
-
18
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
19
- BASE_MODEL,
20
- controlnet=controlnet,
21
- torch_dtype=torch.float16 if device=="cuda" else torch.float32,
22
- safety_checker=None
 
 
 
 
 
 
23
  )
24
- pipe.to(device)
25
-
26
- def generate(prompt, control_image, guidance_scale=7.5, steps=30, seed=0):
27
- print("API called:", type(control_image))
28
- generator = torch.Generator(device=device).manual_seed(int(seed)) if seed else None
29
- img = pipe(
30
- prompt=prompt,
31
- image=control_image,
32
- num_inference_steps=int(steps),
33
- guidance_scale=float(guidance_scale),
34
- generator=generator
35
- ).images[0]
36
- return img
37
-
38
- with gr.Blocks() as demo:
39
- gr.Markdown("# ControlNet Image Generator")
40
- with gr.Row():
41
- prompt = gr.Textbox(label="Prompt", value="A futuristic poster, high detail")
42
- seed = gr.Number(label="Seed (0=random)", value=0)
43
- with gr.Row():
44
- control = gr.Image(type="pil", label="Control image (e.g., QR or edge map)")
45
- steps = gr.Slider(10, 50, 30, step=1, label="Steps")
46
- guidance = gr.Slider(1.0, 12.0, 7.5, step=0.1, label="Guidance scale")
47
- out = gr.Image(label="Result")
48
-
49
- btn = gr.Button("Generate")
50
- btn.click(generate, [prompt, control, guidance, steps, seed], out)
51
-
52
- # Enable simple API use
53
- gr.Examples([], inputs=[prompt, control, guidance, steps, seed], outputs=out)
54
 
55
- demo.launch()
 
 
1
  import gradio as gr
2
  import torch
3
+ from diffusers import StableDiffusionPipeline
4
+
5
+ # Load Stable Diffusion (v1.5)
6
+ device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
7
+ dtype = torch.float16 if device != "cpu" else torch.float32
8
+
9
+ pipe = StableDiffusionPipeline.from_pretrained(
10
+ "runwayml/stable-diffusion-v1-5",
11
+ torch_dtype=dtype
12
+ ).to(device)
13
+
14
+ def generate(prompt, steps, guidance, seed):
15
+ generator = torch.Generator(device=device).manual_seed(int(seed)) if seed != 0 else None
16
+ with torch.autocast(device):
17
+ image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance, generator=generator).images[0]
18
+ return image
19
+
20
+ demo = gr.Interface(
21
+ fn=generate,
22
+ inputs=[
23
+ gr.Textbox(label="Prompt", value="A fantasy castle at sunset"),
24
+ gr.Slider(10, 50, value=30, label="Steps"),
25
+ gr.Slider(1, 12, value=7.5, label="Guidance Scale"),
26
+ gr.Number(value=0, label="Seed (0 = random)")
27
+ ],
28
+ outputs=gr.Image()
29
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ if __name__ == "__main__":
32
+ demo.launch()
requirements.txt CHANGED
@@ -1,8 +1,6 @@
1
  torch
2
- diffusers>=0.27.0
3
  transformers
4
  accelerate
5
  safetensors
6
- controlnet-aux
7
- gradio>=4.0.0
8
- Pillow
 
1
  torch
2
+ diffusers
3
  transformers
4
  accelerate
5
  safetensors
6
+ gradio