Manu
commited on
Commit
·
89875d4
1
Parent(s):
10e69ed
added hf_utils.py
Browse files- hf_utils.py +20 -0
hf_utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
def hf_validate_api_token(api_token):
|
| 4 |
+
|
| 5 |
+
# Define la URL de la API
|
| 6 |
+
url = "https://huggingface.co/api/whoami-v2"
|
| 7 |
+
|
| 8 |
+
# Define los encabezados de la solicitud
|
| 9 |
+
headers = {
|
| 10 |
+
"Authorization": f"Bearer {api_token}"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Realiza la solicitud a la API
|
| 14 |
+
response = requests.get(url, headers=headers)
|
| 15 |
+
|
| 16 |
+
# Si la respuesta tiene un código de estado 200, el token es válido
|
| 17 |
+
if response.status_code == 200:
|
| 18 |
+
return True, "Welcome " + response.json()['fullname'] + "! Thank you for trying this mini-app!"
|
| 19 |
+
else:
|
| 20 |
+
return False, response.json()['error']
|