Spaces:
Build error
Build error
Update app.py
Browse files9 unmodified lines1011121314156 unmodified lines22232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182839 unmodified linesfrom deoldify.device_id import DeviceIdfrom deoldify.visualize import *from huggingface_hub import HfApi# Suppress warningswarnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")6 unmodified linesdevice.set(device=DeviceId.CPU) # Use CPU firstapi = HfApi()snapshot_folder = api.snapshot_download(repo_id=repo_id, repo_type=repo_type)device.set(device=DeviceId.GPU0) # If GPU available# Load model_colorizer = get_image_colorizer(root_folder=Path(snapshot_folder), artistic=True)def colorizer_fn(input_img):"""Colorize grayscale image with render factor 40 and return comparison image."""if input_img:# Load original imageoriginal = Image.open(input_img).convert("RGB")# Colorize the imageoutput_img = _colorizer.get_transformed_image(path=input_img,render_factor=40, # Fixed render factorwatermarked=False,post_process=True,)# Ensure both images have the same sizecolorized = output_img.resize(original.size)# Merge images side by sidemerged = Image.new("RGB", (original.width * 2, original.height))merged.paste(original, (0, 0))merged.paste(colorized, (original.width, 0))return merged # Return combined imageelse:return None# ✅ Custom CSS to:# - Hide share button# - Fix image in place (disable dragging)# - Full-screen outputcustom_css = """body, .gradio-container { background-color: black !important; color: white !important; }.gr-box { width: 100% !important; }img { max-width: 100vw !important; max-height: 90vh !important; object-fit: contain; }footer {display: none !important;} /* Hide footer */button[title="Share"] { display: none !important; } /* Hides the Share button */img { pointer-events: none !important; } /* Prevents image dragging */"""# Gradio Interfacewith gr.Blocks(css=custom_css) as demo:gr.Markdown("# Colorize Image")# File uploaderimage_input = gr.File(label="Select Image", file_types=[".png", ".jpg", ".jpeg"])# Output imageoutput = gr.Image(label="Original vs. Colorized", interactive=False)# Process and display the combined imageimage_input.change(fn=colorizer_fn, inputs=image_input, outputs=output)# Launch the appif __name__ == "__main__":9 unmodified lines10111213141516176 unmodified lines242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939 unmodified linesfrom deoldify.device_id import DeviceIdfrom deoldify.visualize import *from huggingface_hub import HfApiimport numpy as npimport cv2# Suppress warningswarnings.filterwarnings("ignore", category=UserWarning, message=".*?Your .*? set is empty.*?")6 unmodified linesdevice.set(device=DeviceId.CPU) # Use CPU firstapi = HfApi()snapshot_folder = api.snapshot_download(repo_id=repo_id, repo_type=repo_type)device.set(device=DeviceId.GPU0) # Switch to GPU if available# Load model_colorizer = get_image_colorizer(root_folder=Path(snapshot_folder), artistic=True)def colorizer_fn(input_img):"""Colorize grayscale image and return a slider comparison."""if input_img:# Load the original imageoriginal = Image.open(input_img).convert("RGB")# Colorize the imagecolorized = _colorizer.get_transformed_image(path=input_img,render_factor=40, # Fixed render factorwatermarked=False,post_process=True,)# Resize both images to the same sizecolorized = colorized.resize(original.size)# Convert PIL images to NumPy arrays for mergingoriginal_np = np.array(original)colorized_np = np.array(colorized)# Ensure both images have 3 channelsif len(original_np.shape) == 2:original_np = cv2.cvtColor(original_np, cv2.COLOR_GRAY2RGB)if len(colorized_np.shape) == 2:colorized_np = cv2.cvtColor(colorized_np, cv2.COLOR_GRAY2RGB)# Create a slider effect image (Gradio handles the slider visualization)return original_np, colorized_npelse:return None# ✅ Custom CSS:# - Hides the share button# - Prevents image dragging# - Full-screen output displaycustom_css = """body, .gradio-container { background-color: black !important; color: white !important; }.gr-box { width: 100% !important; }footer {display: none !important;} /* Hide footer */button[title="Share"] { display: none !important; } /* Hides the Share button */img { pointer-events: none !important; } /* Prevents image dragging */"""# Gradio Interface with Sliderwith gr.Blocks(css=custom_css) as demo:gr.Markdown("# Colorizer")# File uploaderimage_input = gr.File(label="Select Image", file_types=[".png", ".jpg", ".jpeg"])# Before/After sliderwith gr.Row():comparison_slider = gr.Image(label="Before / After", tool="editor", interactive=False)# When an image is uploaded → Colorize and show slider comparisonimage_input.change(fn=colorizer_fn, inputs=image_input, outputs=comparison_slider)# Launch the appif __name__ == "__main__":
app.py+32-22