import gradio as gr import numpy as np import random import json from PIL import Image import spaces from http import HTTPStatus from urllib.parse import urlparse, unquote from pathlib import PurePosixPath import requests import os from diffusers import DiffusionPipeline import torch model_name = "Qwen/Qwen-Image" pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16) pipe.to('cuda') MAX_SEED = np.iinfo(np.int32).max #MAX_IMAGE_SIZE = 1440 examples = json.loads(open("examples.json").read()) # (1664, 928), (1472, 1140), (1328, 1328) def get_image_size(aspect_ratio): if aspect_ratio == "1:1": return 1328, 1328 elif aspect_ratio == "16:9": return 1664, 928 elif aspect_ratio == "9:16": return 928, 1664 elif aspect_ratio == "4:3": return 1472, 1140 elif aspect_ratio == "3:4": return 1140, 1472 else: return 1328, 1328 @spaces.GPU(duration=60) def infer( prompt, negative_prompt=" ", seed=42, randomize_seed=False, aspect_ratio="16:9", guidance_scale=4, num_inference_steps=50, progress=gr.Progress(track_tqdm=True), ): if randomize_seed: seed = random.randint(0, MAX_SEED) width, height = get_image_size(aspect_ratio) print("Generating for prompt:", prompt) image = pipe( prompt=prompt, negative_prompt=negative_prompt, width=width, height=height, num_inference_steps=num_inference_steps, true_cfg_scale=guidance_scale, generator=torch.Generator(device="cuda").manual_seed(seed) ).images[0] #image.save("example.png") return image, seed css = """ #col-container { margin: 0 auto; max-width: 1024px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): # gr.Markdown('
') gr.Markdown('