Skip to content

Commit 1df9278

Browse files
committed
Improve HF token usage
1 parent b3f0b0e commit 1df9278

File tree

6 files changed

+85
-529
lines changed

6 files changed

+85
-529
lines changed

docs/guides/troubleshooting.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ os.environ["LOCALLAB_QUANTIZATION_TYPE"] = "int8"
4444
os.environ["HUGGINGFACE_MODEL"] = "microsoft/phi-2" # Smaller model
4545
```
4646

47+
### Authentication Issues
48+
49+
**Issue:** HuggingFace Authentication Error
50+
```
51+
ERROR: Failed to load model: Invalid credentials in Authorization header
52+
```
53+
**Solution:**
54+
1. Get a HuggingFace token from [HuggingFace tokens page](https://huggingface.co/settings/tokens)
55+
2. Set the token in one of these ways:
56+
```python
57+
# Option 1: Environment variable
58+
os.environ["HUGGINGFACE_TOKEN"] = "your_token_here"
59+
60+
# Option 2: Configuration file
61+
from locallab.cli.config import set_config_value
62+
set_config_value("huggingface_token", "your_token_here")
63+
```
64+
3. Restart the LocalLab server
65+
66+
Note: Some models like microsoft/phi-2 require authentication to download.
67+
4768
## Related Documentation
4869
- [Getting Started](./getting-started.md)
4970
- [Performance Guide](./features/performance.md)

locallab/cli/interactive.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,6 @@ def get_missing_required_env_vars() -> List[str]:
4242
def prompt_for_config(use_ngrok: bool = None, port: int = None, ngrok_auth_token: str = None, force_reconfigure: bool = False) -> Dict[str, Any]:
4343
"""
4444
Interactive prompt for configuration
45-
46-
Args:
47-
use_ngrok: Whether to use ngrok
48-
port: Port to run the server on
49-
ngrok_auth_token: Ngrok authentication token
50-
force_reconfigure: Whether to force reconfiguration of all settings
51-
52-
Returns:
53-
Dict of configuration values
5445
"""
5546
# Import here to avoid circular imports
5647
from .config import load_config, get_config_value
@@ -72,6 +63,28 @@ def prompt_for_config(use_ngrok: bool = None, port: int = None, ngrok_auth_token
7263
# Determine if we're in Colab
7364
in_colab = is_in_colab()
7465

66+
# If in Colab, use simplified configuration
67+
if in_colab:
68+
# Set default values for Colab environment
69+
config.setdefault("port", 8000)
70+
config.setdefault("use_ngrok", True)
71+
config.setdefault("model_id", os.environ.get("HUGGINGFACE_MODEL", DEFAULT_MODEL))
72+
73+
# Use ngrok token from environment if available
74+
if os.environ.get("NGROK_AUTH_TOKEN"):
75+
config["ngrok_auth_token"] = os.environ.get("NGROK_AUTH_TOKEN")
76+
elif ngrok_auth_token:
77+
config["ngrok_auth_token"] = ngrok_auth_token
78+
79+
# Set some reasonable defaults for Colab
80+
config.setdefault("enable_quantization", True)
81+
config.setdefault("quantization_type", "int8")
82+
config.setdefault("enable_attention_slicing", True)
83+
config.setdefault("enable_flash_attention", True)
84+
config.setdefault("enable_better_transformer", True)
85+
86+
return config
87+
7588
# Check for GPU
7689
has_gpu = False
7790
gpu_memory = get_gpu_memory()

locallab/core/app.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ async def startup_event():
8181
"""Initialization tasks when the server starts"""
8282
logger.info("Starting LocalLab server...")
8383

84+
# Get HuggingFace token and set it in environment if available
85+
from ..config import get_hf_token
86+
hf_token = get_hf_token(interactive=False)
87+
if hf_token:
88+
os.environ["HUGGINGFACE_TOKEN"] = hf_token
89+
logger.info("HuggingFace token loaded from configuration")
90+
else:
91+
logger.warning("No HuggingFace token found. Some models may not be accessible.")
92+
8493
# Initialize cache if available
8594
if FASTAPI_CACHE_AVAILABLE:
8695
FastAPICache.init(InMemoryBackend(), prefix="locallab-cache")
@@ -182,6 +191,15 @@ async def load_model_in_background(model_id: str):
182191
start_time = time.time()
183192

184193
try:
194+
# Ensure HF token is set before loading model
195+
from ..config import get_hf_token
196+
hf_token = get_hf_token(interactive=False)
197+
if hf_token:
198+
os.environ["HUGGINGFACE_TOKEN"] = hf_token
199+
logger.debug("Using HuggingFace token from configuration")
200+
else:
201+
logger.warning("No HuggingFace token found. Some models may not be accessible.")
202+
185203
# Wait for the model to load
186204
await model_manager.load_model(model_id)
187205

@@ -191,4 +209,7 @@ async def load_model_in_background(model_id: str):
191209
# We don't need to call log_model_loaded here since it's already done in the model_manager
192210
logger.info(f"{Fore.GREEN}Model {model_id} loaded successfully in {load_time:.2f} seconds!{Style.RESET_ALL}")
193211
except Exception as e:
194-
logger.error(f"Failed to load model {model_id}: {str(e)}")
212+
logger.error(f"Failed to load model {model_id}: {str(e)}")
213+
if "401 Client Error: Unauthorized" in str(e):
214+
logger.error("This appears to be an authentication error. Please ensure your HuggingFace token is set correctly.")
215+
logger.info("You can set your token using: locallab config")

0 commit comments

Comments
 (0)