File size: 2,232 Bytes
2d8f0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d236ff
2d8f0dd
 
 
 
 
 
 
 
 
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
import torch

torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True

import os
import gradio as gr
import spaces
from diffusers import FlowMatchEulerDiscreteScheduler
from lakonlab.ui.gradio.create_text_to_img import create_interface_text_to_img
from lakonlab.pipelines.piqwen_pipeline import PiQwenImagePipeline

from huggingface_hub import login
login(token=os.getenv('HF_TOKEN'))


DEFAULT_PROMPT = ('Photo of a coffee shop entrance featuring a chalkboard sign reading "ฯ€-Qwen Coffee ๐Ÿ˜Š $2 per cup," '
                  'with a neon light beside it displaying "ฯ€-้€šไน‰ๅƒ้—ฎ". Next to it hangs a poster showing a beautiful '
                  'Chinese woman, and beneath the poster is written "eโ‰ˆ2.71828-18284-59045-23536-02874-71352".')


pipe = PiQwenImagePipeline.from_pretrained(
    'Qwen/Qwen-Image',
    torch_dtype=torch.bfloat16)
pipe.load_piflow_adapter(
    'Lakonik/pi-Qwen-Image',
    subfolder='gmqwen_k8_piid_4step',
    target_module_name='transformer')
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(  # use fixed shift=3.2
    pipe.scheduler.config, shift=3.2, shift_terminal=None, use_dynamic_shifting=False)
pipe = pipe.to('cuda')


@spaces.GPU
def generate(seed, prompt, width, height, steps):
    return pipe(
        prompt=prompt,
        width=width,
        height=height,
        num_inference_steps=steps,
        generator=torch.Generator().manual_seed(seed),
    ).images[0]


with gr.Blocks(analytics_enabled=False,
               title='pi-Qwen Demo',
               css='lakonlab/ui/gradio/style.css'
               ) as demo:

    md_txt = '# pi-Qwen Demo\n\n' \
             'Official demo of the paper [pi-Flow: Policy-Based Few-Step Generation via Imitation Distillation](https://arxiv.org/abs/2510.14974). ' \
             '**Base model:** [Qwen-Image](https://huggingface.co/Qwen/Qwen-Image). **Fast policy:** GMFlow. **Code:** [https://github.com/Lakonik/piFlow](https://github.com/Lakonik/piFlow).'
    gr.Markdown(md_txt)

    create_interface_text_to_img(
        generate,
        prompt=DEFAULT_PROMPT,
        steps=4, guidance_scale=None,
        args=['last_seed', 'prompt', 'width', 'height', 'steps'])
    demo.queue().launch()