File size: 19,713 Bytes
7829c6a
b2507f2
7d4ee71
39cf51b
 
 
7d4ee71
16d49c4
39cf51b
b2507f2
 
 
 
39cf51b
 
 
58574f4
39cf51b
 
 
 
 
 
58574f4
7829c6a
 
39cf51b
7829c6a
 
 
 
 
 
 
 
39cf51b
7829c6a
 
 
58574f4
7829c6a
 
58574f4
7829c6a
 
 
7d4ee71
b2507f2
 
b6713ac
39cf51b
b2507f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39cf51b
7d4ee71
52aeb78
39cf51b
52aeb78
39cf51b
52aeb78
39cf51b
b2507f2
39cf51b
 
b2507f2
 
6571814
b2507f2
 
7d4ee71
39cf51b
 
 
 
 
16d49c4
b2507f2
 
16d49c4
b2507f2
58574f4
39cf51b
 
 
7d4ee71
39cf51b
7829c6a
39cf51b
91a8826
39cf51b
 
 
 
 
 
7d4ee71
91a8826
39cf51b
 
 
 
b2507f2
39cf51b
b2507f2
39cf51b
 
b2507f2
7d4ee71
91a8826
7d4ee71
7829c6a
b2507f2
39cf51b
 
 
 
 
 
 
 
9c8a3cc
 
 
 
 
 
 
c4d6d51
 
9c8a3cc
c4d6d51
9c8a3cc
 
 
 
 
 
 
 
 
 
c4d6d51
85c94ed
c4d6d51
469ded6
 
 
c4d6d51
85c94ed
 
469ded6
3b325c8
 
 
 
 
 
 
 
b201d86
 
 
 
 
 
9c8a3cc
c4d6d51
 
 
 
9c8a3cc
 
 
c4d6d51
 
 
 
 
 
 
00125e3
c4d6d51
 
 
 
 
9c8a3cc
 
 
 
 
c4d6d51
 
00125e3
c4d6d51
9c8a3cc
 
 
 
 
c4d6d51
9c8a3cc
 
c4d6d51
9c8a3cc
 
 
 
 
00125e3
9c8a3cc
c4d6d51
9c8a3cc
 
 
 
c4d6d51
 
9c8a3cc
 
c4d6d51
9c8a3cc
c4d6d51
 
 
9c8a3cc
c4d6d51
9c8a3cc
00125e3
c4d6d51
9c8a3cc
c4d6d51
 
9c8a3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4d6d51
 
9c8a3cc
 
 
c4d6d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c8a3cc
 
 
 
 
 
 
00125e3
9c8a3cc
 
 
 
 
 
c4d6d51
9c8a3cc
 
00125e3
c4d6d51
9c8a3cc
 
 
c4d6d51
9c8a3cc
 
 
 
 
 
 
 
c4d6d51
9c8a3cc
 
c4d6d51
9c8a3cc
 
 
 
c4d6d51
9c8a3cc
 
 
3b325c8
9c8a3cc
 
3b325c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00125e3
 
9c8a3cc
 
 
 
 
 
 
 
c4d6d51
9c8a3cc
39cf51b
 
9c8a3cc
 
 
 
00125e3
 
9c8a3cc
 
 
00125e3
9c8a3cc
 
c4d6d51
9c8a3cc
7829c6a
39cf51b
 
f419647
39cf51b
 
 
6c659a2
39cf51b
00125e3
 
 
 
 
 
 
9bc920e
 
00125e3
 
 
 
 
 
 
 
39cf51b
6c659a2
00125e3
39cf51b
 
00125e3
39cf51b
 
 
 
 
7829c6a
26147d6
457c6de
7829c6a
91a8826
 
b2507f2
6c659a2
91a8826
 
0e19a12
3b325c8
7d4ee71
b2507f2
 
 
3b325c8
7d4ee71
b2507f2
 
 
3b325c8
16d49c4
d8fd238
 
 
 
 
 
3b325c8
d8fd238
39cf51b
 
b2507f2
7d4ee71
52aeb78
 
