prithivMLmods commited on
Commit
31dbf49
·
verified ·
1 Parent(s): 9b8fb30

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +211 -1
README.md CHANGED
@@ -9,4 +9,214 @@ pipeline_tag: image-to-image
9
  tags:
10
  - art
11
  - 8bit
12
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  tags:
10
  - art
11
  - 8bit
12
+ ---
13
+
14
+ ## Quick Start with Diffusers 🧨
15
+
16
+ ### Install the required packages
17
+
18
+ ```py
19
+ transformers # - transformers@v4.57.6
20
+ torch # - torch@v2.9.1+cu128
21
+ diffusers # - diffusers@v0.37.0.dev0
22
+ bitsandbytes # - bitsandbytes@v0.49.2
23
+ gradio # - gradio@v6.6.0
24
+ accelerate # - accelerate@v1.12.0
25
+ ```
26
+
27
+ ### Run FireRed-Image-Edit-1.0-8bit [Demo]
28
+
29
+ ```py
30
+ import os
31
+ import gc
32
+ import gradio as gr
33
+ import numpy as np
34
+ #import spaces # Uncomment the Spaces-related modules if you are using HF ZeroGPU
35
+
36
+ import torch
37
+ import random
38
+ from PIL import Image
39
+
40
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
41
+
42
+ print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
43
+ print("torch.__version__ =", torch.__version__)
44
+ print("Using device:", device)
45
+
46
+ from diffusers.models import QwenImageTransformer2DModel
47
+ from diffusers import QwenImageEditPlusPipeline
48
+ from diffusers.utils import load_image
49
+
50
+ dtype = torch.bfloat16
51
+
52
+ transformer = QwenImageTransformer2DModel.from_pretrained(
53
+ "prithivMLmods/FireRed-Image-Edit-1.0-8bit",
54
+ subfolder="transformer",
55
+ torch_dtype=dtype
56
+ )
57
+
58
+ pipe = QwenImageEditPlusPipeline.from_pretrained(
59
+ "prithivMLmods/FireRed-Image-Edit-1.0-8bit",
60
+ transformer=transformer,
61
+ torch_dtype=dtype
62
+ ).to(device)
63
+
64
+ MAX_SEED = np.iinfo(np.int32).max
65
+
66
+ def update_dimensions_on_upload(image):
67
+ if image is None:
68
+ return 1024, 1024
69
+
70
+ original_width, original_height = image.size
71
+
72
+ if original_width > original_height:
73
+ new_width = 1024
74
+ aspect_ratio = original_height / original_width
75
+ new_height = int(new_width * aspect_ratio)
76
+ else:
77
+ new_height = 1024
78
+ aspect_ratio = original_width / original_height
79
+ new_width = int(new_height * aspect_ratio)
80
+
81
+ new_width = (new_width // 8) * 8
82
+ new_height = (new_height // 8) * 8
83
+
84
+ return new_width, new_height
85
+
86
+ #@spaces.GPU
87
+ def infer(
88
+ images,
89
+ prompt,
90
+ seed,
91
+ randomize_seed,
92
+ guidance_scale,
93
+ steps,
94
+ progress=gr.Progress(track_tqdm=True)
95
+ ):
96
+ gc.collect()
97
+ torch.cuda.empty_cache()
98
+
99
+ if not images:
100
+ raise gr.Error("Please upload at least one image to edit.")
101
+
102
+ pil_images = []
103
+ if images is not None:
104
+ for item in images:
105
+ try:
106
+ if isinstance(item, tuple) or isinstance(item, list):
107
+ path_or_img = item[0]
108
+ else:
109
+ path_or_img = item
110
+
111
+ if isinstance(path_or_img, str):
112
+ pil_images.append(Image.open(path_or_img).convert("RGB"))
113
+ elif isinstance(path_or_img, Image.Image):
114
+ pil_images.append(path_or_img.convert("RGB"))
115
+ else:
116
+ pil_images.append(Image.open(path_or_img.name).convert("RGB"))
117
+ except Exception as e:
118
+ print(f"Skipping invalid image item: {e}")
119
+ continue
120
+
121
+ if not pil_images:
122
+ raise gr.Error("Could not process uploaded images.")
123
+
124
+ if randomize_seed:
125
+ seed = random.randint(0, MAX_SEED)
126
+
127
+ generator = torch.Generator(device=device).manual_seed(seed)
128
+ 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"
129
+
130
+ width, height = update_dimensions_on_upload(pil_images[0])
131
+
132
+ try:
133
+ result_image = pipe(
134
+ image=pil_images,
135
+ prompt=prompt,
136
+ negative_prompt=negative_prompt,
137
+ height=height,
138
+ width=width,
139
+ num_inference_steps=steps,
140
+ generator=generator,
141
+ true_cfg_scale=guidance_scale,
142
+ ).images[0]
143
+
144
+ return result_image, seed
145
+
146
+ except Exception as e:
147
+ raise e
148
+ finally:
149
+ gc.collect()
150
+ torch.cuda.empty_cache()
151
+
152
+ #@spaces.GPU
153
+ def infer_example(images, prompt):
154
+ if not images:
155
+ return None, 0
156
+
157
+ if isinstance(images, str):
158
+ images_list = [images]
159
+ else:
160
+ images_list = images
161
+
162
+ result, seed = infer(
163
+ images=images_list,
164
+ prompt=prompt,
165
+ seed=0,
166
+ randomize_seed=True,
167
+ guidance_scale=1.0,
168
+ steps=20
169
+ )
170
+ return result, seed
171
+
172
+ css="""
173
+ #col-container {
174
+ margin: 0 auto;
175
+ max-width: 1000px;
176
+ }
177
+ #main-title h1 {font-size: 2.4em !important;}
178
+ """
179
+
180
+ with gr.Blocks() as demo:
181
+ with gr.Column(elem_id="col-container"):
182
+ gr.Markdown("# **FireRed-Image-Edit-1.0-8bit**", elem_id="main-title")
183
+
184
+ with gr.Row(equal_height=True):
185
+ with gr.Column():
186
+ images = gr.Gallery(
187
+ label="Upload Images",
188
+ type="filepath",
189
+ columns=2,
190
+ rows=1,
191
+ height=300,
192
+ allow_preview=True
193
+ )
194
+
195
+ with gr.Row():
196
+ prompt = gr.Text(
197
+ label="Edit Prompt",
198
+ show_label=True,
199
+ placeholder="e.g., transform into anime..",
200
+ )
201
+
202
+ with gr.Row():
203
+ run_button = gr.Button("Edit Image", variant="primary")
204
+
205
+ with gr.Column():
206
+ output_image = gr.Image(label="Output Image", interactive=False, format="png", height=390)
207
+
208
+ with gr.Accordion("Advanced Settings", open=False, visible=True):
209
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
210
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
211
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
212
+ steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=20)
213
+
214
+ run_button.click(
215
+ fn=infer,
216
+ inputs=[images, prompt, seed, randomize_seed, guidance_scale, steps],
217
+ outputs=[output_image, seed]
218
+ )
219
+
220
+ if __name__ == "__main__":
221
+ demo.queue(max_size=30).launch(css=css, mcp_server=True, ssr_mode=False, show_error=True)
222
+ ```