NikeZoldyck commited on
Commit
9d8d63c
1 Parent(s): 5842b4a

Additional changes for style transfer and examples

Browse files
Files changed (2) hide show
  1. app.py +67 -20
  2. utils/shared_utils.py +1 -1
app.py CHANGED
@@ -1,15 +1,41 @@
1
- from pathlib import Path
2
- import numpy as np
3
  import gradio as gr
4
- import utils.shared_utils as st
 
 
 
 
 
 
 
 
5
 
 
6
 
7
- import torch
 
 
 
 
8
  from torch import autocast
9
- import torchvision.transforms as T
10
  from contextlib import nullcontext
 
 
 
11
  device = "cuda" if torch.cuda.is_available() else "cpu"
12
  context = autocast if device == "cuda" else nullcontext
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Apply the transformations needed
14
 
15
 
@@ -32,8 +58,14 @@ def infer(prompt,samples):
32
  return images
33
 
34
 
 
 
 
 
 
35
 
36
-
 
37
 
38
  def change_bg_option(choice):
39
  if choice == "I have an Image":
@@ -49,16 +81,16 @@ def change_bg_option(choice):
49
  title = "FSDL- One-Shot, Green-Screen, Composition-Transfer"
50
  DEFAULT_TEXT = "Photorealistic scenery of bookshelf in a room"
51
  description = """
52
- <center><a href="https://docs.google.com/document/d/1fde8XKIMT1nNU72859ytd2c58LFBxepS3od9KFBrJbM/edit?usp=sharing">[PAPER]</a> <a href="https://github.com/snknitin/FSDL-Project/blob/main/src/utils/shared_utils.py">[CODE]</a></center>
53
  <details>
54
  <summary><b>Instructions</b></summary>
55
  <p style="margin-top: -3px;">With this app, you can generate a suitable background image to overlay your portrait!<br />You have several ways to set how your final auto-edited image will look like:<br /></p>
56
  <ul style="margin-top: -20px;margin-bottom: -15px;">
57
- <li style="margin-bottom: -10px;margin-left: 20px;">Use the "<i>Inputs</i>" tab to either upload an image from your device or allow the use of your webcam to capture</li>
58
- <li style="margin-left: 20px;">Use the "<i>Background Image Inputs</i>" to upload your own background</li>
59
- <li style="margin-left: 20px;">Use the "<i>Text prompt</i>" tab to generate a satisfactory bacground image.</li>
60
  </ul>
61
- <p>After customization, just hit "<i>Edit</i>" and wait a few seconds.<br />The final image will be available for download <br /> <b>Enjoy!<b><p>
62
  </details>
63
  """
64
 
@@ -71,6 +103,13 @@ running = """
71
  * **Smoothing** - Given than image resolutions and clarity can be an issue, this smoothing button makes your final image crisp after the stylization transfer. Fair warning - this last process can take 5-10 mins
72
  """
73
 
 
 
 
 
 
 
 
74
 
75
  demo = gr.Blocks()
76
 
@@ -101,10 +140,7 @@ with demo:
101
  placeholder="Enter your prompt to generate a background image... something like - Photorealistic scenery of bookshelf in a room")
102
 
103
  samples = gr.Slider(label="Number of Images", minimum=1, maximum=5, value=2, step=1)
104
- btn = gr.Button("Generate images",variant="primary").style(
105
- margin=False,
106
- rounded=(False, True, True, False),
107
- )
108
 
109
  gallery = gr.Gallery(label="Generated images", show_label=True).style(grid=(1, 3), height="auto")
110
  # image_options = gr.Radio(label="Pick", interactive=True, choices=None, type="value")
@@ -130,10 +166,17 @@ with demo:
130
 
131
  with gr.Row(scale=1):
132
 
133
- with gr.Column(scale=1):
134
- supimp_btn = gr.Button("SuperImpose")
135
- overlay_img = gr.Image(shape=(800, 800), label="Overlay", type="pil")
 
 
 
 
 
 
136
 
 
137
 
138
  with gr.Column(scale=1):
139
  style_btn = gr.Button("Composition-Transfer",variant="primary")
@@ -143,10 +186,14 @@ with demo:
143
  submit_btn = gr.Button("Smoothen",variant="primary")
144
  output_img = gr.Image(shape=(800, 800),label="FinalSmoothened Image",type="pil")
145
 
146
- supimp_btn.click(fn=st.superimpose, inputs=[final_input_img, final_back_img], outputs=[overlay_img])
147
- style_btn.click(fn=st.style_transfer, inputs=[overlay_img,final_input_img], outputs=[style_img])
148
  submit_btn.click(fn=st.smoother, inputs=[style_img,overlay_img], outputs=[output_img])
149
 
 
 
 
 
150
  demo.queue()
151
  demo.launch()
152
 
 
1
+ import os
 
2
  import gradio as gr
3
+ from pathlib import Path
4
+ from diffusers import StableDiffusionPipeline
5
+ from PIL import Image
6
+ from huggingface_hub import notebook_login
7
+
8
+ from huggingface_hub import notebook_login
9
+ #if not (Path.home()/'.huggingface'/'token').exists():
10
+ #token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
11
+ token = "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"
12
 
13
+ import src.utils.shared_utils as st
14
 
15
+
16
+ import torch, logging
17
+ logging.disable(logging.WARNING)
18
+ torch.cuda.empty_cache()
19
+ torch.manual_seed(3407)
20
  from torch import autocast
 
21
  from contextlib import nullcontext
22
+ torch.backends.cudnn.benchmark = True
23
+
24
+ model_id = "CompVis/stable-diffusion-v1-4"
25
  device = "cuda" if torch.cuda.is_available() else "cpu"
26
  context = autocast if device == "cuda" else nullcontext
