import os os.system("pip freeze") import spaces import tempfile import shutil import gradio as gr import torch as torch from gradio_dualvision import DualVisionApp from huggingface_hub import login from PIL import Image from windowseat_inference import load_network, run_inference uri_base = "Qwen/Qwen-Image-Edit-2509" uri_lora = "huawei-bayerlab/windowseat-reflection-removal-v1-0" if "HF_TOKEN_LOGIN" in os.environ: login(token=os.environ["HF_TOKEN_LOGIN"]) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32 vae, transformer, embeds_dict, processing_resolution = load_network(uri_base, uri_lora, device) # # As of transformers==4.57.1 , xformers is not supported in QwenImageTransformer2DModel # try: # transformer.enable_xformers_memory_efficient_attention() # print("xformers enabled") # except: # print("xformers not enabled") class WindowSeatApp(DualVisionApp): DEFAULT_SEED = 2025 def make_header(self): gr.Markdown( """ ## WindowSeat Reflection Removal """ ) with gr.Row(elem_classes="remove-elements"): gr.Markdown( f"""
Upload a photo or pick an example below to remove reflections, wait for the result, then explore it with the slider. If a quota limit appears, duplicate the space to continue.
""" ) def build_user_components(self): return {} def process(self, image_in: Image.Image, **kwargs): input_temp_dir = tempfile.mkdtemp() output_temp_dir = tempfile.mkdtemp() try: input_image_path = os.path.join(input_temp_dir, "image.png") image_in.save(input_image_path) run_inference( vae, transformer, embeds_dict, processing_resolution, input_temp_dir, output_temp_dir, use_short_edge_tile=True, save_comparison=False, save_alternating=False, ) output_image_path = os.path.join(output_temp_dir, "image_windowseat_output.png") result_image = Image.open(output_image_path) result_image.load() out_modalities = { "Result": result_image, } out_settings = {} return out_modalities, out_settings finally: if os.path.exists(input_temp_dir): shutil.rmtree(input_temp_dir) if os.path.exists(output_temp_dir): shutil.rmtree(output_temp_dir) with WindowSeatApp( title="WindowSeat Reflection Removal", examples_path="example_images", examples_per_page=12, right_selector_visible=False, advanced_settings_visible=False, squeeze_canvas=True, spaces_zero_gpu_enabled=True, ) as demo: demo.queue( api_open=False, ).launch( server_name="0.0.0.0", server_port=7860, ssr_mode=False, )