rjarun20 commited on
Commit
81ba8a4
Β·
verified Β·
1 Parent(s): cdc6f25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -46,7 +46,7 @@ logger = logging.getLogger(__name__)
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
@@ -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 for Spaces...")
98
- client = InferenceClient(token=api_token)
 
 
 
 
 
99
 
100
- # Simple connectivity test (optional)
101
  logger.info("πŸ§ͺ Testing client connectivity...")
102
  try:
103
- # Try fill-mask with bert-base-uncased (more likely to be available)
104
- test_result = client.fill_mask("Paris is the [MASK] of France.", model="bert-base-uncased")
105
- logger.info(f"βœ… Client test successful with bert-base-uncased - got {len(test_result)} results")
 
 
 
106
  except Exception as e1:
107
  try:
108
- # Fallback: try text classification (almost always available)
109
- test_result = client.text_classification("This is a test.", model="distilbert-base-uncased-finetuned-sst-2-english")
110
- logger.info(f"βœ… Client test successful with sentiment model")
 
 
 
111
  except Exception as e2:
112
- # If tests fail, client is still valid - just skip testing
113
- logger.info("βœ… Client initialized (skipping model tests - they may be loading)")
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
+