saicharan1234 commited on
Commit
84ec534
1 Parent(s): 4b9e459

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +92 -64
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
- # Load pipelines
20
- HF_TOKEN = os.getenv("HF_TOKEN") # Replace with your actual token
21
- pipe_xl_final = StableDiffusionXLPipeline.from_single_file(
22
- hf_hub_download(repo_id="fluently/Fluently-XL-Final", filename="FluentlyXL-Final.safetensors", token=HF_TOKEN),
23
- torch_dtype=torch.float16,
24
- use_safetensors=True,
25
- )
26
- pipe_xl_final.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_xl_final.scheduler.config)
27
- pipe_xl_final.to(device)
28
-
29
- pipe_anime = StableDiffusionPipeline.from_pretrained(
30
- "fluently/Fluently-anime",
31
- torch_dtype=torch.float16,
32
- use_safetensors=True,
33
- )
34
- pipe_anime.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_anime.scheduler.config)
35
- pipe_anime.to(device)
36
-
37
- pipe_epic = StableDiffusionPipeline.from_pretrained(
38
- "fluently/Fluently-epic",
39
- torch_dtype=torch.float16,
40
- use_safetensors=True,
41
- )
42
- pipe_epic.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_epic.scheduler.config)
43
- pipe_epic.to(device)
44
-
45
- pipe_xl_inpaint = StableDiffusionXLInpaintPipeline.from_single_file(
46
- "https://huggingface.co/fluently/Fluently-XL-v3-inpainting/blob/main/FluentlyXL-v3-inpainting.safetensors",
47
- torch_dtype=torch.float16,
48
- use_safetensors=True,
49
- )
50
- pipe_xl_inpaint.to(device)
51
-
52
- pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
53
- "fluently/Fluently-v4-inpainting",
54
- torch_dtype=torch.float16,
55
- use_safetensors=True,
56
- )
57
- pipe_inpaint.to(device)
58
-
59
- pipe_xl = StableDiffusionXLPipeline.from_pretrained(
60
- "fluently/Fluently-XL-v4",
61
- torch_dtype=torch.float16,
62
- use_safetensors=True,
63
- )
64
- pipe_xl.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_xl.scheduler.config)
65
- pipe_xl.to(device)
66
-
67
- pipe_xl_lightning = StableDiffusionXLPipeline.from_pretrained(
68
- "fluently/Fluently-XL-v3-lightning",
69
- torch_dtype=torch.float16,
70
- use_safetensors=True,
71
- )
72
- pipe_xl_lightning.scheduler = DPMSolverSinglestepScheduler.from_config(pipe_xl_lightning.scheduler.config, use_karras_sigmas=False, timestep_spacing="trailing", lower_order_final=True)
73
- pipe_xl_lightning.to(device)
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 = pipe_xl_final(
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 = pipe_anime(
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 = pipe_epic(
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 = pipe_xl(
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 = pipe_xl_lightning(
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 = pipe_inpaint.mask_processor.blur(mask_image_pil, blur_factor=blur_factor)
163
- images = pipe_inpaint(
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')