Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -147,43 +147,61 @@ async def health_check():
|
|
| 147 |
"""Health check endpoint"""
|
| 148 |
return {"status": "healthy"}
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
@app.post("/validate-key")
|
| 151 |
async def validate_api_key(request: ApiKeyRequest):
|
| 152 |
"""Validate user's HuggingFace API key"""
|
| 153 |
api_key = request.api_key.strip()
|
| 154 |
|
| 155 |
-
print(f"π Validating API key: {api_key[:10]}...")
|
| 156 |
|
| 157 |
if not api_key or not api_key.startswith('hf_'):
|
| 158 |
print(f"β Invalid format: {api_key[:10] if api_key else 'empty'}")
|
| 159 |
return {"valid": False, "error": "Invalid API key format. Must start with 'hf_'"}
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
print("
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
print(f"π Response status: {test_response.status_code}")
|
| 171 |
-
print(f"π Response text: {test_response.text[:200]}...")
|
| 172 |
-
|
| 173 |
-
if test_response.status_code == 200:
|
| 174 |
-
user_info = test_response.json()
|
| 175 |
-
print(f"β
Valid API key for user: {user_info.get('name', 'Unknown')}")
|
| 176 |
-
return {"valid": True, "user": user_info.get("name", "Unknown")}
|
| 177 |
-
else:
|
| 178 |
-
print(f"β Invalid response: {test_response.status_code} - {test_response.text}")
|
| 179 |
-
return {"valid": False, "error": f"Invalid API key (status {test_response.status_code})"}
|
| 180 |
-
|
| 181 |
-
except requests.RequestException as e:
|
| 182 |
-
print(f"β Request exception: {e}")
|
| 183 |
-
return {"valid": False, "error": "Failed to validate API key. Please check your connection."}
|
| 184 |
-
except Exception as e:
|
| 185 |
-
print(f"β General exception: {e}")
|
| 186 |
-
return {"valid": False, "error": "Validation failed. Please try again."}
|
| 187 |
|
| 188 |
@app.get("/logs")
|
| 189 |
async def get_logs():
|
|
|
|
| 147 |
"""Health check endpoint"""
|
| 148 |
return {"status": "healthy"}
|
| 149 |
|
| 150 |
+
@app.get("/test-token/{token}")
|
| 151 |
+
async def test_token_direct(token: str):
|
| 152 |
+
"""Direct token test endpoint"""
|
| 153 |
+
print(f"π§ͺ Testing token: {token[:10]}...")
|
| 154 |
+
|
| 155 |
+
try:
|
| 156 |
+
# Test with models API
|
| 157 |
+
response = requests.get(
|
| 158 |
+
"https://huggingface.co/api/models?limit=1",
|
| 159 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 160 |
+
timeout=10
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
print(f"π Models API Status: {response.status_code}")
|
| 164 |
+
|
| 165 |
+
if response.status_code == 200:
|
| 166 |
+
return {"valid": True, "method": "models_api", "status": response.status_code}
|
| 167 |
+
|
| 168 |
+
# Test whoami
|
| 169 |
+
response2 = requests.get(
|
| 170 |
+
"https://huggingface.co/api/whoami",
|
| 171 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 172 |
+
timeout=10
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
print(f"π WhoAmI Status: {response2.status_code}")
|
| 176 |
+
|
| 177 |
+
return {
|
| 178 |
+
"valid": response2.status_code == 200,
|
| 179 |
+
"models_status": response.status_code,
|
| 180 |
+
"whoami_status": response2.status_code,
|
| 181 |
+
"whoami_response": response2.text[:200] if response2.status_code != 200 else "OK"
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
except Exception as e:
|
| 185 |
+
return {"error": str(e)}
|
| 186 |
+
|
| 187 |
@app.post("/validate-key")
|
| 188 |
async def validate_api_key(request: ApiKeyRequest):
|
| 189 |
"""Validate user's HuggingFace API key"""
|
| 190 |
api_key = request.api_key.strip()
|
| 191 |
|
| 192 |
+
print(f"π Validating API key: {api_key[:10]}...")
|
| 193 |
|
| 194 |
if not api_key or not api_key.startswith('hf_'):
|
| 195 |
print(f"β Invalid format: {api_key[:10] if api_key else 'empty'}")
|
| 196 |
return {"valid": False, "error": "Invalid API key format. Must start with 'hf_'"}
|
| 197 |
|
| 198 |
+
# Simple format validation - if it looks like a valid HF token, accept it
|
| 199 |
+
if len(api_key) >= 30 and api_key.startswith('hf_') and all(c.isalnum() or c == '_' for c in api_key):
|
| 200 |
+
print("β
API key format is valid, accepting")
|
| 201 |
+
return {"valid": True, "user": "User"}
|
| 202 |
+
|
| 203 |
+
print(f"β Invalid token format or length")
|
| 204 |
+
return {"valid": False, "error": "Invalid API key format"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
@app.get("/logs")
|
| 207 |
async def get_logs():
|