Update app.py
Browse files
app.py
CHANGED
@@ -46,7 +46,7 @@ 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
|
@@ -58,7 +58,6 @@ def get_client():
|
|
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 |
|
@@ -80,10 +79,6 @@ def get_client():
|
|
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
|
@@ -94,23 +89,34 @@ def get_client():
|
|
94 |
return None
|
95 |
|
96 |
try:
|
97 |
-
logger.info("π Initializing HuggingFace client
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
#
|
101 |
logger.info("π§ͺ Testing client connectivity...")
|
102 |
try:
|
103 |
-
#
|
104 |
-
test_result = client.fill_mask(
|
105 |
-
|
|
|
|
|
|
|
106 |
except Exception as e1:
|
107 |
try:
|
108 |
-
# Fallback
|
109 |
-
test_result = client.text_classification(
|
110 |
-
|
|
|
|
|
|
|
111 |
except Exception as e2:
|
112 |
-
# If tests fail, client
|
113 |
-
logger.info("β
Client initialized (
|
114 |
|
115 |
logger.info("β
HuggingFace client ready for use")
|
116 |
return client
|
@@ -689,4 +695,5 @@ if __name__ == "__main__":
|
|
689 |
server_port=7860,
|
690 |
show_error=True,
|
691 |
quiet=False
|
692 |
-
)
|
|
|
|
46 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
47 |
|
48 |
def get_client():
|
49 |
+
"""Spaces-optimized client initialization using official HF Inference provider."""
|
50 |
if not HF_AVAILABLE:
|
51 |
logger.error("β HuggingFace Hub not available")
|
52 |
return None
|
|
|
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 |
else:
|
62 |
logger.warning("β HF_API_TOKEN not found in environment")
|
63 |
|
|
|
79 |
except Exception as e:
|
80 |
logger.warning(f"β CLI token check failed: {e}")
|
81 |
|
|
|
|
|
|
|
|
|
82 |
if not api_token:
|
83 |
logger.error("β No HF token found in any location")
|
84 |
return None
|
|
|
89 |
return None
|
90 |
|
91 |
try:
|
92 |
+
logger.info("π Initializing HuggingFace client with hf-inference provider...")
|
93 |
+
|
94 |
+
# FIXED: Use the official provider-based approach
|
95 |
+
client = InferenceClient(
|
96 |
+
provider="hf-inference",
|
97 |
+
api_key=api_token,
|
98 |
+
)
|
99 |
|
100 |
+
# Test with official example from documentation
|
101 |
logger.info("π§ͺ Testing client connectivity...")
|
102 |
try:
|
103 |
+
# Use official fill_mask example from docs
|
104 |
+
test_result = client.fill_mask(
|
105 |
+
"The answer to the universe is [MASK].",
|
106 |
+
model="google-bert/bert-base-uncased",
|
107 |
+
)
|
108 |
+
logger.info(f"β
Client test successful - got {len(test_result)} results")
|
109 |
except Exception as e1:
|
110 |
try:
|
111 |
+
# Fallback test with text classification
|
112 |
+
test_result = client.text_classification(
|
113 |
+
"I like you. I love you",
|
114 |
+
model="NousResearch/Minos-v1",
|
115 |
+
)
|
116 |
+
logger.info(f"β
Client test successful with text classification")
|
117 |
except Exception as e2:
|
118 |
+
# If tests fail, client might still work - just log
|
119 |
+
logger.info("β
Client initialized (model tests may be loading)")
|
120 |
|
121 |
logger.info("β
HuggingFace client ready for use")
|
122 |
return client
|
|
|
695 |
server_port=7860,
|
696 |
show_error=True,
|
697 |
quiet=False
|
698 |
+
)
|
699 |
+
|