Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,3 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import urllib.request
|
| 3 |
-
|
| 4 |
-
# Target path (relative to your repo root)
|
| 5 |
-
MODEL_DIR = './.assets/models'
|
| 6 |
-
MODEL_PATH = os.path.join(MODEL_DIR, 'GFPGANv1.4.pth')
|
| 7 |
-
MODEL_URL = 'https://huggingface.co/talhaty/GFPGANv1.4/resolve/main/GFPGANv1.4.pth'
|
| 8 |
-
|
| 9 |
-
# Create directory if it does not exist
|
| 10 |
-
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
# Download if not already present
|
| 13 |
-
if not os.path.isfile(MODEL_PATH):
|
| 14 |
-
print(f'Downloading GFPGANv1.4.pth to {MODEL_PATH}...')
|
| 15 |
-
urllib.request.urlretrieve(MODEL_URL, MODEL_PATH)
|
| 16 |
-
print('Download complete.')
|
| 17 |
-
else:
|
| 18 |
-
print(f'GFPGANv1.4.pth already exists at {MODEL_PATH}, skipping download.')
|
| 19 |
"""
|
| 20 |
π Advanced Face Swap Studio - HuggingFace Spaces Optimized
|
| 21 |
=========================================================
|
|
@@ -75,15 +57,75 @@ BASE_DIR = Path(__file__).parent
|
|
| 75 |
TEMP_DIR = Path(tempfile.mkdtemp(prefix="facefusion_"))
|
| 76 |
OUTPUT_DIR = BASE_DIR / "outputs"
|
| 77 |
CONVERT_DIR = BASE_DIR / "Convert"
|
|
|
|
| 78 |
|
| 79 |
# Create directories
|
| 80 |
TEMP_DIR.mkdir(exist_ok=True)
|
| 81 |
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 82 |
CONVERT_DIR.mkdir(exist_ok=True)
|
|
|
|
| 83 |
|
| 84 |
print(f"π Base directory: {BASE_DIR}")
|
| 85 |
print(f"π Temp directory: {TEMP_DIR}")
|
| 86 |
print(f"π€ Output directory: {OUTPUT_DIR}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# Global variables
|
| 89 |
current_process = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
π Advanced Face Swap Studio - HuggingFace Spaces Optimized
|
| 3 |
=========================================================
|
|
|
|
| 57 |
TEMP_DIR = Path(tempfile.mkdtemp(prefix="facefusion_"))
|
| 58 |
OUTPUT_DIR = BASE_DIR / "outputs"
|
| 59 |
CONVERT_DIR = BASE_DIR / "Convert"
|
| 60 |
+
ASSETS_DIR = BASE_DIR / ".assets" / "models"
|
| 61 |
|
| 62 |
# Create directories
|
| 63 |
TEMP_DIR.mkdir(exist_ok=True)
|
| 64 |
OUTPUT_DIR.mkdir(exist_ok=True)
|
| 65 |
CONVERT_DIR.mkdir(exist_ok=True)
|
| 66 |
+
ASSETS_DIR.mkdir(parents=True, exist_ok=True)
|
| 67 |
|
| 68 |
print(f"π Base directory: {BASE_DIR}")
|
| 69 |
print(f"π Temp directory: {TEMP_DIR}")
|
| 70 |
print(f"π€ Output directory: {OUTPUT_DIR}")
|
| 71 |
+
print(f"π― Assets directory: {ASSETS_DIR}")
|
| 72 |
+
|
| 73 |
+
# Download required model files
|
| 74 |
+
def download_required_models():
|
| 75 |
+
"""Download required model files if not present"""
|
| 76 |
+
import urllib.request
|
| 77 |
+
import urllib.error
|
| 78 |
+
|
| 79 |
+
models_to_download = [
|
| 80 |
+
{
|
| 81 |
+
'name': 'GFPGANv1.4.pth',
|
| 82 |
+
'url': 'https://huggingface.co/talhaty/GFPGANv1.4/resolve/main/GFPGANv1.4.pth',
|
| 83 |
+
'path': ASSETS_DIR / 'GFPGANv1.4.pth',
|
| 84 |
+
'description': 'GFPGAN face enhancement model'
|
| 85 |
+
}
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
for model in models_to_download:
|
| 89 |
+
model_path = model['path']
|
| 90 |
+
model_url = model['url']
|
| 91 |
+
model_name = model['name']
|
| 92 |
+
|
| 93 |
+
if model_path.exists():
|
| 94 |
+
file_size = model_path.stat().st_size / (1024 * 1024) # MB
|
| 95 |
+
print(f"β
{model_name} already exists ({file_size:.1f}MB)")
|
| 96 |
+
continue
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
print(f"π₯ Downloading {model_name}...")
|
| 100 |
+
print(f" URL: {model_url}")
|
| 101 |
+
print(f" Path: {model_path}")
|
| 102 |
+
|
| 103 |
+
# Create a progress callback
|
| 104 |
+
def progress_callback(block_num, block_size, total_size):
|
| 105 |
+
if total_size > 0:
|
| 106 |
+
percent = min(100, (block_num * block_size * 100) / total_size)
|
| 107 |
+
if block_num % 50 == 0: # Update every 50 blocks to avoid spam
|
| 108 |
+
print(f" Progress: {percent:.1f}%")
|
| 109 |
+
|
| 110 |
+
# Download with progress
|
| 111 |
+
urllib.request.urlretrieve(model_url, str(model_path), progress_callback)
|
| 112 |
+
|
| 113 |
+
# Verify download
|
| 114 |
+
if model_path.exists() and model_path.stat().st_size > 0:
|
| 115 |
+
file_size = model_path.stat().st_size / (1024 * 1024) # MB
|
| 116 |
+
print(f"β
{model_name} downloaded successfully ({file_size:.1f}MB)")
|
| 117 |
+
else:
|
| 118 |
+
print(f"β {model_name} download failed - file not created or empty")
|
| 119 |
+
|
| 120 |
+
except urllib.error.URLError as e:
|
| 121 |
+
print(f"β Network error downloading {model_name}: {e}")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(f"β Error downloading {model_name}: {e}")
|
| 124 |
+
|
| 125 |
+
# Download models at startup
|
| 126 |
+
print("\nπ Checking required model files...")
|
| 127 |
+
download_required_models()
|
| 128 |
+
print("β
Model check complete\n")
|
| 129 |
|
| 130 |
# Global variables
|
| 131 |
current_process = None
|