Spaces:
Runtime error
Runtime error
File size: 3,878 Bytes
94492e5 99b2259 94492e5 b903474 94492e5 b903474 94492e5 99b2259 94492e5 b903474 94492e5 b903474 94492e5 b903474 94492e5 b903474 94492e5 99b2259 94492e5 b903474 94492e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#!/usr/bin/env python3
"""
SmolAgents Authentication Fix
Resolves 401 "Invalid username or password" errors
"""
import os
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, InferenceClientModel
# Method 1: Explicit token (most reliable)
def create_agent_with_token():
"""Create agent with explicit HF token - most reliable method"""
# Replace with your actual HuggingFace token
hf_token = "hf_xxxxxxxxxxxxxxxxxxxxxxxxxx"
model = HfApiModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
token=hf_token
)
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=model
)
return agent
# Method 2: Environment variable (recommended for production)
def create_agent_with_env_var():
"""Create agent using HF_TOKEN environment variable"""
# Set environment variable first:
# export HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxx"
# Verify token is set
if not os.getenv("HF_TOKEN"):
raise ValueError("HF_TOKEN environment variable not set!")
model = HfApiModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct"
# token will be read from HF_TOKEN automatically
)
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=model
)
return agent
# Method 3: InferenceClientModel (newest approach, 2025)
def create_agent_with_inference_client():
"""Create agent using newer InferenceClientModel - better error handling"""
hf_token = "hf_xxxxxxxxxxxxxxxxxxxxxxxxxx"
model = InferenceClientModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
token=hf_token
)
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=model
)
return agent
# Method 4: Login first (alternative approach)
def create_agent_with_login():
"""Create agent after logging in to HuggingFace Hub"""
from huggingface_hub import login
# Login to HF (will prompt for token if not provided)
login(token="hf_xxxxxxxxxxxxxxxxxxxxxxxxxx")
# Now HfApiModel should work without explicit token
model = HfApiModel(
model_id="Qwen/Qwen2.5-Coder-32B-Instruct"
)
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=model
)
return agent
def test_agent(agent):
"""Test the agent with a simple query"""
try:
result = agent.run("What is the current time?")
print("β
Success! Agent is working properly.")
print(f"Result: {result}")
return True
except Exception as e:
print(f"β Error: {e}")
return False
if __name__ == "__main__":
print("Testing SmolAgents authentication fixes...\n")
# Try Method 1 first (most common solution)
print("Method 1: Explicit token")
try:
agent = create_agent_with_token()
if test_agent(agent):
print("β
Method 1 successful!\n")
else:
print("β Method 1 failed, trying next method...\n")
except Exception as e:
print(f"β Method 1 failed: {e}\n")
# Try Method 3 (newest approach)
print("Method 3: InferenceClientModel")
try:
agent = create_agent_with_inference_client()
if test_agent(agent):
print("β
Method 3 successful!\n")
except Exception as e:
print(f"β Method 3 failed: {e}\n")
# Token Requirements Checklist:
"""
Your HuggingFace token must have these permissions:
β
"Make calls to the serverless Inference API"
β
"Read access to contents of all public gated repos" (for gated models)
To get your token:
1. Go to: https://huggingface.co/settings/tokens
2. Click "New token"
3. Select "Write" permissions
4. Copy the token (starts with hf_)
Common issues:
- Token is expired or revoked
- Token lacks proper permissions
- Model is gated and requires special access
- Network/firewall blocking HF API calls
"""
|