Spaces:
Sleeping
Sleeping
File size: 1,933 Bytes
3bde652 f00ce12 8cddfb4 f00ce12 1f130f9 5e969ae 4e68eb9 8cddfb4 f00ce12 4e68eb9 656bd99 3bde652 8cddfb4 3bde652 1f00c37 bdbf589 1f00c37 3bde652 1f00c37 3bde652 f00ce12 3bde652 1f00c37 3bde652 162bb59 a9c859f 162bb59 8c0b565 8cddfb4 |
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 |
import gradio as gr
from transformers import pipeline, T5Tokenizer, T5ForConditionalGeneration
import requests
import os
# Cargar el token de Hugging Face configurado en el espacio
token = os.getenv("HF_TOKEN")
if not token:
raise ValueError("El token no se configuró correctamente en las variables de entorno del Espacio.")
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, legacy=False)
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):
# Dividir el texto en partes si es demasiado largo para el modelo de resumen
max_input_length = 512
text_chunks = [text[i:i+max_input_length] for i in range(0, len(text), max_input_length)]
# Resumir cada parte y unir los resúmenes
summaries = [summarizer(chunk, max_length=30, min_length=10, do_sample=False)[0]['summary_text'] for chunk in text_chunks]
full_summary = " ".join(summaries)
# Realizar la traducción del resumen completo utilizando la API de Hugging Face
response = requests.post(API_URL, headers=headers, json={"inputs": full_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"
)
# Lanzar la interfaz
demo.launch()
|