prithivMLmods commited on
Commit
e4a57c6
·
verified ·
1 Parent(s): 90c06dc

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -280
app.py DELETED
@@ -1,280 +0,0 @@
1
- import os
2
- import gradio as gr
3
- import numpy as np
4
- import spaces
5
- import torch
6
- import random
7
- from PIL import Image
8
- from typing import Iterable
9
-
10
- from gradio.themes import Soft
11
- from gradio.themes.utils import colors, fonts, sizes
12
-
13
- colors.steel_blue = colors.Color(
14
- name="steel_blue",
15
- c50="#EBF3F8",
16
- c100="#D3E5F0",
17
- c200="#A8CCE1",
18
- c300="#7DB3D2",
19
- c400="#529AC3",
20
- c500="#4682B4",
21
- c600="#3E72A0",
22
- c700="#36638C",
23
- c800="#2E5378",
24
- c900="#264364",
25
- c950="#1E3450",
26
- )
27
-
28
- class SteelBlueTheme(Soft):
29
- def __init__(
30
- self,
31
- *,
32
- primary_hue: colors.Color | str = colors.gray,
33
- secondary_hue: colors.Color | str = colors.steel_blue,
34
- neutral_hue: colors.Color | str = colors.slate,
35
- text_size: sizes.Size | str = sizes.text_lg,
36
- font: fonts.Font | str | Iterable[fonts.Font | str] = (
37
- fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
38
- ),
39
- font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
40
- fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
41
- ),
42
- ):
43
- super().__init__(
44
- primary_hue=primary_hue,
45
- secondary_hue=secondary_hue,
46
- neutral_hue=neutral_hue,
47
- text_size=text_size,
48
- font=font,
49
- font_mono=font_mono,
50
- )
51
- super().set(
52
- background_fill_primary="*primary_50",
53
- background_fill_primary_dark="*primary_900",
54
- body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
55
- body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
56
- button_primary_text_color="white",
57
- button_primary_text_color_hover="white",
58
- button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
59
- button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
60
- button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_800)",
61
- button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_500)",
62
- button_secondary_text_color="black",
63
- button_secondary_text_color_hover="white",
64
- button_secondary_background_fill="linear-gradient(90deg, *primary_300, *primary_300)",
65
- button_secondary_background_fill_hover="linear-gradient(90deg, *primary_400, *primary_400)",
66
- button_secondary_background_fill_dark="linear-gradient(90deg, *primary_500, *primary_600)",
67
- button_secondary_background_fill_hover_dark="linear-gradient(90deg, *primary_500, *primary_500)",
68
- slider_color="*secondary_500",
69
- slider_color_dark="*secondary_600",
70
- block_title_text_weight="600",
71
- block_border_width="3px",
72
- block_shadow="*shadow_drop_lg",
73
- button_primary_shadow="*shadow_drop_lg",
74
- button_large_padding="11px",
75
- color_accent_soft="*primary_100",
76
- block_label_background_fill="*primary_200",
77
- )
78
-
79
- steel_blue_theme = SteelBlueTheme()
80
-
81
- from diffusers import FlowMatchEulerDiscreteScheduler
82
- from optimization import optimize_pipeline_
83
- from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
84
- from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
85
- from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
86
-
87
- dtype = torch.bfloat16
88
- device = "cuda" if torch.cuda.is_available() else "cpu"
89
-
90
- pipe = QwenImageEditPlusPipeline.from_pretrained(
91
- "Qwen/Qwen-Image-Edit-2509",
92
- transformer=QwenImageTransformer2DModel.from_pretrained(
93
- "linoyts/Qwen-Image-Edit-Rapid-AIO",
94
- subfolder='transformer',
95
- torch_dtype=dtype,
96
- device_map='cuda'
97
- ),
98
- torch_dtype=dtype
99
- ).to(device)
100
-
101
- # Load all LoRA adapters
102
- pipe.load_lora_weights("autoweeb/Qwen-Image-Edit-2509-Photo-to-Anime",
103
- weight_name="Qwen-Image-Edit-2509-Photo-to-Anime_000001000.safetensors",
104
- adapter_name="anime")
105
- pipe.load_lora_weights("dx8152/Qwen-Edit-2509-Multiple-angles",
106
- weight_name="镜头转换.safetensors",
107
- adapter_name="multiple-angles")
108
- pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Light_restoration",
109
- weight_name="移除光影.safetensors",
110
- adapter_name="light-restoration")
111
- pipe.load_lora_weights("dx8152/Qwen-Image-Edit-2509-Relight",
112
- weight_name="Qwen-Edit-Relight.safetensors",
113
- adapter_name="relight")
114
-
115
- pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
116
- # It's recommended to run optimization after loading all weights
117
- #optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
118
-
119
- MAX_SEED = np.iinfo(np.int32).max
120
-
121
- # --- Helper Functions ---
122
- def update_dimensions_on_upload(image):
123
- if image is None:
124
- return 1024, 1024
125
-
126
- original_width, original_height = image.size
127
-
128
- # Cap max dimension to 1024 while preserving aspect ratio
129
- if original_width > original_height:
130
- new_width = 1024
131
- aspect_ratio = original_height / original_width
132
- new_height = int(new_width * aspect_ratio)
133
- else:
134
- new_height = 1024
135
- aspect_ratio = original_width / original_height
136
- new_width = int(new_height * aspect_ratio)
137
-
138
- # Ensure dimensions are multiples of 8 for model compatibility
139
- new_width = (new_width // 8) * 8
140
- new_height = (new_height // 8) * 8
141
-
142
- return new_width, new_height
143
-
144
- # --- Main Inference Function ---
145
- @spaces.GPU
146
- def infer(
147
- input_image,
148
- prompt,
149
- lora_adapter,
150
- seed,
151
- randomize_seed,
152
- guidance_scale,
153
- steps,
154
- width,
155
- height,
156
- progress=gr.Progress(track_tqdm=True)
157
- ):
158
- if input_image is None:
159
- raise gr.Error("Please upload an image to edit.")
160
-
161
- # Dynamically set the adapter
162
- if lora_adapter == "Photo-to-Anime":
163
- pipe.set_adapters(["anime"], adapter_weights=[1.0])
164
- elif lora_adapter == "Multiple-Angles":
165
- pipe.set_adapters(["multiple-angles"], adapter_weights=[1.0])
166
- elif lora_adapter == "Light-Restoration":
167
- pipe.set_adapters(["light-restoration"], adapter_weights=[1.0])
168
- elif lora_adapter == "Relight":
169
- pipe.set_adapters(["relight"], adapter_weights=[1.0])
170
-
171
- if randomize_seed:
172
- seed = random.randint(0, MAX_SEED)
173
-
174
- generator = torch.Generator(device=device).manual_seed(seed)
175
-
176
- # *** FIX: Added a negative prompt to enable classifier-free guidance ***
177
- negative_prompt = "worst quality, low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
178
-
179
- result = pipe(
180
- image=input_image.convert("RGB"),
181
- prompt=prompt,
182
- negative_prompt=negative_prompt, # This line enables CFG
183
- height=height,
184
- width=width,
185
- num_inference_steps=steps,
186
- generator=generator,
187
- true_cfg_scale=guidance_scale,
188
- num_images_per_prompt=1,
189
- ).images[0]
190
-
191
- return result, seed, gr.Button(visible=True)
192
-
193
- # Wrapper for examples to handle file paths
194
- @spaces.GPU
195
- def infer_example(input_image_path, prompt, lora_adapter):
196
- input_pil = Image.open(input_image_path).convert("RGB")
197
- width, height = update_dimensions_on_upload(input_pil)
198
- # Set default values for example inference
199
- result, seed, _ = infer(input_pil, prompt, lora_adapter, 0, True, 1.0, 4, width, height)
200
- return result, seed
201
-
202
- # --- UI Layout ---
203
- css="""
204
- #col-container {
205
- margin: 0 auto;
206
- max-width: 960px;
207
- }
208
- #main-title h1 {font-size: 2.1em !important;}
209
- """
210
-
211
- with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
212
- with gr.Column(elem_id="col-container"):
213
- gr.Markdown("# **Qwen-Image-Edit-2509-LoRAs-Fast**", elem_id="main-title")
214
- gr.Markdown("Perform diverse image edits using specialized LoRA adapters for the Qwen-Image-Edit model.")
215
-
216
- with gr.Row():
217
- with gr.Column():
218
- input_image = gr.Image(label="Upload Image", type="pil")
219
-
220
- prompt = gr.Text(
221
- label="Edit Prompt",
222
- show_label=True,
223
- placeholder="e.g., transform into anime",
224
- )
225
-
226
- run_button = gr.Button("Run", variant="primary")
227
-
228
- with gr.Column():
229
- output_image = gr.Image(label="Output Image", interactive=False, format="png", height=290)
230
-
231
- with gr.Row():
232
- lora_adapter = gr.Dropdown(
233
- label="Choose Editing Style",
234
- choices=["Photo-to-Anime", "Multiple-Angles", "Light-Restoration", "Relight"],
235
- value="Photo-to-Anime"
236
- )
237
-
238
- with gr.Accordion("⚙️ Advanced Settings", open=False):
239
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
240
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
241
- guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
242
- steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
243
- # Hidden sliders to hold image dimensions
244
- height = gr.Slider(label="Height", minimum=256, maximum=4096, step=8, value=1024, visible=True)
245
- width = gr.Slider(label="Width", minimum=256, maximum=4096, step=8, value=1024, visible=True)
246
-
247
- gr.Examples(
248
- examples=[
249
- ["examples/1.jpg", "Transform into anime.", "Photo-to-Anime"],
250
- ["examples/2.jpeg", "Move the camera left.", "Multiple-Angles"],
251
- ["examples/2.jpeg", "Move the camera right.", "Multiple-Angles"],
252
- ["examples/2.jpeg", "Move the camera down.", "Multiple-Angles"],
253
- ["examples/2.jpeg", "Rotate the camera 45 degrees to the left.", "Multiple-Angles"],
254
- ["examples/3.jpg", "Rotate the camera 45 degrees to the right.", "Multiple-Angles"],
255
- ["examples/3.jpg", "Switch the camera to a top-down view.", "Multiple-Angles"],
256
- ["examples/3.jpg", "Switch the camera to a wide-angle lens.", "Multiple-Angles"],
257
- ["examples/3.jpg", "Switch the camera to a close-up lens.", "Multiple-Angles"],
258
- ["examples/shadow_example.jpg", "Remove shadows and relight the image using soft lighting.", "Light-Restoration"],
259
- ["examples/relight_example.jpg", "Relight the image using soft, diffused lighting that simulates sunlight filtering through curtains.", "Relight"],
260
- ],
261
- inputs=[input_image, prompt, lora_adapter],
262
- outputs=[output_image, seed],
263
- fn=infer_example,
264
- cache_examples=False,
265
- label="Examples"
266
- )
267
-
268
- run_button.click(
269
- fn=infer,
270
- inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps, width, height],
271
- outputs=[output_image, seed]
272
- )
273
-
274
- input_image.upload(
275
- fn=update_dimensions_on_upload,
276
- inputs=[input_image],
277
- outputs=[width, height]
278
- )
279
-
280
- demo.launch(mcp_server=True, ssr_mode=False, show_error=True)