Spaces:
Paused
Paused
saicharan1234
commited on
Commit
•
84ec534
1
Parent(s):
4b9e459
Update main.py
Browse files
main.py
CHANGED
@@ -16,69 +16,91 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
16 |
|
17 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
"fluently/Fluently-
|
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 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
77 |
if randomize_seed:
|
78 |
seed = random.randint(0, MAX_SEED)
|
79 |
return seed
|
80 |
|
81 |
-
|
82 |
@app.post("/generate")
|
83 |
async def generate(
|
84 |
model: str = Form(...),
|
@@ -103,8 +125,10 @@ async def generate(
|
|
103 |
inpaint_image_pil = Image.open(io.BytesIO(await inpaint_image.read())) if inpaint_image else None
|
104 |
mask_image_pil = Image.open(io.BytesIO(await mask_image.read())) if mask_image else None
|
105 |
|
|
|
|
|
106 |
if model == "Fluently XL Final":
|
107 |
-
images =
|
108 |
prompt=prompt,
|
109 |
negative_prompt=negative_prompt,
|
110 |
width=width,
|
@@ -115,7 +139,7 @@ async def generate(
|
|
115 |
output_type="pil",
|
116 |
).images
|
117 |
elif model == "Fluently Anime":
|
118 |
-
images =
|
119 |
prompt=prompt,
|
120 |
negative_prompt=negative_prompt,
|
121 |
width=width,
|
@@ -126,7 +150,7 @@ async def generate(
|
|
126 |
output_type="pil",
|
127 |
).images
|
128 |
elif model == "Fluently Epic":
|
129 |
-
images =
|
130 |
prompt=prompt,
|
131 |
negative_prompt=negative_prompt,
|
132 |
width=width,
|
@@ -137,7 +161,7 @@ async def generate(
|
|
137 |
output_type="pil",
|
138 |
).images
|
139 |
elif model == "Fluently XL v4":
|
140 |
-
images =
|
141 |
prompt=prompt,
|
142 |
negative_prompt=negative_prompt,
|
143 |
width=width,
|
@@ -148,7 +172,7 @@ async def generate(
|
|
148 |
output_type="pil",
|
149 |
).images
|
150 |
elif model == "Fluently XL v3 Lightning":
|
151 |
-
images =
|
152 |
prompt=prompt,
|
153 |
negative_prompt=negative_prompt,
|
154 |
width=width,
|
@@ -159,8 +183,8 @@ async def generate(
|
|
159 |
output_type="pil",
|
160 |
).images
|
161 |
elif model == "Fluently v4 inpaint" or model == "Fluently XL v3 inpaint":
|
162 |
-
blurred_mask =
|
163 |
-
images =
|
164 |
prompt=prompt,
|
165 |
image=inpaint_image_pil,
|
166 |
mask_image=blurred_mask,
|
@@ -174,6 +198,10 @@ async def generate(
|
|
174 |
output_type="pil",
|
175 |
).images
|
176 |
|
|
|
|
|
|
|
|
|
177 |
img = images[0]
|
178 |
img_byte_arr = io.BytesIO()
|
179 |
img.save(img_byte_arr, format='PNG')
|
|
|
16 |
|
17 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
18 |
|
19 |
+
# Set directory for model storage
|
20 |
+
MODEL_DIR = "/data"
|
21 |
+
|
22 |
+
# Ensure model directory exists
|
23 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
24 |
+
|
25 |
+
# Download models to local directory
|
26 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token
|
27 |
+
def download_model(repo_id, filename=None, model_dir=MODEL_DIR, token=HF_TOKEN):
|
28 |
+
if filename:
|
29 |
+
return hf_hub_download(repo_id=repo_id, filename=filename, local_dir=model_dir, token=token)
|
30 |
+
return hf_hub_download(repo_id=repo_id, local_dir=model_dir, token=token)
|
31 |
+
|
32 |
+
# Paths for models
|
33 |
+
paths = {
|
34 |
+
"Fluently XL Final": download_model("fluently/Fluently-XL-Final", "FluentlyXL-Final.safetensors"),
|
35 |
+
"Fluently Anime": download_model("fluently/Fluently-anime"),
|
36 |
+
"Fluently Epic": download_model("fluently/Fluently-epic"),
|
37 |
+
"Fluently XL v4": download_model("fluently/Fluently-XL-v4"),
|
38 |
+
"Fluently XL v3 Lightning": download_model("fluently/Fluently-XL-v3-lightning"),
|
39 |
+
"Fluently v4 inpaint": download_model("fluently/Fluently-v4-inpainting"),
|
40 |
+
"Fluently XL v3 inpaint": download_model("fluently/Fluently-XL-v3-inpainting", "FluentlyXL-v3-inpainting.safetensors"),
|
41 |
+
}
|
42 |
+
|
43 |
+
# Function to load model dynamically
|
44 |
+
def load_model(model_name):
|
45 |
+
if model_name == "Fluently XL Final":
|
46 |
+
model = StableDiffusionXLPipeline.from_single_file(
|
47 |
+
paths[model_name],
|
48 |
+
torch_dtype=torch.float16,
|
49 |
+
use_safetensors=True,
|
50 |
+
)
|
51 |
+
model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)
|
52 |
+
elif model_name == "Fluently Anime":
|
53 |
+
model = StableDiffusionPipeline.from_pretrained(
|
54 |
+
paths[model_name],
|
55 |
+
torch_dtype=torch.float16,
|
56 |
+
use_safetensors=True,
|
57 |
+
)
|
58 |
+
model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)
|
59 |
+
elif model_name == "Fluently Epic":
|
60 |
+
model = StableDiffusionPipeline.from_pretrained(
|
61 |
+
paths[model_name],
|
62 |
+
torch_dtype=torch.float16,
|
63 |
+
use_safetensors=True,
|
64 |
+
)
|
65 |
+
model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)
|
66 |
+
elif model_name == "Fluently XL v4":
|
67 |
+
model = StableDiffusionXLPipeline.from_pretrained(
|
68 |
+
paths[model_name],
|
69 |
+
torch_dtype=torch.float16,
|
70 |
+
use_safetensors=True,
|
71 |
+
)
|
72 |
+
model.scheduler = EulerAncestralDiscreteScheduler.from_config(model.scheduler.config)
|
73 |
+
elif model_name == "Fluently XL v3 Lightning":
|
74 |
+
model = StableDiffusionXLPipeline.from_pretrained(
|
75 |
+
paths[model_name],
|
76 |
+
torch_dtype=torch.float16,
|
77 |
+
use_safetensors=True,
|
78 |
+
)
|
79 |
+
model.scheduler = DPMSolverSinglestepScheduler.from_config(model.scheduler.config, use_karras_sigmas=False, timestep_spacing="trailing", lower_order_final=True)
|
80 |
+
elif model_name in ["Fluently v4 inpaint", "Fluently XL v3 inpaint"]:
|
81 |
+
if model_name == "Fluently v4 inpaint":
|
82 |
+
model = StableDiffusionInpaintPipeline.from_pretrained(
|
83 |
+
paths[model_name],
|
84 |
+
torch_dtype=torch.float16,
|
85 |
+
use_safetensors=True,
|
86 |
+
)
|
87 |
+
else:
|
88 |
+
model = StableDiffusionXLInpaintPipeline.from_single_file(
|
89 |
+
paths[model_name],
|
90 |
+
torch_dtype=torch.float16,
|
91 |
+
use_safetensors=True,
|
92 |
+
)
|
93 |
+
else:
|
94 |
+
raise ValueError(f"Model {model_name} not found")
|
95 |
+
|
96 |
+
model.to(device)
|
97 |
+
return model
|
98 |
|
99 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
100 |
if randomize_seed:
|
101 |
seed = random.randint(0, MAX_SEED)
|
102 |
return seed
|
103 |
|
|
|
104 |
@app.post("/generate")
|
105 |
async def generate(
|
106 |
model: str = Form(...),
|
|
|
125 |
inpaint_image_pil = Image.open(io.BytesIO(await inpaint_image.read())) if inpaint_image else None
|
126 |
mask_image_pil = Image.open(io.BytesIO(await mask_image.read())) if mask_image else None
|
127 |
|
128 |
+
model_pipeline = load_model(model)
|
129 |
+
|
130 |
if model == "Fluently XL Final":
|
131 |
+
images = model_pipeline(
|
132 |
prompt=prompt,
|
133 |
negative_prompt=negative_prompt,
|
134 |
width=width,
|
|
|
139 |
output_type="pil",
|
140 |
).images
|
141 |
elif model == "Fluently Anime":
|
142 |
+
images = model_pipeline(
|
143 |
prompt=prompt,
|
144 |
negative_prompt=negative_prompt,
|
145 |
width=width,
|
|
|
150 |
output_type="pil",
|
151 |
).images
|
152 |
elif model == "Fluently Epic":
|
153 |
+
images = model_pipeline(
|
154 |
prompt=prompt,
|
155 |
negative_prompt=negative_prompt,
|
156 |
width=width,
|
|
|
161 |
output_type="pil",
|
162 |
).images
|
163 |
elif model == "Fluently XL v4":
|
164 |
+
images = model_pipeline(
|
165 |
prompt=prompt,
|
166 |
negative_prompt=negative_prompt,
|
167 |
width=width,
|
|
|
172 |
output_type="pil",
|
173 |
).images
|
174 |
elif model == "Fluently XL v3 Lightning":
|
175 |
+
images = model_pipeline(
|
176 |
prompt=prompt,
|
177 |
negative_prompt=negative_prompt,
|
178 |
width=width,
|
|
|
183 |
output_type="pil",
|
184 |
).images
|
185 |
elif model == "Fluently v4 inpaint" or model == "Fluently XL v3 inpaint":
|
186 |
+
blurred_mask = model_pipeline.mask_processor.blur(mask_image_pil, blur_factor=blur_factor)
|
187 |
+
images = model_pipeline(
|
188 |
prompt=prompt,
|
189 |
image=inpaint_image_pil,
|
190 |
mask_image=blurred_mask,
|
|
|
198 |
output_type="pil",
|
199 |
).images
|
200 |
|
201 |
+
# Unload the model from the device
|
202 |
+
model_pipeline.to("cpu")
|
203 |
+
torch.cuda.empty_cache()
|
204 |
+
|
205 |
img = images[0]
|
206 |
img_byte_arr = io.BytesIO()
|
207 |
img.save(img_byte_arr, format='PNG')
|