GoHugo commited on
Commit
7abde8c
·
verified ·
1 Parent(s): c31002d

update app

Browse files
Files changed (1) hide show
  1. app.py +7 -100
app.py CHANGED
@@ -1,97 +1,3 @@
1
- import os
2
-
3
- os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
4
-
5
- import spaces
6
- import random
7
- import torch
8
- import gradio as gr
9
- from PIL import Image
10
- from diffusers import QwenImage21Pipeline
11
-
12
- MODEL_ID = os.environ.get("QWEN_IMAGE_MODEL", "Qwen/Qwen-Image-2.1")
13
- LORA_REPO = os.environ.get(
14
- "UNCENSORED_LORA_REPO", "abenzerps/Qwen-Image-2.1-Uncensored-GGUF"
15
- )
16
- LORA_WEIGHT = os.environ.get(
17
- LORA_REPO = "abenzerps/Qwen-Image-2.1-Uncensored-Quantized"
18
- LORA_WEIGHT = "qwen-image-2.1-uncensored-lora.safetensors"
19
-
20
- pipe.load_lora_weights(
21
- LORA_REPO,
22
- weight_name=LORA_WEIGHT
23
- )
24
- )
25
-
26
- SIZE_PRESETS = {
27
- "1024x1024 (1:1)": (1024, 1024),
28
- "1344x768 (16:9)": (1344, 768),
29
- "768x1344 (9:16)": (768, 1344),
30
- "1184x864 (4:3)": (1184, 864),
31
- "864x1184 (3:4)": (864, 1184),
32
- }
33
-
34
- pipe = QwenImage21Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
35
- pipe.load_lora_weights(LORA_REPO, weight_name=LORA_WEIGHT)
36
- pipe.to("cuda")
37
- pipe.vae.enable_tiling(
38
- tile_sample_min_height=1024,
39
- tile_sample_min_width=1024,
40
- tile_sample_stride_height=768,
41
- tile_sample_stride_width=768,
42
- )
43
-
44
-
45
- def _seed(seed: int, randomize_seed: bool) -> int:
46
- if randomize_seed or seed is None or int(seed) < 0:
47
- return random.randint(0, 2**32 - 1)
48
- return int(seed)
49
-
50
-
51
- def _set_lora(scale: float) -> None:
52
- try:
53
- pipe.set_adapters(["default"], adapter_weights=[float(scale)])
54
- except Exception:
55
- pass
56
-
57
-
58
- def _as_pil(img) -> Image.Image:
59
- if img is None:
60
- raise gr.Error("Upload an image.")
61
- if isinstance(img, Image.Image):
62
- return img.convert("RGB")
63
- return Image.fromarray(img).convert("RGB")
64
-
65
-
66
- @spaces.GPU(duration=120)
67
- def generate(
68
- prompt: str,
69
- size: str,
70
- steps: int,
71
- seed: int,
72
- randomize_seed: bool,
73
- lora_scale: float,
74
- ) -> tuple:
75
- """Text-to-image with Qwen-Image-2.1 + uncensored LoRA."""
76
- if not prompt or not prompt.strip():
77
- raise gr.Error("Enter a prompt.")
78
- seed = _seed(seed, randomize_seed)
79
- width, height = SIZE_PRESETS.get(size, (1024, 1024))
80
- generator = torch.Generator("cuda").manual_seed(seed)
81
- _set_lora(lora_scale)
82
- image = pipe(
83
- prompt=prompt.strip(),
84
- width=width,
85
- height=height,
86
- num_inference_steps=int(steps),
87
- true_cfg_scale=1.0,
88
- generator=generator,
89
- ).images[0]
90
- return image, seed
91
-
92
-
93
- @spaces.GPU(duration=120)
94
- def edit(
95
  prompt: str,
96
  input_image,
97
  extra_images: list | None,
@@ -114,7 +20,7 @@ def edit(
114
  generator = torch.Generator("cuda").manual_seed(seed)
115
  _set_lora(lora_scale)
116
  w, h = images[0].size
117
- # keep ~1K area so ZeroGPU large fits
118
  scale = (1024 * 1024 / max(w * h, 1)) ** 0.5
119
  width = max(64, int(w * scale) // 32 * 32)
120
  height = max(64, int(h * scale) // 32 * 32)
@@ -133,12 +39,13 @@ def edit(
133
  with gr.Blocks(title="Qwen-Image-2.1 Uncensored") as demo:
134
  gr.Markdown(
135
  """
136
- # Qwen-Image-2.1 Uncensored — generate & edit
 
 
137
 
138
- [abenzerps/Qwen-Image-2.1-Uncensored-GGUF](https://huggingface.co/abenzerps/Qwen-Image-2.1-Uncensored-GGUF)
139
- via Diffusers + uncensored LoRA (not Comfy GGUF).
140
 
141
- **Generate** = text-to-image. **Edit** = 1–10 refs (`image=` on the same pipeline).
142
 
143
  **18+.** No safety filter.
144
  """
@@ -163,7 +70,7 @@ via Diffusers + uncensored LoRA (not Comfy GGUF).
163
  g_out_seed = gr.Number(label="Used seed", precision=0)
164
  gr.Examples(
165
  examples=[
166
- ["A neon shop sign that reads \"QWEN IMAGE 2.1\", rainy night"],
167
  ["Oil painting of a red fox in snow, soft morning light"],
168
  ],
169
  inputs=[g_prompt],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  prompt: str,
2
  input_image,
3
  extra_images: list | None,
 
20
  generator = torch.Generator("cuda").manual_seed(seed)
21
  _set_lora(lora_scale)
22
  w, h = images[0].size
23
+ # Keep the image area near 1 megapixel to reduce ZeroGPU memory use.
24
  scale = (1024 * 1024 / max(w * h, 1)) ** 0.5
25
  width = max(64, int(w * scale) // 32 * 32)
26
  height = max(64, int(h * scale) // 32 * 32)
 
39
  with gr.Blocks(title="Qwen-Image-2.1 Uncensored") as demo:
40
  gr.Markdown(
41
  """
42
+ # Qwen-Image-2.1 Uncensored: generate and edit
43
+
44
+ [abenzerps/Qwen-Image-2.1-Uncensored-Quantized](https://huggingface.co/abenzerps/Qwen-Image-2.1-Uncensored-Quantized)
45
 
46
+ Uses the Diffusers pipeline with the uncensored LoRA adapter.
 
47
 
48
+ **Generate** creates an image from text. **Edit** accepts 1–10 reference images.
49
 
50
  **18+.** No safety filter.
51
  """
 
70
  g_out_seed = gr.Number(label="Used seed", precision=0)
71
  gr.Examples(
72
  examples=[
73
+ ['A neon shop sign that reads "QWEN IMAGE 2.1", rainy night'],
74
  ["Oil painting of a red fox in snow, soft morning light"],
75
  ],
76
  inputs=[g_prompt],