Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,9 +53,36 @@ This app uses the FLUX.1-Fill-dev model which requires special access:
|
|
| 53 |
# Try to get a Hugging Face token from environment variables
|
| 54 |
def get_hf_token():
|
| 55 |
# Check common environment variable names for HF tokens
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return None
|
| 60 |
|
| 61 |
@st.cache_resource
|
|
@@ -73,29 +100,84 @@ def load_model():
|
|
| 73 |
token = get_hf_token()
|
| 74 |
|
| 75 |
st.info("Loading FLUX.1-Fill-dev model...")
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
st.error(f"Error loading model: {str(e)}")
|
| 93 |
-
if "401 Client Error" in str(e):
|
| 94 |
st.error("""
|
| 95 |
Access Denied: You need to:
|
| 96 |
1. Request access to the model at https://huggingface.co/black-forest-labs/FLUX.1-Fill-dev
|
| 97 |
-
2.
|
|
|
|
|
|
|
|
|
|
| 98 |
3. Wait for approval from model owners
|
|
|
|
|
|
|
| 99 |
""")
|
| 100 |
elif "Tried to instantiate class" in str(e):
|
| 101 |
st.error("""
|
|
|
|
| 53 |
# Try to get a Hugging Face token from environment variables
|
| 54 |
def get_hf_token():
|
| 55 |
# Check common environment variable names for HF tokens
|
| 56 |
+
token_env_vars = [
|
| 57 |
+
'HF_TOKEN',
|
| 58 |
+
'HUGGINGFACE_TOKEN',
|
| 59 |
+
'HUGGING_FACE_HUB_TOKEN',
|
| 60 |
+
'HF_API_TOKEN',
|
| 61 |
+
'HUGGINGFACE_API_TOKEN',
|
| 62 |
+
'HUGGINGFACE_HUB_TOKEN'
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
for env_var in token_env_vars:
|
| 66 |
+
if env_var in os.environ and os.environ[env_var].strip():
|
| 67 |
+
st.sidebar.success(f"Found token in {env_var}")
|
| 68 |
+
return os.environ[env_var].strip()
|
| 69 |
+
|
| 70 |
+
# If we're here, no token was found
|
| 71 |
+
st.sidebar.warning("No Hugging Face token found in environment variables")
|
| 72 |
+
st.sidebar.info("Checking for cached credentials...")
|
| 73 |
+
|
| 74 |
+
# Check if there's a cached token
|
| 75 |
+
try:
|
| 76 |
+
from huggingface_hub.constants import HF_TOKEN_PATH
|
| 77 |
+
if os.path.exists(HF_TOKEN_PATH):
|
| 78 |
+
with open(HF_TOKEN_PATH, "r") as f:
|
| 79 |
+
token = f.read().strip()
|
| 80 |
+
if token:
|
| 81 |
+
st.sidebar.success("Found cached Hugging Face token")
|
| 82 |
+
return token
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.sidebar.error(f"Error checking cached token: {str(e)}")
|
| 85 |
+
|
| 86 |
return None
|
| 87 |
|
| 88 |
@st.cache_resource
|
|
|
|
| 100 |
token = get_hf_token()
|
| 101 |
|
| 102 |
st.info("Loading FLUX.1-Fill-dev model...")
|
| 103 |
+
st.info(f"Token available: {'Yes' if token else 'No'}")
|
| 104 |
|
| 105 |
+
try:
|
| 106 |
+
# Try loading with token if available
|
| 107 |
+
if token:
|
| 108 |
+
st.info("Attempting to load model with auth token...")
|
| 109 |
+
# Try the modern token parameter first
|
| 110 |
+
try:
|
| 111 |
+
model = FluxFillPipeline.from_pretrained(
|
| 112 |
+
"black-forest-labs/FLUX.1-Fill-dev",
|
| 113 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 114 |
+
token=token,
|
| 115 |
+
verbose=True
|
| 116 |
+
)
|
| 117 |
+
except Exception as token_error:
|
| 118 |
+
st.info(f"Trying with use_auth_token instead of token: {str(token_error)}")
|
| 119 |
+
# Fall back to older parameter name
|
| 120 |
+
model = FluxFillPipeline.from_pretrained(
|
| 121 |
+
"black-forest-labs/FLUX.1-Fill-dev",
|
| 122 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 123 |
+
use_auth_token=token,
|
| 124 |
+
verbose=True
|
| 125 |
+
)
|
| 126 |
+
else:
|
| 127 |
+
st.info("Attempting to load model without auth token...")
|
| 128 |
+
model = FluxFillPipeline.from_pretrained(
|
| 129 |
+
"black-forest-labs/FLUX.1-Fill-dev",
|
| 130 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 131 |
+
verbose=True # Add verbose output
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
st.success("Model loaded successfully!")
|
| 135 |
+
return model.to(device)
|
| 136 |
+
except Exception as detailed_error:
|
| 137 |
+
st.error(f"Detailed loading error: {str(detailed_error)}")
|
| 138 |
+
|
| 139 |
+
# Try alternative approach - load with huggingface_hub directly
|
| 140 |
+
st.info("Trying alternative loading approach using huggingface_hub...")
|
| 141 |
+
try:
|
| 142 |
+
from huggingface_hub import login, HfApi
|
| 143 |
+
|
| 144 |
+
if token:
|
| 145 |
+
# First log in explicitly
|
| 146 |
+
login(token)
|
| 147 |
+
|
| 148 |
+
# Verify login worked and we have access
|
| 149 |
+
api = HfApi()
|
| 150 |
+
try:
|
| 151 |
+
# Check if we can access the model
|
| 152 |
+
model_info = api.model_info("black-forest-labs/FLUX.1-Fill-dev")
|
| 153 |
+
st.info(f"Successfully authenticated and verified model access: {model_info.id}")
|
| 154 |
+
except Exception as access_error:
|
| 155 |
+
st.error(f"Authentication succeeded but access verification failed: {str(access_error)}")
|
| 156 |
|
| 157 |
+
# Now try loading the model again (should use the login session)
|
| 158 |
+
model = FluxFillPipeline.from_pretrained(
|
| 159 |
+
"black-forest-labs/FLUX.1-Fill-dev",
|
| 160 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 161 |
+
)
|
| 162 |
+
st.success("Successfully loaded model with alternative approach!")
|
| 163 |
+
except Exception as alt_error:
|
| 164 |
+
st.error(f"Alternative loading approach failed: {str(alt_error)}")
|
| 165 |
+
raise
|
| 166 |
+
|
| 167 |
+
return model.to(device)
|
| 168 |
except Exception as e:
|
| 169 |
st.error(f"Error loading model: {str(e)}")
|
| 170 |
+
if "401 Client Error" in str(e) or "401" in str(e) or "access" in str(e).lower() or "denied" in str(e).lower():
|
| 171 |
st.error("""
|
| 172 |
Access Denied: You need to:
|
| 173 |
1. Request access to the model at https://huggingface.co/black-forest-labs/FLUX.1-Fill-dev
|
| 174 |
+
2. Set up your Hugging Face token in Spaces:
|
| 175 |
+
- Go to your Space settings > Secrets
|
| 176 |
+
- Add a new secret with name 'HF_TOKEN'
|
| 177 |
+
- Set its value to your Hugging Face API token
|
| 178 |
3. Wait for approval from model owners
|
| 179 |
+
|
| 180 |
+
Note: You can find your token at https://huggingface.co/settings/tokens
|
| 181 |
""")
|
| 182 |
elif "Tried to instantiate class" in str(e):
|
| 183 |
st.error("""
|