Spaces:
Runtime error
Runtime error
Download models.py from AlexHanxss/lab-xzcspl5a: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/spaces/AlexHanxss/lab-xzcspl5a/resolve/main/models.py
- Command line
-
hf download hf://spaces/AlexHanxss/lab-xzcspl5a/models.py
-
curl -L -o models.py https://huggingface.co/spaces/AlexHanxss/lab-xzcspl5a/resolve/main/models.py
2.94 kB
| """ | |
| Model loading and inference functions for Qwen Image models | |
| """ | |
| import torch | |
| import math | |
| from PIL import Image | |
| from diffusers import ( | |
| QwenImageEditPlusPipeline, | |
| DiffusionPipeline, | |
| FlowMatchEulerDiscreteScheduler | |
| ) | |
| from config import ( | |
| EDIT_MODEL_ID, | |
| IMAGE_MODEL_ID, | |
| SCHEDULER_CONFIG, | |
| TORCH_DTYPE | |
| ) | |
| def load_models(): | |
| """Load both Qwen models""" | |
| print("Loading Qwen-Image-Edit-2509...") | |
| edit_pipeline = QwenImageEditPlusPipeline.from_pretrained( | |
| EDIT_MODEL_ID, | |
| torch_dtype=TORCH_DTYPE | |
| ) | |
| edit_pipeline.to("cuda") | |
| edit_pipeline.set_progress_bar_config(disable=None) | |
| print("Qwen-Image-Edit-2509 loaded successfully") | |
| print("Loading Qwen-Image-Lightning...") | |
| scheduler = FlowMatchEulerDiscreteScheduler.from_config(SCHEDULER_CONFIG) | |
| image_pipeline = DiffusionPipeline.from_pretrained( | |
| IMAGE_MODEL_ID, | |
| scheduler=scheduler, | |
| torch_dtype=TORCH_DTYPE | |
| ).to("cuda") | |
| print("Qwen-Image-Lightning loaded successfully") | |
| return edit_pipeline, image_pipeline | |
| def combine_images(pipeline, image1, image2, prompt, negative_prompt, num_inference_steps, seed): | |
| """ | |
| Combine two images using Qwen-Image-Edit-2509 | |
| Args: | |
| pipeline: QwenImageEditPlusPipeline | |
| image1: First PIL Image | |
| image2: Second PIL Image | |
| prompt: Text prompt describing the combination | |
| negative_prompt: Negative prompt | |
| num_inference_steps: Number of inference steps | |
| seed: Random seed | |
| Returns: | |
| Combined PIL Image | |
| """ | |
| inputs = { | |
| "image": [image1, image2], | |
| "prompt": prompt, | |
| "negative_prompt": negative_prompt, | |
| "generator": torch.manual_seed(seed), | |
| "true_cfg_scale": 4.0, | |
| "num_inference_steps": num_inference_steps, | |
| "guidance_scale": 1.0, | |
| "num_images_per_prompt": 1, | |
| } | |
| with torch.inference_mode(): | |
| output = pipeline(**inputs) | |
| combined_image = output.images[0] | |
| return combined_image | |
| def enhance_image(pipeline, image, prompt, negative_prompt, num_inference_steps, cfg_scale, seed): | |
| """ | |
| Enhance an image using Qwen-Image with prompt | |
| Args: | |
| pipeline: DiffusionPipeline with Qwen-Image | |
| image: Input PIL Image | |
| prompt: Text prompt for enhancement | |
| negative_prompt: Negative prompt | |
| num_inference_steps: Number of inference steps | |
| cfg_scale: CFG scale | |
| seed: Random seed | |
| Returns: | |
| Enhanced PIL Image | |
| """ | |
| # Prepare input for img2img | |
| enhanced_image = pipeline( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt, | |
| image=image, | |
| width=image.width, | |
| height=image.height, | |
| num_inference_steps=num_inference_steps, | |
| true_cfg_scale=cfg_scale, | |
| generator=torch.manual_seed(seed), | |
| ).images[0] | |
| return enhanced_image |