Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,48 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import glob
|
| 3 |
-
import argparse
|
| 4 |
-
import shutil
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import PIL
|
| 7 |
-
import gradio as gr
|
| 8 |
-
import random
|
| 9 |
-
import numpy as np
|
| 10 |
-
from diffusion import generate_latent
|
| 11 |
-
from vq_vae import create_mask
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import glob
|
| 3 |
+
import argparse
|
| 4 |
+
import shutil
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import PIL
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import random
|
| 9 |
+
import numpy as np
|
| 10 |
+
from diffusion import generate_latent
|
| 11 |
+
from vq_vae import create_mask
|
| 12 |
+
from huggingface_hub import snapshot_download
|
| 13 |
+
import spaces
|
| 14 |
+
|
| 15 |
+
# model_dir = 'trained_models'
|
| 16 |
+
|
| 17 |
+
from huggingface_hub import login
|
| 18 |
+
login(token = os.getenv('HF_TOKEN'))
|
| 19 |
+
|
| 20 |
+
local_dir = snapshot_download(
|
| 21 |
+
repo_id="srijaydeshpande/diffusion"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
@spaces.GPU(duration=120)
|
| 25 |
+
def create_image(cancer_type):
|
| 26 |
+
tmp_dir = "./tmp"
|
| 27 |
+
if os.path.exists(tmp_dir):
|
| 28 |
+
shutil.rmtree(tmp_dir)
|
| 29 |
+
os.makedirs(tmp_dir)
|
| 30 |
+
generate_latent(model_dir, cancer_type, tmp_dir)
|
| 31 |
+
create_mask(model_dir, "./tmp", "./tmp/test_masks")
|
| 32 |
+
os.system('python pix2pixhd_test.py --name diffusion_dp --dataroot ./tmp --label_nc 0 --no_instance --resize_or_crop none')
|
| 33 |
+
image_dir = "./tmp/diffusion_dp/test_latest/images"
|
| 34 |
+
input_label_image = Image.open(os.path.join(image_dir, "sample_input_label.jpg"))
|
| 35 |
+
synthesized_image = Image.open(os.path.join(image_dir, "sample_synthesized_image.jpg"))
|
| 36 |
+
return input_label_image, synthesized_image
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(
|
| 40 |
+
create_image,
|
| 41 |
+
inputs=gr.Radio(choices=["benign", "malignant"], label="Choose Type", value="benign"),
|
| 42 |
+
outputs=[gr.Image(), gr.Image()],
|
| 43 |
+
title="Diffusion based Image Generation"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
demo.launch()
|
| 47 |
+
|
| 48 |
+
# create_image('benign')
|