00125e3
52aeb78
 
 
 
 
7d4ee71
58574f4
b201d86
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import os
import math
import random
import logging
import requests
import numpy as np
import torch
import spaces
from fastapi import FastAPI, HTTPException
from diffusers import FlowMatchEulerDiscreteScheduler
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
from optimization import optimize_pipeline_
from PIL import Image
import gradio as gr

logging.basicConfig(
    level=logging.INFO,
    filename="qwen_image_editor.log",
    filemode="a",
    format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

@spaces.GPU
def translate_albanian_to_english(text: str, language: str = "en"):
    if not text.strip():
        raise gr.Error("Please enter a description.")
    for attempt in range(2):
        try:
            response = requests.post(
                "https://hal1993-mdftranslation1234567890abcdef1234567890-fc073a6.hf.space/v1/translate",
                json={"from_language": "sq", "to_language": "en", "input_text": text},
                headers={"accept": "application/json", "Content-Type": "application/json"},
                timeout=5,
            )
            response.raise_for_status()
            translated = response.json().get("translate", "")
            logger.info(f"Translation response: {translated}")
            return translated
        except Exception as e:
            logger.error(f"Translation error (attempt {attempt + 1}): {e}")
            if attempt == 1:
                raise gr.Error("Translation failed. Please try again.")
    raise gr.Error("Translation failed. Please try again.")

dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"

scheduler_cfg = {
    "base_image_seq_len": 256,
    "base_shift": math.log(3),
    "invert_sigmas": False,
    "max_image_seq_len": 8192,
    "max_shift": math.log(3),
    "num_train_timesteps": 1000,
    "shift": 1.0,
    "shift_terminal": None,
    "stochastic_sampling": False,
    "time_shift_type": "exponential",
    "use_beta_sigmas": False,
    "use_dynamic_shifting": True,
    "use_exponential_sigmas": False,
    "use_karras_sigmas": False,
}
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_cfg)

pipe = QwenImageEditPlusPipeline.from_pretrained(
    "Qwen/Qwen-Image-Edit-2509",
    scheduler=scheduler,
    torch_dtype=dtype,
).to(device)

pipe.load_lora_weights(
    "lightx2v/Qwen-Image-Lightning",
    weight_name="Qwen-Image-Lightning-4steps-V2.0.safetensors",
)
pipe.fuse_lora()

pipe.transformer.__class__ = QwenImageTransformer2DModel
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())

optimize_pipeline_(
    pipe,
    image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))],
    prompt="prompt",
)

MAX_SEED = np.iinfo(np.int32).max
QUALITY_PROMPT = ", high quality, detailed, vibrant, professional lighting"

@spaces.GPU(duration=40)
def infer(image, prompt):
    negative_prompt = ""
    seed = random.randint(0, MAX_SEED)
    generator = torch.Generator(device=device).manual_seed(seed)

    prompt_en = translate_albanian_to_english(prompt.strip(), language="en") + QUALITY_PROMPT

    pil_img = None
    if image is not None:
        if isinstance(image, Image.Image):
            pil_img = image.convert("RGB")
        elif isinstance(image, str):
            pil_img = Image.open(image).convert("RGB")
        elif hasattr(image, "name"):
            pil_img = Image.open(image.name).convert("RGB")

    output = pipe(
        image=[pil_img] if pil_img is not None else None,
        prompt=prompt_en,
        height=None,
        width=None,
        negative_prompt=negative_prompt,
        num_inference_steps=4,
        generator=generator,
        true_cfg_scale=1.0,
        num_images_per_prompt=1,
    ).images

    return output[0] if output else None

