File size: 972 Bytes
f00ce12
4e68eb9
f00ce12
4e68eb9
133a6e0
4e68eb9
 
f00ce12
4e68eb9
f00ce12
4e68eb9
656bd99
4e68eb9
 
 
 
f00ce12
4e68eb9
 
 
 
656bd99
4e68eb9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import requests
import os

# Configurar el token de Hugging Face como variable de entorno
token = os.getenv("HF_TOKEN")
if not token:
    raise ValueError("El token HF_TOKEN no se encontró en las variables de entorno. Asegúrate de configurarlo correctamente.")

# Configuración de headers para la API de traducción
API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es"
headers = {"Authorization": f"Bearer {token}"}

# Prueba de traducción para verificar el token
def test_translation_api():
    test_response = requests.post(API_URL, headers=headers, json={"inputs": "Hello, how are you?"})
    response_data = test_response.json()
    
    # Comprobar si hay errores en la respuesta
    if 'error' in response_data:
        raise ValueError(f"Error en la API de traducción: {response_data['error']}")
    print("Token válido y API accesible. Respuesta de prueba:", response_data)

# Ejecutar la prueba de token
test_translation_api()