miguelmuzo commited on
Commit
38b38c8
·
verified ·
1 Parent(s): 2872aa3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +672 -0
  2. requirements.txt +20 -0
app.py ADDED
@@ -0,0 +1,672 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hashlib
2
+ import os
3
+ from io import BytesIO
4
+ import base64
5
+ import requests
6
+ from pathlib import Path
7
+ import subprocess
8
+ import shutil
9
+ import gc
10
+ import time
11
+ import json
12
+ import threading
13
+
14
+ import gradio as gr
15
+ from PIL import Image
16
+ from cachetools import LRUCache
17
+ import torch
18
+ import numpy as np
19
+ import torchvision.transforms.functional as F
20
+
21
+ # FastAPI imports for enhanced frontend integration
22
+ from fastapi import FastAPI, HTTPException
23
+ from fastapi.middleware.cors import CORSMiddleware
24
+ from pydantic import BaseModel
25
+ from typing import Optional
26
+ import uvicorn
27
+
28
+ # T4 Medium GPU Optimizations
29
+ torch.backends.cudnn.benchmark = True
30
+ torch.backends.cuda.max_split_size_mb = 512
31
+
32
+ # API Models for FastAPI
33
+ class ImageRequest(BaseModel):
34
+ source_image: str # base64 string
35
+ shape_image: Optional[str] = None # base64 string
36
+ color_image: Optional[str] = None # base64 string
37
+ blending: str = "Article"
38
+ poisson_iters: int = 0
39
+ poisson_erosion: int = 15
40
+
41
+ class ImageResponse(BaseModel):
42
+ success: bool
43
+ result_image: Optional[str] = None # base64 string
44
+ message: str
45
+
46
+ # Download working face landmarks
47
+ def download_face_landmarks():
48
+ """Download working dlib face landmarks predictor"""
49
+ landmarks_path = 'pretrained_models/ShapeAdaptor/shape_predictor_68_face_landmarks.dat'
50
+
51
+ if os.path.exists(landmarks_path) and os.path.getsize(landmarks_path) > 50000000:
52
+ print("Face landmarks already exists and appears valid")
53
+ return True
54
+
55
+ try:
56
+ print("Downloading working face landmarks predictor...")
57
+ url = 'https://github.com/davisking/dlib-models/raw/master/shape_predictor_68_face_landmarks.dat.bz2'
58
+
59
+ os.makedirs(os.path.dirname(landmarks_path), exist_ok=True)
60
+
61
+ response = requests.get(url, stream=True, timeout=300)
62
+ response.raise_for_status()
63
+
64
+ import bz2
65
+ compressed_data = response.content
66
+
67
+ with open(landmarks_path, 'wb') as f:
68
+ f.write(bz2.decompress(compressed_data))
69
+
70
+ print(f"Face landmarks downloaded successfully ({os.path.getsize(landmarks_path)/1024/1024:.1f}MB)")
71
+ return True
72
+
73
+ except Exception as e:
74
+ print(f"Failed to download face landmarks: {e}")
75
+ return False
76
+
77
+ # Comprehensive model download for full accuracy
78
+ def download_all_missing_models():
79
+ """Download ALL required models for full accuracy"""
80
+
81
+ all_required_models = {
82
+ 'pretrained_models/ArcFace/backbone_ir50.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/ArcFace/backbone_ir50.pth',
83
+ 'pretrained_models/ArcFace/ir_se50.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/ArcFace/ir_se50.pth',
84
+ 'pretrained_models/BiSeNet/face_parsing_79999_iter.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/BiSeNet/face_parsing_79999_iter.pth',
85
+ 'pretrained_models/FeatureStyleEncoder/backbone.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/FeatureStyleEncoder/backbone.pth',
86
+ 'pretrained_models/FeatureStyleEncoder/psp_ffhq_encode.pt': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/FeatureStyleEncoder/psp_ffhq_encode.pt',
87
+ 'pretrained_models/FeatureStyleEncoder/79999_iter.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/FeatureStyleEncoder/79999_iter.pth',
88
+ 'pretrained_models/FeatureStyleEncoder/143_enc.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/FeatureStyleEncoder/143_enc.pth',
89
+ 'pretrained_models/encoder4editing/e4e_ffhq_encode.pt': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/encoder4editing/e4e_ffhq_encode.pt',
90
+ 'pretrained_models/sean_checkpoints/CelebA-HQ_pretrained/latest_net_G.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/sean_checkpoints/CelebA-HQ_pretrained/latest_net_G.pth',
91
+ 'pretrained_models/PostProcess/pp_model.pth': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/PostProcess/pp_model.pth',
92
+ 'pretrained_models/PostProcess/latent_avg.pt': 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/PostProcess/latent_avg.pt',
93
+ }
94
+
95
+ print("Checking for missing models for full accuracy...")
96
+
97
+ missing_models = []
98
+ existing_models = []
99
+
100
+ for model_path, url in all_required_models.items():
101
+ if os.path.exists(model_path):
102
+ existing_models.append(os.path.basename(model_path))
103
+ else:
104
+ missing_models.append((model_path, url))
105
+
106
+ if existing_models:
107
+ print(f"Found existing models: {', '.join(existing_models)}")
108
+
109
+ if missing_models:
110
+ print(f"Downloading {len(missing_models)} missing models for full accuracy...")
111
+
112
+ for i, (model_path, url) in enumerate(missing_models, 1):
113
+ try:
114
+ model_name = os.path.basename(model_path)
115
+ print(f"[{i}/{len(missing_models)}] Downloading: {model_name}")
116
+
117
+ os.makedirs(os.path.dirname(model_path), exist_ok=True)
118
+
119
+ start_time = time.time()
120
+ response = requests.get(url, stream=True, timeout=600)
121
+ response.raise_for_status()
122
+
123
+ total_size = int(response.headers.get('content-length', 0))
124
+ downloaded = 0
125
+
126
+ with open(model_path, 'wb') as f:
127
+ for chunk in response.iter_content(chunk_size=1024*1024):
128
+ if chunk:
129
+ f.write(chunk)
130
+ downloaded += len(chunk)
131
+
132
+ if total_size > 100*1024*1024 and downloaded % (50*1024*1024) == 0:
133
+ progress = (downloaded / total_size) * 100 if total_size > 0 else 0
134
+ print(f" Progress: {progress:.1f}% ({downloaded/1024/1024:.1f}MB/{total_size/1024/1024:.1f}MB)")
135
+
136
+ elapsed = time.time() - start_time
137
+ print(f" Downloaded: {model_name} ({downloaded/1024/1024:.1f}MB in {elapsed:.1f}s)")
138
+
139
+ except Exception as e:
140
+ print(f" Failed to download {model_name}: {e}")
141
+ continue
142
+ else:
143
+ print("All models already present!")
144
+
145
+ return True
146
+
147
+ def download_mask_generator_separately():
148
+ """Download large mask_generator.pth file separately"""
149
+ mask_path = 'pretrained_models/ShapeAdaptor/mask_generator.pth'
150
+
151
+ if os.path.exists(mask_path):
152
+ print("Mask generator already exists")
153
+ return True
154
+
155
+ try:
156
+ print("Downloading mask_generator.pth (919MB)...")
157
+ url = 'https://huggingface.co/AIRI-Institute/HairFastGAN/resolve/main/pretrained_models/ShapeAdaptor/mask_generator.pth'
158
+
159
+ os.makedirs(os.path.dirname(mask_path), exist_ok=True)
160
+
161
+ response = requests.get(url, stream=True, timeout=900)
162
+ response.raise_for_status()
163
+
164
+ total_size = int(response.headers.get('content-length', 0))
165
+ downloaded = 0
166
+
167
+ with open(mask_path, 'wb') as f:
168
+ for chunk in response.iter_content(chunk_size=2*1024*1024):
169
+ if chunk:
170
+ f.write(chunk)
171
+ downloaded += len(chunk)
172
+
173
+ if downloaded % (100*1024*1024) == 0:
174
+ progress = (downloaded / total_size) * 100 if total_size > 0 else 0
175
+ print(f" Mask Generator Progress: {progress:.1f}% ({downloaded/1024/1024:.1f}MB/{total_size/1024/1024:.1f}MB)")
176
+
177
+ print(f"Successfully downloaded mask_generator.pth ({downloaded/1024/1024:.1f}MB)")
178
+ return True
179
+
180
+ except Exception as e:
181
+ print(f"Failed to download mask_generator.pth: {e}")
182
+ return False
183
+
184
+ # Download all models
185
+ download_all_missing_models()
186
+ download_mask_generator_separately()
187
+ download_face_landmarks()
188
+
189
+ # Direct HairFast imports
190
+ try:
191
+ from hair_swap import HairFast, get_parser
192
+ HAIRFAST_AVAILABLE = True
193
+ print("HairFast successfully imported!")
194
+ except ImportError as e:
195
+ print(f"HairFast import failed: {e}")
196
+ HAIRFAST_AVAILABLE = False
197
+
198
+ try:
199
+ from utils.shape_predictor import align_face
200
+ ALIGN_AVAILABLE = True
201
+ print("Face alignment available!")
202
+ except ImportError as e:
203
+ print(f"Face alignment not available: {e}")
204
+ ALIGN_AVAILABLE = False
205
+
206
+ # Global variables
207
+ hair_fast_model = None
208
+ align_cache = LRUCache(maxsize=10)
209
+
210
+ def get_gpu_memory():
211
+ """Check GPU memory for optimization"""
212
+ if torch.cuda.is_available():
213
+ return torch.cuda.get_device_properties(0).total_memory / 1e9
214
+ return 0
215
+
216
+ def optimize_for_t4():
217
+ """T4 GPU specific optimizations"""
218
+ if torch.cuda.is_available():
219
+ gpu_memory = get_gpu_memory()
220
+ print(f"GPU Memory: {gpu_memory:.1f}GB")
221
+
222
+ if gpu_memory < 20:
223
+ torch.cuda.empty_cache()
224
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:512'
225
+ print("T4 optimizations applied")
226
+
227
+ def check_model_completeness():
228
+ """Check which critical models are available"""
229
+ critical_models = {
230
+ 'StyleGAN': 'pretrained_models/StyleGAN/ffhq.pt',
231
+ 'Blending': 'pretrained_models/Blending/checkpoint.pth',
232
+ 'Rotate': 'pretrained_models/Rotate/rotate_best.pth',
233
+ 'PostProcess': 'pretrained_models/PostProcess/pp_model.pth',
234
+ 'FeatureEncoder': 'pretrained_models/FeatureStyleEncoder/143_enc.pth',
235
+ 'E4E': 'pretrained_models/encoder4editing/e4e_ffhq_encode.pt',
236
+ 'SEAN': 'pretrained_models/sean_checkpoints/CelebA-HQ_pretrained/latest_net_G.pth',
237
+ 'ShapeAdaptor': 'pretrained_models/ShapeAdaptor/mask_generator.pth',
238
+ }
239
+
240
+ available_models = {}
241
+ for name, path in critical_models.items():
242
+ available_models[name] = os.path.exists(path)
243
+
244
+ return available_models
245
+
246
+ def initialize_hairfast_original():
247
+ """Initialize HairFast exactly like original - use pure defaults"""
248
+ global hair_fast_model
249
+
250
+ if not HAIRFAST_AVAILABLE:
251
+ print("HairFast not available")
252
+ return False
253
+
254
+ try:
255
+ print("Initializing HairFast with original default arguments...")
256
+
257
+ optimize_for_t4()
258
+
259
+ available_models = check_model_completeness()
260
+ print("Available models:", {k: v for k, v in available_models.items() if v})
261
+
262
+ parser = get_parser()
263
+ args = parser.parse_args([])
264
+
265
+ hair_fast_model = HairFast(args)
266
+
267
+ available_count = sum(available_models.values())
268
+ total_count = len(available_models)
269
+ accuracy_percentage = (available_count / total_count) * 100
270
+
271
+ print(f"HairFast initialized with original defaults ({accuracy_percentage:.1f}% accuracy)")
272
+
273
+ torch.cuda.empty_cache()
274
+ gc.collect()
275
+
276
+ return True
277
+
278
+ except Exception as e:
279
+ print(f"HairFast initialization failed: {e}")
280
+ hair_fast_model = None
281
+ torch.cuda.empty_cache()
282
+ return False
283
+
284
+ def get_bytes(img):
285
+ """EXACT copy of original get_bytes function"""
286
+ if img is None:
287
+ return img
288
+ buffered = BytesIO()
289
+ img.save(buffered, format="JPEG")
290
+ return buffered.getvalue()
291
+
292
+ def bytes_to_image(image: bytes) -> Image.Image:
293
+ """EXACT copy of original bytes_to_image function"""
294
+ image = Image.open(BytesIO(image))
295
+ return image
296
+
297
+ def base64_to_image(base64_string):
298
+ """Convert base64 string to PIL Image with error handling"""
299
+ try:
300
+ if base64_string.startswith('data:image'):
301
+ base64_string = base64_string.split(',')[1]
302
+ image_bytes = base64.b64decode(base64_string)
303
+ image = Image.open(BytesIO(image_bytes))
304
+ if image.mode != 'RGB':
305
+ image = image.convert('RGB')
306
+ return image
307
+ except Exception as e:
308
+ print(f"Error converting base64 to image: {e}")
309
+ return None
310
+
311
+ def image_to_base64(image):
312
+ """Convert PIL Image to base64 string with maximum quality"""
313
+ if image is None:
314
+ return None
315
+ buffered = BytesIO()
316
+ # Use PNG for lossless quality
317
+ image.save(buffered, format="PNG", optimize=False)
318
+ img_bytes = buffered.getvalue()
319
+ img_base64 = base64.b64encode(img_bytes).decode('utf-8')
320
+ return f"data:image/png;base64,{img_base64}"
321
+
322
+ def center_crop(img):
323
+ """EXACT copy of original center_crop function"""
324
+ width, height = img.size
325
+ side = min(width, height)
326
+ left = (width - side) / 2
327
+ top = (height - side) / 2
328
+ right = (width + side) / 2
329
+ bottom = (height + side) / 2
330
+ img = img.crop((left, top, right, bottom))
331
+ return img
332
+
333
+ def resize(name):
334
+ """Fixed resize function with proper size handling"""
335
+ def resize_inner(img, align):
336
+ global align_cache
337
+ if name in align and ALIGN_AVAILABLE:
338
+ img_hash = hashlib.md5(get_bytes(img)).hexdigest()
339
+ if img_hash not in align_cache:
340
+ try:
341
+ aligned_imgs = align_face(img, return_tensors=False)
342
+ if aligned_imgs and len(aligned_imgs) > 0:
343
+ img = aligned_imgs[0]
344
+ if img.size != (1024, 1024):
345
+ img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
346
+ align_cache[img_hash] = img
347
+ else:
348
+ img = center_crop(img)
349
+ img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
350
+ align_cache[img_hash] = img
351
+ except Exception as e:
352
+ print(f"Face alignment failed for {name}, using center crop: {e}")
353
+ img = center_crop(img)
354
+ img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
355
+ align_cache[img_hash] = img
356
+ else:
357
+ img = align_cache[img_hash]
358
+ else:
359
+ if img.size != (1024, 1024):
360
+ img = center_crop(img)
361
+ img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
362
+ return img
363
+ return resize_inner
364
+
365
+ def swap_hair_selective(face, shape, color, blending, poisson_iters, poisson_erosion):
366
+ """
367
+ Enhanced swap logic with selective transfer:
368
+ - If only shape provided: change hairstyle only
369
+ - If only color provided: change hair color only
370
+ - If both provided: change both hairstyle and color
371
+ """
372
+ global hair_fast_model
373
+
374
+ if hair_fast_model is None:
375
+ if not initialize_hairfast_original():
376
+ return None, "HairFast model not available. Please check if all model files are uploaded."
377
+
378
+ if not face and not shape and not color:
379
+ return None, "Need to upload a face and at least a shape or color ❗"
380
+ elif not face:
381
+ return None, "Need to upload a face ❗"
382
+ elif not shape and not color:
383
+ return None, "Need to upload at least a shape or color ❗"
384
+
385
+ try:
386
+ print("Starting selective hair transfer...")
387
+
388
+ torch.cuda.empty_cache()
389
+ gc.collect()
390
+
391
+ def validate_size(img, name):
392
+ if img is not None:
393
+ if img.size != (1024, 1024):
394
+ print(f"Resizing {name} from {img.size} to (1024, 1024)")
395
+ img = center_crop(img)
396
+ img = img.resize((1024, 1024), Image.Resampling.LANCZOS)
397
+ return img
398
+
399
+ face = validate_size(face, "face")
400
+ shape = validate_size(shape, "shape") if shape else None
401
+ color = validate_size(color, "color") if color else None
402
+
403
+ # Determine transfer mode
404
+ has_shape = shape is not None
405
+ has_color = color is not None
406
+
407
+ if has_shape and has_color:
408
+ transfer_mode = "both"
409
+ print("Transfer mode: Both hairstyle and color")
410
+ elif has_shape and not has_color:
411
+ transfer_mode = "shape_only"
412
+ color = face # Use original face for color reference
413
+ print("Transfer mode: Hairstyle only (preserving original color)")
414
+ elif has_color and not has_shape:
415
+ transfer_mode = "color_only"
416
+ shape = face # Use original face for shape reference
417
+ print("Transfer mode: Color only (preserving original hairstyle)")
418
+
419
+ print(f"Final sizes - Face: {face.size}, Shape: {shape.size if shape else 'None'}, Color: {color.size if color else 'None'}")
420
+
421
+ with torch.no_grad():
422
+ start_time = time.time()
423
+
424
+ # Use the HairFast model's swap method with proper parameters
425
+ final_image = hair_fast_model.swap(face, shape, color)
426
+
427
+ inference_time = time.time() - start_time
428
+ print(f"Inference completed in {inference_time:.2f} seconds")
429
+
430
+ result_image = F.to_pil_image(final_image)
431
+
432
+ torch.cuda.empty_cache()
433
+ gc.collect()
434
+
435
+ success_message = f"Hair transfer ({transfer_mode}) completed successfully in {inference_time:.2f}s"
436
+ print(success_message)
437
+
438
+ return result_image, success_message
439
+
440
+ except Exception as e:
441
+ torch.cuda.empty_cache()
442
+ gc.collect()
443
+ error_msg = f"Hair transfer failed: {str(e)}"
444
+ print(f"Detailed error: {e}")
445
+ return None, error_msg
446
+
447
+ def hair_transfer_api(source_image, shape_image=None, color_image=None,
448
+ blending="Article", poisson_iters=0, poisson_erosion=15):
449
+ """Enhanced API function for frontend integration with quality preservation"""
450
+ try:
451
+ print("API call received - processing images...")
452
+
453
+ if isinstance(source_image, str):
454
+ source_image = base64_to_image(source_image)
455
+ print(f"Source image loaded: {source_image.size if source_image else 'Failed'}")
456
+
457
+ if isinstance(shape_image, str) and shape_image:
458
+ shape_image = base64_to_image(shape_image)
459
+ print(f"Shape image loaded: {shape_image.size if shape_image else 'Failed'}")
460
+
461
+ if isinstance(color_image, str) and color_image:
462
+ color_image = base64_to_image(color_image)
463
+ print(f"Color image loaded: {color_image.size if color_image else 'Failed'}")
464
+
465
+ if not source_image:
466
+ return None, "Failed to process source image"
467
+
468
+ result_image, status_message = swap_hair_selective(
469
+ source_image, shape_image, color_image,
470
+ blending, poisson_iters, poisson_erosion
471
+ )
472
+
473
+ if result_image is not None:
474
+ print(f"Result image generated: {result_image.size}")
475
+ result_base64 = image_to_base64(result_image)
476
+ print("Result converted to base64 successfully")
477
+ return result_base64, status_message
478
+ else:
479
+ print("Result image is None")
480
+ return None, status_message
481
+
482
+ except Exception as e:
483
+ error_msg = f"API Error: {str(e)}"
484
+ print(f"API Error details: {e}")
485
+ return None, error_msg
486
+
487
+ def get_demo():
488
+ """Enhanced Gradio interface with selective transfer options"""
489
+ with gr.Blocks(
490
+ title="HairFastGAN - Selective Transfer",
491
+ theme=gr.themes.Soft(),
492
+ css="""
493
+ .error-message {
494
+ color: red !important;
495
+ background-color: #ffebee !important;
496
+ padding: 10px !important;
497
+ border-radius: 5px !important;
498
+ }
499
+ .transfer-info {
500
+ background-color: #e3f2fd !important;
501
+ padding: 10px !important;
502
+ border-radius: 5px !important;
503
+ margin: 10px 0 !important;
504
+ }
505
+ """
506
+ ) as demo:
507
+
508
+ gr.Markdown("## HairFastGan - Selective Transfer")
509
+ gr.Markdown(
510
+ '<div style="display: flex; align-items: center; gap: 10px;">'
511
+ '<span>Enhanced HairFastGAN with selective transfer:</span>'
512
+ '<a href="https://arxiv.org/abs/2404.01094"><img src="https://img.shields.io/badge/arXiv-2404.01094-b31b1b.svg" height=22.5></a>'
513
+ '<a href="https://github.com/AIRI-Institute/HairFastGAN"><img src="https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white" height=22.5></a>'
514
+ '<a href="https://huggingface.co/AIRI-Institute/HairFastGAN"><img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/model-on-hf-md.svg" height=22.5></a>'
515
+ '<a href="https://colab.research.google.com/#fileId=https://huggingface.co/AIRI-Institute/HairFastGAN/blob/main/notebooks/HairFast_inference.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" height=22.5></a>'
516
+ '</div>'
517
+ )
518
+
519
+ gr.Markdown(
520
+ """
521
+ <div class="transfer-info">
522
+ <b>🎯 Selective Transfer Modes:</b><br>
523
+ • <b>Shape Only:</b> Upload only shape image → Changes hairstyle while preserving original color<br>
524
+ • <b>Color Only:</b> Upload only color image → Changes hair color while preserving original hairstyle<br>
525
+ • <b>Both:</b> Upload both images → Changes both hairstyle and color
526
+ </div>
527
+ """,
528
+ elem_classes="transfer-info"
529
+ )
530
+
531
+ with gr.Row():
532
+ with gr.Column():
533
+ source = gr.Image(label="Source photo to try on the hairstyle", type="pil")
534
+
535
+ with gr.Row():
536
+ shape = gr.Image(label="Shape photo with desired hairstyle (optional)", type="pil")
537
+ color = gr.Image(label="Color photo with desired hair color (optional)", type="pil")
538
+
539
+ # Transfer mode indicator
540
+ transfer_status = gr.Textbox(label="Transfer Mode", interactive=False,
541
+ value="Upload images to see transfer mode")
542
+
543
+ with gr.Accordion("Advanced Options", open=False):
544
+ blending = gr.Radio(["Article", "Alternative_v1", "Alternative_v2"], value='Article',
545
+ label="Color Encoder version")
546
+ poisson_iters = gr.Slider(0, 2500, value=0, step=1, label="Poisson iters")
547
+ poisson_erosion = gr.Slider(1, 100, value=15, step=1, label="Poisson erosion")
548
+ align = gr.CheckboxGroup(["Face", "Shape", "Color"], value=["Face", "Shape", "Color"],
549
+ label="Image cropping [recommended]")
550
+
551
+ btn = gr.Button("Get the haircut", variant="primary")
552
+
553
+ with gr.Column():
554
+ output = gr.Image(label="Your result")
555
+ error_message = gr.Textbox(label="⚠️ Error ⚠️", visible=False, elem_classes="error-message")
556
+
557
+ # Function to update transfer mode display
558
+ def update_transfer_mode(shape_img, color_img):
559
+ if shape_img is not None and color_img is not None:
560
+ return "🎯 Both hairstyle and color will be transferred"
561
+ elif shape_img is not None and color_img is None:
562
+ return "🎨 Only hairstyle will be transferred (color preserved)"
563
+ elif shape_img is None and color_img is not None:
564
+ return "🌈 Only hair color will be transferred (style preserved)"
565
+ else:
566
+ return "Upload shape and/or color reference images"
567
+
568
+ # Update transfer mode when images change
569
+ shape.change(fn=update_transfer_mode, inputs=[shape, color], outputs=transfer_status)
570
+ color.change(fn=update_transfer_mode, inputs=[shape, color], outputs=transfer_status)
571
+
572
+ source.upload(fn=resize('Face'), inputs=[source, align], outputs=source)
573
+ shape.upload(fn=resize('Shape'), inputs=[shape, align], outputs=shape)
574
+ color.upload(fn=resize('Color'), inputs=[color, align], outputs=color)
575
+
576
+ btn.click(
577
+ fn=swap_hair_selective,
578
+ inputs=[source, shape, color, blending, poisson_iters, poisson_erosion],
579
+ outputs=[output, error_message],
580
+ api_name="predict"
581
+ )
582
+
583
+ gr.Markdown('''
584
+ ### How to use:
585
+ 1. **Upload your source photo** (the face you want to modify)
586
+ 2. **Choose your transfer mode:**
587
+ - For **hairstyle only**: Upload only a shape reference image
588
+ - For **color only**: Upload only a color reference image
589
+ - For **both**: Upload both shape and color reference images
590
+ 3. **Click "Get the haircut"** and wait for results!
591
+
592
+
593
+ ''')
594
+
595
+ return demo
596
+
597
+ def create_api_app():
598
+ """Create FastAPI app for better frontend integration"""
599
+ app = FastAPI(title="HairFastGAN Selective Transfer API", version="2.0")
600
+
601
+ app.add_middleware(
602
+ CORSMiddleware,
603
+ allow_origins=["*"],
604
+ allow_credentials=True,
605
+ allow_methods=["*"],
606
+ allow_headers=["*"],
607
+ )
608
+
609
+ @app.post("/api/hair-transfer", response_model=ImageResponse)
610
+ async def transfer_hair(request: ImageRequest):
611
+ """Enhanced API endpoint for hair transfer"""
612
+ try:
613
+ print(f"API request received: {len(request.source_image)} chars source")
614
+
615
+ result_base64, message = hair_transfer_api(
616
+ source_image=request.source_image,
617
+ shape_image=request.shape_image,
618
+ color_image=request.color_image,
619
+ blending=request.blending,
620
+ poisson_iters=request.poisson_iters,
621
+ poisson_erosion=request.poisson_erosion
622
+ )
623
+
624
+ if result_base64:
625
+ return ImageResponse(
626
+ success=True,
627
+ result_image=result_base64,
628
+ message=message or "Hair transfer completed successfully"
629
+ )
630
+ else:
631
+ return ImageResponse(
632
+ success=False,
633
+ message=message or "Hair transfer failed"
634
+ )
635
+
636
+ except Exception as e:
637
+ print(f"API endpoint error: {e}")
638
+ raise HTTPException(status_code=500, detail=str(e))
639
+
640
+ @app.get("/api/health")
641
+ async def health_check():
642
+ """Health check endpoint"""
643
+ return {"status": "healthy", "model_loaded": hair_fast_model is not None}
644
+
645
+ return app
646
+
647
+ if __name__ == '__main__':
648
+ optimize_for_t4()
649
+
650
+ align_cache = LRUCache(maxsize=10)
651
+
652
+ if not initialize_hairfast_original():
653
+ print("Failed to initialize HairFast model")
654
+ exit(1)
655
+
656
+ gradio_demo = get_demo()
657
+ fastapi_app = create_api_app()
658
+
659
+ def run_fastapi():
660
+ uvicorn.run(fastapi_app, host="0.0.0.0", port=8000, log_level="info")
661
+
662
+ def run_gradio():
663
+ gradio_demo.queue(max_size=10, default_concurrency_limit=2)
664
+ gradio_demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
665
+
666
+ print("Starting FastAPI server on port 8000...")
667
+ fastapi_thread = threading.Thread(target=run_fastapi)
668
+ fastapi_thread.daemon = True
669
+ fastapi_thread.start()
670
+
671
+ print("Starting Gradio server on port 7860...")
672
+ run_gradio()
requirements.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pillow==10.0.0
2
+ face_alignment==1.3.4
3
+ addict==2.4.0
4
+ git+https://github.com/openai/CLIP.git
5
+ gdown==4.7.1
6
+ grpcio==1.63.0
7
+ grpcio_tools==1.63.0
8
+ gradio==4.31.5
9
+ cachetools==5.3.3
10
+ dlib-binary==19.24.1
11
+ opencv-python==4.11.0.86
12
+ torch>=2.0.0
13
+ torchvision>=0.15.0
14
+ numpy>=1.21.0
15
+ scipy>=1.7.0
16
+ scikit-image>=0.18.0
17
+ scikit-learn>=1.0.0
18
+ numba>=0.56.0
19
+ ninja
20
+ cmake