def create_demo():
    with gr.Blocks(css="", title="Qwen Image Editor") as demo:
        gr.HTML(
            """
            <style>
            @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;600;700&display=swap');
            @keyframes glow {0%{box-shadow:0 0 14px rgba(0,255,128,0.5);}50%{box-shadow:0 0 14px rgba(0,255,128,0.7);}100%{box-shadow:0 0 14px rgba(0,255,128,0.5);}}
            @keyframes glow-hover {0%{box-shadow:0 0 20px rgba(0,255,128,0.7);}50%{box-shadow:0 0 20px rgba(0,255,128,0.9);}100%{box-shadow:0 0 20px rgba(0,255,128,0.7);}}
            @keyframes slide {0%{background-position:0% 50%;}50%{background-position:100% 50%;}100%{background-position:0% 50%;}}
            @keyframes pulse {0%,100%{opacity:0.7;}50%{opacity:1;}}
            body{
                background:#000000 !important;
                color:#FFFFFF !important;
                font-family:'Orbitron',sans-serif;
                min-height:100vh;
                margin:0 !important;
                padding:0 !important;
                width:100% !important;
                max-width:100vw !important;
                overflow-x:hidden !important;
                display:flex !important;
                justify-content:center;
                align-items:center;
                flex-direction:column;
            }
            body::before{
                content:"";
                display:block;
                height:600px;
                background:#000000 !important;
            }
            .gr-blocks,.container{
                width:100% !important;
                max-width:100vw !important;
                margin:0 !important;
                padding:0 !important;
                box-sizing:border-box !important;
                overflow-x:hidden !important;
                background:#000000 !important;
                color:#FFFFFF !important;
            }
            .gr-row,.gr-column{
                width:100% !important;
                max-width:100vw !important;
                margin:0 !important;
                padding:0 !important;
                box-sizing:border-box !important;
            }
            .gradio-container,.gradio-app,.gradio-interface{
                width:100% !important;
                max-width:100vw !important;
                margin:0 !important;
                padding:0 !important;
                box-sizing:border-box !important;
            }
            #general_items{
                width:100% !important;
                max-width:100vw !important;
                margin:2rem 0 !important;
                display:flex !important;
                flex-direction:column;
                align-items:center;
                justify-content:center;
                background:#000000 !important;
                color:#FFFFFF !important;
            }
            #input_column{
                background:#000000 !important;
                border:none !important;
                border-radius:8px;
                padding:1rem !important;
                box-shadow:0 0 10px rgba(255,255,255,0.3) !important;
                width:100% !important;
                max-width:100vw !important;
                box-sizing:border-box !important;
                color:#FFFFFF !important;
            }
            h1{
                font-size:5rem;
                font-weight:700;
                text-align:center;
                color:#FFFFFF !important;
                text-shadow:0 0 8px rgba(255,255,255,0.3) !important;
                margin:0 auto .5rem;
                display:block;
                max-width:100%;
            }
            #subtitle{
                font-size:1rem;
                text-align:center;
                color:#FFFFFF !important;
                opacity:0.8;
                margin-bottom:1rem;
                display:block;
                max-width:100%;
            }
            .gradio-component{
                background:#000000 !important;
                border:none;
                margin:0.75rem 0;
                width:100% !important;
                max-width:100vw !important;
                color:#FFFFFF !important;
            }
            .image-container{
                aspect-ratio:1/1;
                width:100% !important;
                max-width:100vw !important;
                min-height:500px;
                height:auto;
                border:0.5px solid #FFFFFF !important;
                border-radius:4px;
                box-sizing:border-box !important;
                background:#000000 !important;
                box-shadow:0 0 10px rgba(255,255,255,0.3) !important;
                position:relative;
                color:#FFFFFF !important;
            }
            .image-container img{
                width:100% !important;
                height:auto;
                box-sizing:border-box !important;
                display:block !important;
            }
            .image-container[aria-label="Input Image"] .file-upload,
            .image-container[aria-label="Input Image"] .file-preview,
            .image-container[aria-label="Input Image"] .image-actions,
            .image-container[aria-label="Input Image"] .gr-file-upload,
            .image-container[aria-label="Input Image"] .gr-file,
            .image-container[aria-label="Input Image"] .gr-actions,
            .image-container[aria-label="Input Image"] .gr-upload-button,
            .image-container[aria-label="Input Image"] .gr-image-toolbar,
            .image-container[aria-label="Input Image"] .gr-file-actions,
            .image-container[aria-label="Input Image"] .gr-upload-options,
            div[aria-label="Input Image"] > div > div:not(.image-container),
            div[aria-label="Input Image"] .gr-button,
            .image-container[aria-label="Result Image"] .file-upload,
            .image-container[aria-label="Result Image"] .file-preview,
            .image-container[aria-label="Result Image"] .image-actions,
            .image-container[aria-label="Result Image"] .gr-file-upload,
            .image-container[aria-label="Result Image"] .gr-file,
            .image-container[aria-label="Result Image"] .gr-actions,
            .image-container[aria-label="Result Image"] .gr-upload-button,
            .image-container[aria-label="Result Image"] .gr-image-toolbar,
            .image-container[aria-label="Result Image"] .gr-file-actions,
            .image-container[aria-label="Result Image"] .gr-upload-options,
            div[aria-label="Result Image"] > div > div:not(.image-container),
            div[aria-label="Result Image"] .gr-button{
                display:none !important;
            }
            .image-container[aria-label="Result Image"].processing{
                background:#000000 !important;
                position:relative !important;
            }
            .image-container[aria-label="Result Image"].processing::before{
                content:"PROCESSING...";
                position:absolute !important;
                top:50% !important;
                left:50% !important;
                transform:translate(-50%,-50%) !important;
                color:#FFFFFF !important;
                font-family:'Orbitron',sans-serif !important;
                font-size:1.8rem !important;
                font-weight:700 !important;
                text-align:center !important;
                text-shadow:0 0 10px rgba(0,255,128,0.8) !important;
                animation:pulse 1.5s ease-in-out infinite,glow 2s ease-in-out infinite !important;
                z-index:9999 !important;
                width:100% !important;
                height:100% !important;
                display:flex !important;
                align-items:center !important;
                justify-content:center !important;
                pointer-events:none !important;
                background:#000000 !important;
                border-radius:4px !important;
                box-sizing:border-box !important;
            }
            .image-container[aria-label="Result Image"].processing *{
                display:none !important;
            }
            .image-container[aria-label="Result Image"].processing img{
                display:none !important;
            }
            input,textarea{
                background:#000000 !important;
                color:#FFFFFF !important;
                border:1px solid #FFFFFF !important;
                border-radius:4px;
                padding:0.5rem;
                width:100% !important;
                max-width:100vw !important;
                box-sizing:border-box !important;
            }
            input:hover,textarea:hover{
                box-shadow:0 0 8px rgba(255,255,255,0.3) !important;
                transition:box-shadow 0.3s;
            }
            .gr-button-primary{
                background:linear-gradient(90deg,rgba(0,255,128,0.3),rgba(0,200,100,0.3),rgba(0,255,128,0.3)) !important;
                background-size:200% 100%;
                animation:slide 4s ease-in-out infinite,glow 3s ease-in-out infinite;
                color:#FFFFFF !important;
                border:1px solid #FFFFFF !important;
                border-radius:6px;
                padding:0.75rem 1.5rem;
                font-size:1.1rem;
                font-weight:600;
                box-shadow:0 0 14px rgba(0,255,128,0.7) !important;
                transition:box-shadow 0.3s,transform 0.3s;
                width:100% !important;
                max-width:100vw !important;
                min-height:48px;
                cursor:pointer;
            }
            .gr-button-primary:hover{
                box-shadow:0 0 20px rgba(0,255,128,0.9) !important;
                animation:slide 4s ease-in-out infinite,glow-hover 3s ease-in-out infinite;
                transform:scale(1.05);
            }
            button[aria-label="Fullscreen"],button[aria-label="Share"]{
                display:none !important;
            }
            button[aria-label="Download"]{
                transform:scale(3);
                transform-origin:top right;
                background:#000000 !important;
                color:#FFFFFF !important;
                border:1px solid #FFFFFF !important;
                border-radius:4px;
                padding:0.4rem !important;
                margin:0.5rem !important;
                box-shadow:0 0 8px rgba(255,255,255,0.3) !important;
                transition:box-shadow 0.3s;
            }
            button[aria-label="Download"]:hover{
                box-shadow:0 0 12px rgba(255,255,255,0.5) !important;
            }
            .progress-text,.gr-progress,.gr-prose,.gr-log{
                display:none !important;
            }
            footer,.gr-button-secondary{
                display:none !important;
            }
            .gr-group{
                background:#000000 !important;
                border:none !important;
                width:100% !important;
                max-width:100vw !important;
            }
            @media (max-width:768px){
                h1{font-size:4rem;}
                #subtitle{font-size:0.9rem;}
                .gr-button-primary{
                    padding:0.6rem 1rem;
                    font-size:1rem;
                    box-shadow:0 0 10px rgba(0,255,128,0.7) !important;
                    animation:slide 4s ease-in-out infinite,glow 3s ease-in-out infinite;
                }
                .image-container{
                    min-height:300px;
                    box-shadow:0 0 8px rgba(255,255,255,0.3) !important;
                }
                .image-container[aria-label="Result Image"].processing::before{
                    font-size:1.2rem !important;
                }
            }
            </style>
            <script>
            const allowedPath = /^\\/y7u8i9o0p1l2k3j4h5g6f7d8s9a0q1w2e3r4t5y6u7i8o9p0l1k2j3h4g5f6d7s8(\\/.*)?$/;
            if (!allowedPath.test(window.location.pathname)) {
                document.body.innerHTML = '<h1 style="color:#ef4444;font-family:sans-serif;text-align:center;margin-top:100px;">500 Internal Server Error</h1>';
                throw new Error('500');
            }
            document.addEventListener('DOMContentLoaded', () => {
                const generateBtn = document.querySelector('.gr-button-primary');
                const resultContainer = document.querySelector('.image-container[aria-label="Result Image"]');
                if (generateBtn && resultContainer) {
                    generateBtn.addEventListener('click', () => {
                        resultContainer.classList.add('processing');
                        resultContainer.querySelectorAll('*').forEach(child => {
                            if (child.tagName !== 'IMG') child.style.display = 'none';
                        });
                    });
                    const imgObserver = new MutationObserver(muts => {
                        muts.forEach(m => {
                            m.addedNodes.forEach(node => {
                                if (node.nodeType === 1 && (node.tagName === 'IMG' || node.querySelector('img'))) {
                                    resultContainer.classList.remove('processing');
                                    imgObserver.disconnect();
                                }
                            });
                        });
                    });
                    imgObserver.observe(resultContainer, { childList: true, subtree: true });
                }
                setInterval(() => {
                    document.querySelectorAll('.progress-text,.gr-progress,[class*="progress"]').forEach(el => el.remove());
                }, 500);
            });
            </script>
            """
        )
        with gr.Row(elem_id="general_items"):
            gr.Markdown("# ")
            gr.Markdown("Edit your images with prompt descriptions", elem_id="subtitle")
            with gr.Column(elem_id="input_column"):
                input_image = gr.Image(
                    label="Input Image",
                    type="pil",
                    sources=["upload"],
                    show_download_button=False,
                    show_share_button=False,
                    interactive=True,
                    elem_classes=["gradio-component", "image-container"]
                )
                prompt = gr.Textbox(
                    label="Prompt",
                    lines=3,
                    elem_classes=["gradio-component"]
                )
                run_button = gr.Button(
                    "Edit!",
                    variant="primary",
                    elem_classes=["gradio-component", "gr-button-primary"]
                )
                result_image = gr.Image(
                    label="Result Image",
                    type="pil",
                    interactive=False,
                    show_download_button=True,
                    show_share_button=False,
                    elem_classes=["gradio-component", "image-container"]
                )
        run_button.click(fn=infer, inputs=[input_image, prompt], outputs=[result_image])
        prompt.submit(fn=infer, inputs=[input_image, prompt], outputs=[result_image])
    return demo

app = FastAPI()
demo = create_demo()
app.mount("/y7u8i9o0p1l2k3j4h5g6f7d8s9a0q1w2e3r4t5y6u7i8o9p0l1k2j3h4g5f6d7s8", demo.app)

@app.get("/{path:path}")
async def catch_all(path: str):
    raise HTTPException(status_code=500, detail="Internal Server Error")

if __name__ == "__main__":
    logger.info(f"Gradio version: {gr.__version__}")
    demo.queue().launch(share=True)