Update app.py
#1
by gg34455 - opened
app.py
CHANGED
|
@@ -5,21 +5,24 @@ from torchvision import transforms
|
|
| 5 |
from PIL import Image
|
| 6 |
from src.lightning_module import StyleTransferModule
|
| 7 |
|
| 8 |
-
|
| 9 |
MODEL_URL = "https://huggingface.co/Michal-Raszkowski/adain-style-transfer/resolve/main/style-transfer-best-v2.ckpt?download=true"
|
| 10 |
CHECKPOINT_PATH = "model.ckpt"
|
| 11 |
|
| 12 |
def download_model_if_missing():
|
| 13 |
if not os.path.exists(CHECKPOINT_PATH):
|
|
|
|
| 14 |
torch.hub.download_url_to_file(MODEL_URL, CHECKPOINT_PATH)
|
| 15 |
|
| 16 |
def load_model():
|
| 17 |
download_model_if_missing()
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
model.eval()
|
| 20 |
-
return model
|
| 21 |
|
| 22 |
-
model
|
|
|
|
| 23 |
|
| 24 |
def stylize(content_image, style_image, alpha):
|
| 25 |
if content_image is None or style_image is None:
|
|
@@ -30,34 +33,46 @@ def stylize(content_image, style_image, alpha):
|
|
| 30 |
transforms.ToTensor()
|
| 31 |
])
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
with torch.no_grad():
|
| 37 |
generated_tensor, _ = model(c, s, alpha=alpha)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
return result_image
|
| 43 |
|
|
|
|
| 44 |
with gr.Blocks(title="Style Transfer Demo", theme=gr.themes.Soft()) as demo:
|
| 45 |
-
gr.Markdown("Neural Style Transfer")
|
| 46 |
-
gr.Markdown("Upload content and style
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column():
|
| 50 |
-
input_content = gr.Image(label="Content
|
| 51 |
-
input_style = gr.Image(label="Style
|
| 52 |
-
slider = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, label="Style
|
| 53 |
btn = gr.Button("Generate", variant="primary")
|
| 54 |
|
| 55 |
with gr.Column():
|
| 56 |
-
output = gr.Image(label="Output", type="pil")
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from src.lightning_module import StyleTransferModule
|
| 7 |
|
|
|
|
| 8 |
MODEL_URL = "https://huggingface.co/Michal-Raszkowski/adain-style-transfer/resolve/main/style-transfer-best-v2.ckpt?download=true"
|
| 9 |
CHECKPOINT_PATH = "model.ckpt"
|
| 10 |
|
| 11 |
def download_model_if_missing():
|
| 12 |
if not os.path.exists(CHECKPOINT_PATH):
|
| 13 |
+
print("Downloading model checkpoint...")
|
| 14 |
torch.hub.download_url_to_file(MODEL_URL, CHECKPOINT_PATH)
|
| 15 |
|
| 16 |
def load_model():
|
| 17 |
download_model_if_missing()
|
| 18 |
+
# Loading to CPU by default; change to "cuda" if GPU is available
|
| 19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
+
model = StyleTransferModule.load_from_checkpoint(CHECKPOINT_PATH, map_location=device)
|
| 21 |
model.eval()
|
| 22 |
+
return model, device
|
| 23 |
|
| 24 |
+
# Initialize model and get target device
|
| 25 |
+
model, device = load_model()
|
| 26 |
|
| 27 |
def stylize(content_image, style_image, alpha):
|
| 28 |
if content_image is None or style_image is None:
|
|
|
|
| 33 |
transforms.ToTensor()
|
| 34 |
])
|
| 35 |
|
| 36 |
+
# Transform and push tensors to the correct device (CPU/GPU)
|
| 37 |
+
c = transform(content_image).unsqueeze(0).to(device)
|
| 38 |
+
s = transform(style_image).unsqueeze(0).to(device)
|
| 39 |
|
| 40 |
with torch.no_grad():
|
| 41 |
generated_tensor, _ = model(c, s, alpha=alpha)
|
| 42 |
|
| 43 |
+
# Bring back to CPU, clamp values, remove batch dimension, and convert to PIL
|
| 44 |
+
generated_tensor = torch.clamp(generated_tensor, 0, 1).cpu().squeeze(0)
|
| 45 |
+
result_image = transforms.ToPILImage()(generated_tensor)
|
| 46 |
|
| 47 |
return result_image
|
| 48 |
|
| 49 |
+
# Build the Gradio Interface
|
| 50 |
with gr.Blocks(title="Style Transfer Demo", theme=gr.themes.Soft()) as demo:
|
| 51 |
+
gr.Markdown("# Neural Style Transfer")
|
| 52 |
+
gr.Markdown("Upload a content image and a style image to combine them using AdaIN.")
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
+
input_content = gr.Image(label="Content Image", type="pil", height=300)
|
| 57 |
+
input_style = gr.Image(label="Style Image", type="pil", height=300)
|
| 58 |
+
slider = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.1, label="Style Strength")
|
| 59 |
btn = gr.Button("Generate", variant="primary")
|
| 60 |
|
| 61 |
with gr.Column():
|
| 62 |
+
output = gr.Image(label="Output Image", type="pil")
|
| 63 |
|
| 64 |
+
# Set up the click event listener
|
| 65 |
+
btn.click(
|
| 66 |
+
fn=stylize,
|
| 67 |
+
inputs=[input_content, input_style, slider],
|
| 68 |
+
outputs=output
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Optional: Uncomment if you want to include default examples
|
| 72 |
+
# gr.Examples(
|
| 73 |
+
# examples=[["examples/c.jpg", "examples/s.jpg", 1.0]],
|
| 74 |
+
# inputs=[input_content, input_style, slider]
|
| 75 |
+
# )
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
| 78 |
demo.launch()
|