rjarun20 commited on
Commit
682dc4d
Β·
verified Β·
1 Parent(s): fe46b35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
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
- # Try multiple token sources for Spaces
55
- api_token = None
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
- pass
 
 
 
 
77
 
78
  if not api_token:
79
- logger.warning("⚠️ No HF token found. Please set HF_API_TOKEN or HF_TOKEN in Spaces secrets.")
 
 
 
 
 
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
+