| 203 | |
| 204 | |
| 205 | def main(): |
| 206 | torch.cuda.manual_seed(25) |
| 207 | |
| 208 | with gr.Blocks() as demo: |
| 209 | gr.Markdown( |
| 210 | """ |
| 211 | # DragGAN |
| 212 | |
| 213 | Unofficial implementation of [Drag Your GAN: Interactive Point-based Manipulation on the Generative Image Manifold](https://vcai.mpi-inf.mpg.de/projects/DragGAN/) |
| 214 | |
| 215 | [Our Implementation](https://github.com/Zeqiang-Lai/DragGAN) | [Official Implementation](https://github.com/XingangPan/DragGAN) |
| 216 | |
| 217 | ## Tutorial |
| 218 | |
| 219 | 1. (Opklional) Draw a mask indicate the movable region. |
| 220 | 2. Setup a least one pair of handle point and target point. |
| 221 | 3. Click "Drag it". |
| 222 | |
| 223 | ## Hints |
| 224 | |
| 225 | - Handle points (Blue): the point you want to drag. |
| 226 | - Target points (Red): the destination you want to drag towards to. |
| 227 | |
| 228 | ## Primary Support of Custom Image. |
| 229 | |
| 230 | - We now support dragging user uploaded image by GAN inversion. |
| 231 | - **Please upload your image at `Setup Handle Points` pannel.** Upload it from `Draw a Mask` would cause errors for now. |
| 232 | - Due to the limitation of GAN inversion, |
| 233 | - You might wait roughly 1 minute to see the GAN version of the uploaded image. |
| 234 | - The shown image might be slightly difference from the uploaded one. |
| 235 | - It could also fail to invert the uploaded image and generate very poor results. |
| 236 | - Idealy, you should choose the closest model of the uploaded image. For example, choose `stylegan2-ffhq-config-f.pkl` for human face. `stylegan2-cat-config-f.pkl` for cat. |
| 237 | |
| 238 | > Please fire an issue if you have encounted any problem. Also don't forgot to give a star to the [Official Repo](https://github.com/XingangPan/DragGAN), [our project](https://github.com/Zeqiang-Lai/DragGAN) could not exist without it. |
| 239 | """, |
| 240 | ) |
| 241 | G = draggan.load_model(utils.get_path(DEFAULT_CKPT), device=device) |
| 242 | model = gr.State({'G': G}) |
| 243 | W = draggan.generate_W( |
| 244 | G, |
| 245 | seed=int(1), |
| 246 | device=device, |
| 247 | truncation_psi=0.8, |
| 248 | truncation_cutoff=8, |
| 249 | ) |
| 250 | img, F0 = draggan.generate_image(W, G, device=device) |
| 251 | |
| 252 | state = gr.State({ |
| 253 | 'W': W, |
| 254 | 'img': img, |
| 255 | 'history': [] |
| 256 | }) |
| 257 | points = gr.State({'target': [], 'handle': []}) |
| 258 | size = gr.State(CKPT_SIZE[DEFAULT_CKPT]) |
| 259 | target_point = gr.State(False) |
| 260 | |
| 261 | with gr.Row(): |
| 262 | with gr.Column(scale=0.3): |