27
+
28
+ # pipe = StableDiffusionPipeline.from_pretrained(model_id,use_auth_token=token).to(device)
29
+ #
30
+ #
31
+ # def infer_original(prompt,samples):
32
+ # with context(device):
33
+ # images = pipe(samples*[prompt], guidance_scale=7.5).images
34
+ # return images
35
+
36
+
37
+
38
+
39
  # Apply the transformations needed
40
 
41
 
 
58
  return images
59
 
60
 
61
+ # def newstyleimage(choice):
62
+ # print(choice)
63
+ # if choice == "yes":
64
+ # return gr.Image.update(visible=True,interactive=True)
65
+ # return
66
 
67
+ def styleimpose(final_input_img, ref_img):
68
+ return st.superimpose(final_input_img, ref_img)[0]
69
 
70
  def change_bg_option(choice):
71
  if choice == "I have an Image":
 
81
  title = "FSDL- One-Shot, Green-Screen, Composition-Transfer"
82
  DEFAULT_TEXT = "Photorealistic scenery of bookshelf in a room"
83
  description = """
84
+ <center><a href="https://docs.google.com/document/d/1fde8XKIMT1nNU72859ytd2c58LFBxepS3od9KFBrJbM/edit?usp=sharing">[PAPER - Documentation]</a> </center>
85
  <details>
86
  <summary><b>Instructions</b></summary>
87
  <p style="margin-top: -3px;">With this app, you can generate a suitable background image to overlay your portrait!<br />You have several ways to set how your final auto-edited image will look like:<br /></p>
88
  <ul style="margin-top: -20px;margin-bottom: -15px;">
89
+ <li style="margin-bottom: -10px;margin-left: 20px;">Use the "<i>Inputs</i>" tab to either upload an image from your device OR allow the use of your webcam to capture</li>
90
+ <li style="margin-left: 20px;">Use the "<i>Background Image Inputs</i>" to upload your own background. OR</li>
91
+ <li style="margin-left: 20px;">Use the "<i>Text prompt</i>" tab to generate a satisfactory background image using Stable Diffusion.</li>
92
  </ul>
93
+ <p>After deciding, just hit "<i>Select</i>" to ensure those images are processed.<br />The final image will be available for download <br /> <b>Enjoy!<b><p>
94
  </details>
95
  """
96
 
 
103
  * **Smoothing** - Given than image resolutions and clarity can be an issue, this smoothing button makes your final image crisp after the stylization transfer. Fair warning - this last process can take 5-10 mins
104
  """
105
 
106
+ style_message = """
107
+ This image above will be the content image. By default, the style will be copied from the input foreground image.
108
+
109
+ If you have a different image in mind, would you like to upload a different image?
110
+ Click yes to add a new style reference image"""
111
+
112
+
113
 
114
  demo = gr.Blocks()
115
 
 
140
  placeholder="Enter your prompt to generate a background image... something like - Photorealistic scenery of bookshelf in a room")
141
 
142
  samples = gr.Slider(label="Number of Images", minimum=1, maximum=5, value=2, step=1)
143
+ btn = gr.Button("Generate images",variant="primary")
 
 
 
144
 
145
  gallery = gr.Gallery(label="Generated images", show_label=True).style(grid=(1, 3), height="auto")
146
  # image_options = gr.Radio(label="Pick", interactive=True, choices=None, type="value")
 
166
 
167
  with gr.Row(scale=1):
168
 
169
+ with gr.Box():
170
+ with gr.Column(scale=1):
171
+ supimp_btn = gr.Button("SuperImpose")
172
+ overlay_img = gr.Image(shape=(800, 800), label="Overlay", type="pil")
173
+ gr.Markdown(style_message)
174
+ #img_choice = gr.Radio(choices= ["yes"],interactive=True,type='value')
175
+ ref_img = gr.Image(shape=(800, 800),label="Style Reference", type="pil",interactive=True)
176
+ ref_img2 = gr.Image(shape=(800, 800), label="Style Reference", type="pil", interactive=True, visible=False)
177
+ ref_btn = gr.Button("Use this style")
178
 
179
+ ref_btn.click(fn=styleimpose, inputs=[final_input_img, ref_img], outputs=[ref_img2])
180
 
181
  with gr.Column(scale=1):
182
  style_btn = gr.Button("Composition-Transfer",variant="primary")
 
186
  submit_btn = gr.Button("Smoothen",variant="primary")
187
  output_img = gr.Image(shape=(800, 800),label="FinalSmoothened Image",type="pil")
188
 
189
+ supimp_btn.click(fn=st.superimpose, inputs=[final_input_img, final_back_img], outputs=[overlay_img,ref_img])
190
+ style_btn.click(fn=st.style_transfer, inputs=[overlay_img,ref_img2], outputs=[style_img])
191
  submit_btn.click(fn=st.smoother, inputs=[style_img,overlay_img], outputs=[output_img])
192
 
193
+
194
+ gr.Examples([["profile_new.png","back_img.png"]],[final_input_img, final_back_img])
195
+ gr.Examples([["profile_new.png","bedroom with a bookshelf in the background and a small stool to sit on the right side, photorealistic",3]], [final_input_img,text,samples])
196
+
197
  demo.queue()
198
  demo.launch()
199
 
utils/shared_utils.py CHANGED
@@ -80,7 +80,7 @@ def memory_limit_image_resize(cont_img):
80
  def superimpose(input_img,back_img):
81
  matte_img = remove(input_img)
82
  back_img.paste(matte_img, (0, 0), matte_img)
83
- return back_img
84
 
85
 
86
 
 
80
  def superimpose(input_img,back_img):
81
  matte_img = remove(input_img)
82
  back_img.paste(matte_img, (0, 0), matte_img)
83
+ return back_img,input_img
84
 
85
 
86