Update app.py
Browse files
app.py
CHANGED
@@ -46,24 +46,29 @@ logger = logging.getLogger(__name__)
|
|
46 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
47 |
|
48 |
def get_client():
|
49 |
-
"""Spaces-optimized client initialization."""
|
50 |
if not HF_AVAILABLE:
|
51 |
logger.error("β HuggingFace Hub not available")
|
52 |
return None
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
|
57 |
# Method 1: Check HF_API_TOKEN (your current setup)
|
58 |
api_token = os.getenv("HF_API_TOKEN")
|
59 |
if api_token:
|
60 |
-
logger.info("β
Found HF_API_TOKEN")
|
|
|
|
|
|
|
61 |
|
62 |
# Method 2: Fallback to HF_TOKEN
|
63 |
if not api_token:
|
64 |
api_token = os.getenv("HF_TOKEN")
|
65 |
if api_token:
|
66 |
-
logger.info("β
Found HF_TOKEN")
|
|
|
|
|
67 |
|
68 |
# Method 3: Hugging Face CLI token (if logged in)
|
69 |
if not api_token:
|
@@ -71,12 +76,21 @@ def get_client():
|
|
71 |
from huggingface_hub import get_token
|
72 |
api_token = get_token()
|
73 |
if api_token:
|
74 |
-
logger.info("β
Found CLI token")
|
75 |
-
except:
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
if not api_token:
|
79 |
-
logger.
|
|
|
|
|
|
|
|
|
|
|
80 |
return None
|
81 |
|
82 |
try:
|
@@ -86,12 +100,13 @@ def get_client():
|
|
86 |
# Quick test with a lightweight model
|
87 |
logger.info("π§ͺ Testing client connectivity...")
|
88 |
test_result = client.fill_mask("Paris is the [MASK] of France.", model="distilbert-base-uncased")
|
89 |
-
logger.info(f"β
Client test successful")
|
90 |
|
91 |
return client
|
92 |
|
93 |
except Exception as e:
|
94 |
logger.error(f"β Client initialization failed: {e}")
|
|
|
95 |
return None
|
96 |
|
97 |
# Initialize client globally for Spaces
|
@@ -646,4 +661,5 @@ if __name__ == "__main__":
|
|
646 |
server_port=7860,
|
647 |
show_error=True,
|
648 |
quiet=False
|
649 |
-
)
|
|
|
|
46 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
47 |
|
48 |
def get_client():
|
49 |
+
"""Spaces-optimized client initialization with debug logging."""
|
50 |
if not HF_AVAILABLE:
|
51 |
logger.error("β HuggingFace Hub not available")
|
52 |
return None
|
53 |
|
54 |
+
# Debug: Check all environment variables for tokens
|
55 |
+
logger.info("π Debugging token detection...")
|
56 |
|
57 |
# Method 1: Check HF_API_TOKEN (your current setup)
|
58 |
api_token = os.getenv("HF_API_TOKEN")
|
59 |
if api_token:
|
60 |
+
logger.info(f"β
Found HF_API_TOKEN (length: {len(api_token)})")
|
61 |
+
logger.info(f"β
Token starts with: {api_token[:10]}..." if len(api_token) > 10 else "β
Token found but very short")
|
62 |
+
else:
|
63 |
+
logger.warning("β HF_API_TOKEN not found in environment")
|
64 |
|
65 |
# Method 2: Fallback to HF_TOKEN
|
66 |
if not api_token:
|
67 |
api_token = os.getenv("HF_TOKEN")
|
68 |
if api_token:
|
69 |
+
logger.info(f"β
Found HF_TOKEN (length: {len(api_token)})")
|
70 |
+
else:
|
71 |
+
logger.warning("β HF_TOKEN not found in environment")
|
72 |
|
73 |
# Method 3: Hugging Face CLI token (if logged in)
|
74 |
if not api_token:
|
|
|
76 |
from huggingface_hub import get_token
|
77 |
api_token = get_token()
|
78 |
if api_token:
|
79 |
+
logger.info(f"β
Found CLI token (length: {len(api_token)})")
|
80 |
+
except Exception as e:
|
81 |
+
logger.warning(f"β CLI token check failed: {e}")
|
82 |
+
|
83 |
+
# Debug: Show all environment variables that start with HF_
|
84 |
+
hf_env_vars = {k: v[:10] + "..." if v else None for k, v in os.environ.items() if k.startswith('HF_')}
|
85 |
+
logger.info(f"π All HF_ environment variables: {hf_env_vars}")
|
86 |
|
87 |
if not api_token:
|
88 |
+
logger.error("β No HF token found in any location")
|
89 |
+
return None
|
90 |
+
|
91 |
+
# Validate token format
|
92 |
+
if not api_token.startswith('hf_'):
|
93 |
+
logger.warning(f"β οΈ Token doesn't start with 'hf_': {api_token[:10]}...")
|
94 |
return None
|
95 |
|
96 |
try:
|
|
|
100 |
# Quick test with a lightweight model
|
101 |
logger.info("π§ͺ Testing client connectivity...")
|
102 |
test_result = client.fill_mask("Paris is the [MASK] of France.", model="distilbert-base-uncased")
|
103 |
+
logger.info(f"β
Client test successful - got {len(test_result)} results")
|
104 |
|
105 |
return client
|
106 |
|
107 |
except Exception as e:
|
108 |
logger.error(f"β Client initialization failed: {e}")
|
109 |
+
logger.error(f"β Token used: {api_token[:15]}..." if api_token else "No token")
|
110 |
return None
|
111 |
|
112 |
# Initialize client globally for Spaces
|
|
|
661 |
server_port=7860,
|
662 |
show_error=True,
|
663 |
quiet=False
|
664 |
+
)
|
665 |
+
|