Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -118,33 +118,75 @@ class Gemma3AIManager:
|
|
| 118 |
return False
|
| 119 |
|
| 120 |
def _test_token_authentication_safe(self, token):
|
| 121 |
-
"""Testar autenticação de forma segura"""
|
| 122 |
try:
|
| 123 |
print(f" 🧪 Testando token {token[:10]}...{token[-4:]}...")
|
| 124 |
-
|
| 125 |
import requests
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
print(f" ❌ Test error: {e}")
|
| 150 |
return False
|
|
|
|
| 118 |
return False
|
| 119 |
|
| 120 |
def _test_token_authentication_safe(self, token):
|
| 121 |
+
"""Testar autenticação de forma segura - VERSÃO CORRIGIDA"""
|
| 122 |
try:
|
| 123 |
print(f" 🧪 Testando token {token[:10]}...{token[-4:]}...")
|
| 124 |
+
|
| 125 |
import requests
|
| 126 |
+
|
| 127 |
+
# TESTE 1: Verificar se token é válido usando whoami
|
| 128 |
+
try:
|
| 129 |
+
whoami_response = requests.get(
|
| 130 |
+
'https://huggingface.co/api/whoami',
|
| 131 |
+
headers={'Authorization': f'Bearer {token}'},
|
| 132 |
+
timeout=10
|
| 133 |
+
)
|
| 134 |
|
| 135 |
+
if whoami_response.status_code == 200:
|
| 136 |
+
print(" ✅ Token válido - whoami success!")
|
| 137 |
+
user_info = whoami_response.json()
|
| 138 |
+
print(f" 👤 Usuário: {user_info.get('name', 'Unknown')}")
|
| 139 |
|
| 140 |
+
# TESTE 2: Tentar login direto do HuggingFace Hub
|
| 141 |
+
try:
|
| 142 |
+
from huggingface_hub import login
|
| 143 |
+
login(token=token, add_to_git_credential=False)
|
| 144 |
+
print(" 🎉 HF hub login success!")
|
| 145 |
+
return True
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f" ⚠️ HF hub login warning: {e}")
|
| 148 |
+
# Mesmo com warning, token pode estar funcionando
|
| 149 |
+
return True
|
| 150 |
+
|
| 151 |
+
else:
|
| 152 |
+
print(f" ❌ Whoami failed: {whoami_response.status_code}")
|
| 153 |
|
| 154 |
+
except Exception as whoami_error:
|
| 155 |
+
print(f" ⚠️ Whoami error: {whoami_error}")
|
| 156 |
+
|
| 157 |
+
# TESTE 3: Fallback - testar com endpoint inference direto
|
| 158 |
+
try:
|
| 159 |
+
# Testar com endpoint mais simples
|
| 160 |
+
simple_response = requests.post(
|
| 161 |
+
'https://api-inference.huggingface.co/models/gpt2',
|
| 162 |
+
headers={'Authorization': f'Bearer {token}'},
|
| 163 |
+
json={'inputs': 'Hello'},
|
| 164 |
+
timeout=15
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
if simple_response.status_code == 200:
|
| 168 |
+
print(" ✅ Inference API success!")
|
| 169 |
+
return True
|
| 170 |
+
elif simple_response.status_code == 503:
|
| 171 |
+
print(" ⏳ Model loading - token válido!")
|
| 172 |
+
return True
|
| 173 |
+
else:
|
| 174 |
+
print(f" ❌ Inference failed: {simple_response.status_code}")
|
| 175 |
|
| 176 |
+
except Exception as inference_error:
|
| 177 |
+
print(f" ⚠️ Inference error: {inference_error}")
|
| 178 |
+
|
| 179 |
+
# TESTE 4: Apenas verificar se HuggingFace Hub reconhece o token
|
| 180 |
+
try:
|
| 181 |
+
from huggingface_hub import whoami
|
| 182 |
+
user_info = whoami(token=token)
|
| 183 |
+
print(f" ✅ Hub whoami success: {user_info['name']}")
|
| 184 |
+
return True
|
| 185 |
+
except Exception as hub_error:
|
| 186 |
+
print(f" ❌ Hub whoami failed: {hub_error}")
|
| 187 |
+
|
| 188 |
+
return False
|
| 189 |
+
|
| 190 |
except Exception as e:
|
| 191 |
print(f" ❌ Test error: {e}")
|
| 192 |
return False
|