Spaces:
Sleeping
Sleeping
File size: 2,167 Bytes
3bde652 6771b9a f00ce12 3bde652 5e969ae 4e68eb9 3bde652 f00ce12 3bde652 f00ce12 4e68eb9 656bd99 3bde652 f00ce12 3bde652 656bd99 3bde652 162bb59 |
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import gradio as gr
from transformers import pipeline, T5Tokenizer, T5ForConditionalGeneration
import os
import requests
# Configura tu token de Hugging Face directamente aquí
token = os.getenv("HF_TOKEN")
if not token:
raise ValueError("El token no se configuró. Asegúrate de ingresarlo correctamente.")
# Configuración de los headers para la API con el token
API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-en-es"
headers = {"Authorization": f"Bearer {token}"}
# Cargar el modelo y el tokenizador de resumen
tokenizer = T5Tokenizer.from_pretrained("sumedh/t5-base-amazonreviews", clean_up_tokenization_spaces=True)
model = T5ForConditionalGeneration.from_pretrained("sumedh/t5-base-amazonreviews")
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
# Función para realizar el resumen y la traducción
def texto_sum(text):
# Resumir el texto de entrada
summary = summarizer(text, do_sample=False)[0]['summary_text']
# Realizar la traducción del resumen utilizando la API de Hugging Face
response = requests.post(API_URL, headers=headers, json={"inputs": summary})
translation = response.json()
# Verificar si hay errores en la respuesta de la traducción
if 'error' in translation:
return f"Error en la traducción: {translation['error']}"
return translation[0]['translation_text']
# Interfaz de Gradio
demo = gr.Interface(
fn=texto_sum,
inputs=gr.Textbox(label="Texto a introducir:", placeholder="Introduce el texto a resumir aquí..."),
outputs="text"
)
# Probar el token en una solicitud simple para verificar su validez
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()
# Lanzar la interfaz
demo.launch() |