shanetx commited on
Commit
406cd21
1 Parent(s): 407b04d

add two sections

Browse files
Files changed (1) hide show
  1. app.py +58 -6
app.py CHANGED
@@ -6,22 +6,74 @@ print(f"Is CUDA available: {torch.cuda.is_available()}")
6
  # True
7
  print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
8
 
9
- from diffusers import StableDiffusionImg2ImgPipeline
10
 
11
  os.environ['GRADIO_THEME'] = 'default'
12
  # load the pipeline
13
  device = "cuda"
14
- model_id_or_path = "runwayml/stable-diffusion-v1-5"
15
- img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
16
  img2img_pipe = img2img_pipe.to(device)
17
 
 
 
 
18
 
19
  def img2img_diff(prompt, pil_img):
20
  img = pil_img.resize((768, 512))
21
  return img2img_pipe(prompt=prompt, image=img, strength=0.75, guidance_scale=7.5).images[0]
22
 
 
 
23
 
24
- app = gr.Interface(fn=img2img_diff, inputs=[gr.Text(label="prompt text"), gr.Image(type='pil', label='draft image')],
25
- outputs=gr.Image(type='pil'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- app.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # True
7
  print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
8
 
9
+ from diffusers import StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline
10
 
11
  os.environ['GRADIO_THEME'] = 'default'
12
  # load the pipeline
13
  device = "cuda"
14
+ model_id_img2img = "runwayml/stable-diffusion-v1-5"
15
+ img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(model_id_img2img, torch_dtype=torch.float16)
16
  img2img_pipe = img2img_pipe.to(device)
17
 
18
+ model_id_inpaint = "runwayml/stable-diffusion-inpainting"
19
+ inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(model_id_inpaint, torch_dtype=torch.float16)
20
+ inpaint_pipe = inpaint_pipe.to(device)
21
 
22
  def img2img_diff(prompt, pil_img):
23
  img = pil_img.resize((768, 512))
24
  return img2img_pipe(prompt=prompt, image=img, strength=0.75, guidance_scale=7.5).images[0]
25
 
26
+ def imginpaint_diff(prompt, pil_img, mask_pil_img):
27
+ return inpaint_pipe(prompt=prompt, image=pil_img, mask_image=mask_pil_img).images[0]
28
 
29
+ def header_html(title):
30
+ return f"""
31
+ <div style="text-align: center; max-width: 650px; margin: 0 auto; padding-top: 7px;">
32
+ <div
33
+ style="
34
+ display: inline-flex;
35
+ align-items: center;
36
+ gap: 0.8rem;
37
+ font-size: 1.75rem;
38
+ "
39
+ >
40
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
41
+ {title}
42
+ </h1>
43
+ </div>
44
+ </div>
45
+ """
46
 
47
+
48
+ with gr.Blocks() as block:
49
+ with gr.Group():
50
+ with gr.Box():
51
+ gr.HTML(header_html("diffusion image to image transform"))
52
+ with gr.Row():
53
+ with gr.Column():
54
+ input_img = gr.Image(type='pil', label='draft image')
55
+ with gr.Row():
56
+ input_prompt = gr.Text(lable="prompt text")
57
+ sumit_button = gr.Button("Generate image").style(
58
+ margin=False,
59
+ rounded=(False, True, True, False),
60
+ full_width=False,
61
+ )
62
+ output_img = gr.Image(type="pil")
63
+ sumit_button.click(img2img_diff, inputs=[input_prompt, input_img], outputs=[output_img])
64
+ with gr.Box():
65
+ gr.HTML(header_html("diffusion image inpaint"))
66
+ with gr.Row():
67
+ with gr.Column():
68
+ input_img = gr.Image(type='pil', label='origin image')
69
+ mask_img = gr.Image(type='pil', label='mask image')
70
+ with gr.Row():
71
+ input_prompt = gr.Text(lable="prompt text")
72
+ sumit_button = gr.Button("Generate image").style(
73
+ margin=False,
74
+ rounded=(False, True, True, False),
75
+ full_width=False,
76
+ )
77
+ output_img = gr.Image(type="pil")
78
+ sumit_button.click(imginpaint_diff, inputs=[input_prompt, input_img, mask_img], outputs=[output_img])
79
+ block.queue(concurrency_count=40, max_size=20).launch(max_threads=150)