Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,39 +42,62 @@ api = HfApi(token=HF_TOKEN)
|
|
| 42 |
# DOWNLOAD FONTS FROM DATASET
|
| 43 |
# =============================================
|
| 44 |
def download_all_fonts():
|
| 45 |
-
"""Download all fonts from HF Dataset to local directory"""
|
| 46 |
try:
|
| 47 |
files = api.list_repo_files(repo_id=FONTS_DATASET, repo_type="dataset")
|
| 48 |
fonts = {}
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
for file in files:
|
| 51 |
if file.endswith(('.ttf', '.otf', '.ttc')):
|
| 52 |
font_name = os.path.basename(file)
|
| 53 |
local_path = os.path.join(FONTS_DIR, font_name)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
print(f"β
Loaded {len(fonts)} fonts")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return fonts
|
| 76 |
except Exception as e:
|
| 77 |
print(f"β Failed to download fonts: {e}")
|
|
|
|
|
|
|
| 78 |
return {}
|
| 79 |
|
| 80 |
# Download fonts at startup
|
|
@@ -166,10 +189,25 @@ def upload_to_dataset(file_path, project_id, filename, subfolder="videos"):
|
|
| 166 |
return None
|
| 167 |
|
| 168 |
def get_font_path(font_family):
|
|
|
|
| 169 |
font_family_lower = font_family.lower().replace(' ', '_')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
for key, font_info in FONTS.items():
|
| 171 |
if font_family_lower in key or key in font_family_lower:
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
return None
|
| 174 |
|
| 175 |
# =============================================
|
|
|
|
| 42 |
# DOWNLOAD FONTS FROM DATASET
|
| 43 |
# =============================================
|
| 44 |
def download_all_fonts():
|
| 45 |
+
"""Download all fonts from HF Dataset to local directory - FIXED PATH"""
|
| 46 |
try:
|
| 47 |
files = api.list_repo_files(repo_id=FONTS_DATASET, repo_type="dataset")
|
| 48 |
fonts = {}
|
| 49 |
|
| 50 |
+
# Ensure directory exists
|
| 51 |
+
os.makedirs(FONTS_DIR, exist_ok=True)
|
| 52 |
+
print(f"π Fonts directory: {FONTS_DIR}")
|
| 53 |
+
|
| 54 |
for file in files:
|
| 55 |
if file.endswith(('.ttf', '.otf', '.ttc')):
|
| 56 |
font_name = os.path.basename(file)
|
| 57 |
local_path = os.path.join(FONTS_DIR, font_name)
|
| 58 |
|
| 59 |
+
print(f"β¬οΈ Downloading font: {font_name} to {local_path}")
|
| 60 |
+
|
| 61 |
+
# Download the font
|
| 62 |
+
downloaded_path = hf_hub_download(
|
| 63 |
+
repo_id=FONTS_DATASET,
|
| 64 |
+
filename=file,
|
| 65 |
+
repo_type="dataset",
|
| 66 |
+
token=HF_TOKEN,
|
| 67 |
+
local_dir=FONTS_DIR,
|
| 68 |
+
local_dir_use_symlinks=False
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
print(f"β
Downloaded to: {downloaded_path}")
|
| 72 |
+
|
| 73 |
+
# Verify file exists
|
| 74 |
+
if os.path.exists(downloaded_path):
|
| 75 |
+
file_size = os.path.getsize(downloaded_path)
|
| 76 |
+
print(f"π File size: {file_size} bytes")
|
| 77 |
+
|
| 78 |
+
# Store font info with correct path
|
| 79 |
+
font_key = font_name.split('.')[0].lower().replace('-', '_')
|
| 80 |
+
fonts[font_key] = {
|
| 81 |
+
"path": downloaded_path,
|
| 82 |
+
"name": font_name,
|
| 83 |
+
"display_name": font_name.replace('.ttf', '').replace('.otf', '').replace('.ttc', '').replace('-', ' ')
|
| 84 |
+
}
|
| 85 |
+
else:
|
| 86 |
+
print(f"β File not found after download: {downloaded_path}")
|
| 87 |
|
| 88 |
print(f"β
Loaded {len(fonts)} fonts")
|
| 89 |
+
print(f"π Font keys: {list(fonts.keys())}")
|
| 90 |
+
|
| 91 |
+
# Debug: List all files in FONTS_DIR
|
| 92 |
+
print(f"π Files in {FONTS_DIR}:")
|
| 93 |
+
for f in os.listdir(FONTS_DIR):
|
| 94 |
+
print(f" - {f}")
|
| 95 |
+
|
| 96 |
return fonts
|
| 97 |
except Exception as e:
|
| 98 |
print(f"β Failed to download fonts: {e}")
|
| 99 |
+
import traceback
|
| 100 |
+
traceback.print_exc()
|
| 101 |
return {}
|
| 102 |
|
| 103 |
# Download fonts at startup
|
|
|
|
| 189 |
return None
|
| 190 |
|
| 191 |
def get_font_path(font_family):
|
| 192 |
+
"""Get font path from font family name - with debugging"""
|
| 193 |
font_family_lower = font_family.lower().replace(' ', '_')
|
| 194 |
+
print(f"π Looking for font: {font_family}")
|
| 195 |
+
print(f"π Available fonts: {list(FONTS.keys())}")
|
| 196 |
+
|
| 197 |
+
# Try exact match
|
| 198 |
+
if font_family_lower in FONTS:
|
| 199 |
+
path = FONTS[font_family_lower]["path"]
|
| 200 |
+
print(f"β
Found exact match: {path}")
|
| 201 |
+
return path
|
| 202 |
+
|
| 203 |
+
# Try partial match
|
| 204 |
for key, font_info in FONTS.items():
|
| 205 |
if font_family_lower in key or key in font_family_lower:
|
| 206 |
+
path = font_info["path"]
|
| 207 |
+
print(f"β
Found partial match: {key} -> {path}")
|
| 208 |
+
return path
|
| 209 |
+
|
| 210 |
+
print(f"β Font not found: {font_family}")
|
| 211 |
return None
|
| 212 |
|
| 213 |
# =============================================
|