#!/usr/bin/env python3 """Run the UniTele Qwen SR adapter on one telephoto image.""" from __future__ import annotations import argparse from pathlib import Path import torch from huggingface_hub import hf_hub_download from PIL import Image from diffsynth.pipelines.qwen_image import ModelConfig, QwenImagePipeline PHONE_NAMES = { "huawei": "Huawei", "iphone": "iPhone 17 Pro Max", "vivo": "vivo X300 Ultra", } def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--input", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--phone", choices=PHONE_NAMES, required=True) parser.add_argument("--lora-path", type=Path) parser.add_argument("--device", default="cuda") parser.add_argument("--steps", type=int, default=40) parser.add_argument("--cfg-scale", type=float, default=4.0) parser.add_argument("--seed", type=int, default=123) args = parser.parse_args() lora_path = args.lora_path or Path( hf_hub_download("AustinZYC/UniTele", "unitele.safetensors") ) with Image.open(args.input) as source: edit_image = source.convert("RGB") width, height = (768, 1024) if edit_image.height > edit_image.width else (1024, 768) pipe = QwenImagePipeline.from_pretrained( torch_dtype=torch.bfloat16, device=args.device, model_configs=[ ModelConfig( model_id="Qwen/Qwen-Image-Edit-2511", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors", ), ModelConfig( model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", ), ModelConfig( model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors", ), ], processor_config=ModelConfig( model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/" ), ) pipe.load_lora(pipe.dit, str(lora_path)) prompt = ( f"Restore this low quality real {PHONE_NAMES[args.phone]} telephoto image " "into the aligned high quality reference image. Preserve the scene " "layout and recover natural sharp details." ) with torch.inference_mode(): result = pipe( prompt, edit_image=edit_image, seed=args.seed, num_inference_steps=args.steps, height=height, width=width, cfg_scale=args.cfg_scale, edit_image_auto_resize=True, zero_cond_t=True, ) args.output.parent.mkdir(parents=True, exist_ok=True) result.save(args.output) print(args.output) if __name__ == "__main__": main()