Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,11 +15,16 @@ from PIL import Image
|
|
| 15 |
import io
|
| 16 |
import time
|
| 17 |
|
| 18 |
-
# Set up
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
from huggingface_hub
|
| 22 |
-
from huggingface_hub import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Configure Hugging Face cache and environment
|
| 25 |
os.environ["HF_HOME"] = os.path.join(os.getcwd(), ".cache/huggingface")
|
|
@@ -138,8 +143,6 @@ def load_model():
|
|
| 138 |
|
| 139 |
# Pre-check model availability
|
| 140 |
try:
|
| 141 |
-
from huggingface_hub import model_info
|
| 142 |
-
|
| 143 |
# Try to get token first
|
| 144 |
token = get_hf_token()
|
| 145 |
|
|
@@ -147,9 +150,23 @@ def load_model():
|
|
| 147 |
# Check if the model is available in Hugging Face
|
| 148 |
try:
|
| 149 |
model_status.info("Checking model access...")
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
except Exception as access_error:
|
| 154 |
model_status.error(f"Model access check failed: {str(access_error)}")
|
| 155 |
else:
|
|
@@ -185,23 +202,34 @@ def load_model():
|
|
| 185 |
download_progress = st.empty()
|
| 186 |
progress_callback = DownloadProgressCallback(download_progress)
|
| 187 |
|
| 188 |
-
#
|
| 189 |
-
from huggingface_hub import snapshot_download
|
| 190 |
-
|
| 191 |
-
# Download snapshot first to ensure we have all files with progress
|
| 192 |
progress_placeholder.text("Starting model files download...")
|
| 193 |
try:
|
| 194 |
-
# Try to download
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
download_progress.text("Download completed successfully!")
|
| 206 |
except Exception as download_error:
|
| 207 |
download_progress.text(f"Snapshot download error: {str(download_error)}")
|
|
@@ -323,7 +351,11 @@ def load_model():
|
|
| 323 |
if token:
|
| 324 |
# First log in explicitly
|
| 325 |
comp_progress.text("Logging in with token...")
|
| 326 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
# Verify login worked and we have access
|
| 329 |
comp_progress.text("Verifying model access...")
|
|
@@ -331,8 +363,8 @@ def load_model():
|
|
| 331 |
try:
|
| 332 |
# Check if we can access the model
|
| 333 |
with timeout(30): # 30 seconds timeout for API check
|
| 334 |
-
|
| 335 |
-
comp_progress.text(f"Access verified to model: {
|
| 336 |
except Exception as access_error:
|
| 337 |
st.error(f"Authentication succeeded but access verification failed: {str(access_error)}")
|
| 338 |
if "401" in str(access_error) or "access" in str(access_error).lower():
|
|
|
|
| 15 |
import io
|
| 16 |
import time
|
| 17 |
|
| 18 |
+
# Set up imports for huggingface_hub
|
| 19 |
+
# Import what we can, but handle potential import errors
|
| 20 |
+
try:
|
| 21 |
+
from huggingface_hub import HfApi, HfFolder, Repository, create_repo, model_info, hf_hub_download
|
| 22 |
+
from huggingface_hub import login as hf_login
|
| 23 |
+
except ImportError as e:
|
| 24 |
+
st.error(f"Error importing from huggingface_hub: {e}")
|
| 25 |
+
|
| 26 |
+
# Don't rely on specific internal modules that might change between versions
|
| 27 |
+
# We'll handle errors more gracefully instead
|
| 28 |
|
| 29 |
# Configure Hugging Face cache and environment
|
| 30 |
os.environ["HF_HOME"] = os.path.join(os.getcwd(), ".cache/huggingface")
|
|
|
|
| 143 |
|
| 144 |
# Pre-check model availability
|
| 145 |
try:
|
|
|
|
|
|
|
| 146 |
# Try to get token first
|
| 147 |
token = get_hf_token()
|
| 148 |
|
|
|
|
| 150 |
# Check if the model is available in Hugging Face
|
| 151 |
try:
|
| 152 |
model_status.info("Checking model access...")
|
| 153 |
+
|
| 154 |
+
# Use safer HfApi instead of direct model_info if possible
|
| 155 |
+
api = HfApi()
|
| 156 |
+
try:
|
| 157 |
+
info = api.model_info("black-forest-labs/FLUX.1-Fill-dev")
|
| 158 |
+
model_status.success(f"Model verified: {info.id}")
|
| 159 |
+
if hasattr(info, 'tags'):
|
| 160 |
+
model_status.info(f"Model tags: {', '.join(info.tags)}")
|
| 161 |
+
except Exception as api_error:
|
| 162 |
+
# Try fallback with model_info
|
| 163 |
+
try:
|
| 164 |
+
info = model_info("black-forest-labs/FLUX.1-Fill-dev", token=token)
|
| 165 |
+
model_status.success(f"Model verified: {info.id}")
|
| 166 |
+
if hasattr(info, 'tags'):
|
| 167 |
+
model_status.info(f"Model tags: {', '.join(info.tags)}")
|
| 168 |
+
except Exception:
|
| 169 |
+
raise api_error
|
| 170 |
except Exception as access_error:
|
| 171 |
model_status.error(f"Model access check failed: {str(access_error)}")
|
| 172 |
else:
|
|
|
|
| 202 |
download_progress = st.empty()
|
| 203 |
progress_callback = DownloadProgressCallback(download_progress)
|
| 204 |
|
| 205 |
+
# Try pre-downloading files to ensure all components are available
|
|
|
|
|
|
|
|
|
|
| 206 |
progress_placeholder.text("Starting model files download...")
|
| 207 |
try:
|
| 208 |
+
# Try to download individual files one by one instead of snapshot
|
| 209 |
+
# This is more robust across different huggingface_hub versions
|
| 210 |
+
progress_placeholder.text("Downloading model files using hf_hub_download...")
|
| 211 |
+
|
| 212 |
+
# List of essential files to try downloading
|
| 213 |
+
essential_files = [
|
| 214 |
+
"config.json",
|
| 215 |
+
"scheduler/scheduler_config.json",
|
| 216 |
+
"tokenizer/tokenizer_config.json"
|
| 217 |
+
]
|
| 218 |
+
|
| 219 |
+
# Try downloading files one by one
|
| 220 |
+
for file_path in essential_files:
|
| 221 |
+
try:
|
| 222 |
+
progress_placeholder.text(f"Downloading {file_path}...")
|
| 223 |
+
hf_hub_download(
|
| 224 |
+
repo_id="black-forest-labs/FLUX.1-Fill-dev",
|
| 225 |
+
filename=file_path,
|
| 226 |
+
token=token,
|
| 227 |
+
local_dir="model_cache",
|
| 228 |
+
resume_download=True
|
| 229 |
+
)
|
| 230 |
+
except Exception as file_error:
|
| 231 |
+
progress_placeholder.text(f"Error downloading {file_path}: {str(file_error)}")
|
| 232 |
+
# Continue with the next file
|
| 233 |
download_progress.text("Download completed successfully!")
|
| 234 |
except Exception as download_error:
|
| 235 |
download_progress.text(f"Snapshot download error: {str(download_error)}")
|
|
|
|
| 351 |
if token:
|
| 352 |
# First log in explicitly
|
| 353 |
comp_progress.text("Logging in with token...")
|
| 354 |
+
try:
|
| 355 |
+
hf_login(token)
|
| 356 |
+
except Exception as login_error:
|
| 357 |
+
comp_progress.error(f"Login error (non-critical): {str(login_error)}")
|
| 358 |
+
# Continue anyway as the token parameter will be used
|
| 359 |
|
| 360 |
# Verify login worked and we have access
|
| 361 |
comp_progress.text("Verifying model access...")
|
|
|
|
| 363 |
try:
|
| 364 |
# Check if we can access the model
|
| 365 |
with timeout(30): # 30 seconds timeout for API check
|
| 366 |
+
model_info_obj = api.model_info("black-forest-labs/FLUX.1-Fill-dev")
|
| 367 |
+
comp_progress.text(f"Access verified to model: {model_info_obj.id if hasattr(model_info_obj, 'id') else 'black-forest-labs/FLUX.1-Fill-dev'}")
|
| 368 |
except Exception as access_error:
|
| 369 |
st.error(f"Authentication succeeded but access verification failed: {str(access_error)}")
|
| 370 |
if "401" in str(access_error) or "access" in str(access_error).lower():
|