File size: 7,753 Bytes
b22b80e
 
 
 
9c2430d
b22b80e
e4f4963
9c2430d
 
 
 
 
b22b80e
a2139ac
 
 
 
 
b6ec892
3e4f76c
a2139ac
b22b80e
9c2430d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2139ac
 
 
 
 
 
 
 
 
 
 
3e4f76c
95ab993
a2139ac
 
 
 
 
7819338
a2139ac
 
 
 
 
 
 
 
 
 
 
 
 
 
e4f4963
9c2430d
 
 
 
 
 
 
 
 
 
b22b80e
 
9c2430d
e4f4963
9c2430d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b22b80e
9c2430d
b22b80e
9c2430d
 
 
b22b80e
 
9c2430d
b22b80e
 
9c2430d
b22b80e
 
 
9c2430d
 
b22b80e
 
9c2430d
 
 
b22b80e
 
 
 
 
 
9c2430d
b22b80e
9c2430d
 
8acf762
9c2430d
b22b80e
9c2430d
 
 
 
 
 
 
 
 
 
 
 
 
b22b80e
 
 
 
 
9c2430d
b22b80e
9c2430d
b22b80e
9c2430d
b22b80e
9c2430d
 
 
 
b22b80e
9c2430d
b22b80e
9c2430d
 
 
 
 
 
 
 
b22b80e
9c2430d
b22b80e
 
 
9c2430d
b22b80e
 
e4f4963
b22b80e
9c2430d
a2139ac
9c2430d
 
 
 
 
 
 
 
 
 
b22b80e
 
9c2430d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import gradio as gr
import numpy as np
import random

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


# (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_diffusers(
    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=50,
        true_cfg_scale=4.0,
        generator=torch.Generator(device="cuda").manual_seed(42)
    ).images[0]

    #image.save("example.png")

    return image, seed


@spaces.GPU(duration=65)
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("calling with prompt: %s" % prompt)
    rsp = ImageSynthesis.call(api_key=os.environ.get("DASH_API_KEY"),
                            model="qwen-image",
                            prompt=prompt,
                            negative_prompt=negative_prompt,
                            n=1,
                            seed=seed,
                            guidance_scale=guidance_scale,
                            steps=num_inference_steps,
                            size=f'{width}*{height}'
                            ) # support 1664*928, 1472*1140, 1328*1328, 1140*1472, 928*1664
    print('response: %s' % rsp)
    if rsp.status_code == HTTPStatus.OK:
        # 在当前目录下保存图片
        for result in rsp.output.results:
            file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
            with open('./%s' % file_name, 'wb+') as f:
                f.write(requests.get(result.url).content)
            print(f'save image to {file_name}')
    else:
        print('sync_call Failed, status_code: %s, code: %s, message: %s' %
            (rsp.status_code, rsp.code, rsp.message))
    image = Image.open(file_name)
    return image, seed

examples = [
        "A capybara wearing a suit holding a sign that reads Hello World",
        """A young girl wearing school uniform stands in a classroom, writing on a chalkboard. The text "Introducing Qwen-Image, a foundational image generation model that excels in complex text rendering and precise image editing" appears in neat white chalk at the center of the blackboard. Soft natural light filters through windows, casting gentle shadows. The scene is rendered in a realistic photography style with fine details, shallow depth of field, and warm tones. The girl's focused expression and chalk dust in the air add dynamism. Background elements include desks and educational posters, subtly blurred to emphasize the central action. Ultra-detailed 32K resolution, DSLR-quality, soft bokeh effect, documentary-style composition""",
        "Realistic still life photography style: A single, fresh apple resting on a clean, soft-textured surface. The apple is slightly off-center, softly backlit to highlight its natural gloss and subtle color gradients—deep crimson red blending into light golden hues. Fine details such as small blemishes, dew drops, and a few light highlights enhance its lifelike appearance. A shallow depth of field gently blurs the neutral background, drawing full attention to the apple. Hyper-detailed 8K resolution, studio lighting, photorealistic render, emphasizing texture and form."
]

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('<div style="text-align: center;"><a href="https://huggingface.co/Qwen/Qwen-Image"><img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png" width="400"/></a></div>')
        gr.Markdown('<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png" alt="your_alt_text" width="400" style="display: block; margin: 0 auto;">')
        gr.Markdown("[Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series. Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image) to run locally with ComfyUI or diffusers.")
        with gr.Row():
            prompt = gr.Text(
                label="Prompt",
                show_label=False,
                placeholder="Enter your prompt",
                container=False,
                
            )
            run_button = gr.Button("Run", scale=0, variant="primary")

        result = gr.Image(label="Result", show_label=False)

        with gr.Accordion("Advanced Settings", open=False):
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=False,
            )
            negative_prompt = gr.Text(
                label="Negative prompt",
                max_lines=1,
                placeholder="Enter a negative prompt",
                visible=False,
            )

            seed = gr.Slider(
                label="Seed",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0,
            )

            randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

            with gr.Row():
                aspect_ratio = gr.Radio(
                    label="Aspect ratio(width:height)",
                    choices=["1:1", "16:9", "9:16", "4:3", "3:4"],
                    value="16:9",
                )

            with gr.Row():
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.0,
                    maximum=7.5,
                    step=0.1,
                    value=4.0,
                )

                num_inference_steps = gr.Slider(
                    label="Number of inference steps",
                    minimum=1,
                    maximum=50,
                    step=1,
                    value=50, 
                )

        gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False, cache_mode="lazy")
    gr.on(
        triggers=[run_button.click, prompt.submit],
        fn=infer_diffusers,
        inputs=[
            prompt,
            negative_prompt,
            seed,
            randomize_seed,
            aspect_ratio,
            guidance_scale,
            num_inference_steps,
        ],
        outputs=[result, seed],
    )

if __name__ == "__main__":
    demo.launch()