File size: 2,273 Bytes
49cba45
 
bc3f282
 
 
 
 
 
49cba45
bc3f282
 
49cba45
bc3f282
 
 
 
 
 
 
 
 
49cba45
bc3f282
 
49cba45
bc3f282
 
49cba45
bc3f282
 
 
 
 
 
49cba45
bc3f282
 
49cba45
 
bc3f282
 
 
49cba45
bc3f282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49cba45
bc3f282
 
 
 
 
49b5b01
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Realizado por Leonardo Vannoni Lorenzo para el curso de Deep Learning de INTEC, 1105795

import gradio as gr
from transformers import pipeline
import torch
from gtts import gTTS
from PyPDF2 import PdfReader

# --- Usar GPU si esta disponible ---
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
# --- Resumidor multilingual ---
summarizer = pipeline(
    "summarization",
    model="csebuetnlp/mT5_multilingual_XLSum",
    tokenizer="csebuetnlp/mT5_multilingual_XLSum",
    device=0 if torch.cuda.is_available() else -1
)
 
def summarize_and_speak(input_type, text_input, pdf_input):
    """
    Resumir el input y devolver mensaje hablado.
    """
    try:
        if input_type == "text":                # Resumir el cuadro de texto
            text = text_input
        elif input_type == "pdf":
            reader = PdfReader(pdf_input.name)  # Resumir el PDF
            text = ""
            for page in reader.pages:
                text += page.extract_text()
        else:
            raise ValueError("Invalid input type. Choose 'text' or 'pdf'.")
 
        # --- Usando el modelo de summarize ---
        summary = summarizer(
            text,
            max_length=2500,
            min_length=500,
            do_sample=False
        )[0]["summary_text"]
 
        tts = gTTS(text=summary, lang='es')     
        tts.save("summary.mp3")
        return summary, "summary.mp3"
    except Exception as e:
        return f"An error occurred: {e}", None
 
with gr.Blocks() as demo:
    gr.Markdown("## Resumen de Historias con Voz")
   
    with gr.Tab("Texto"):
        text_input = gr.Textbox(label="Introduce tu historia aquí")
    with gr.Tab("PDF"):
        pdf_input = gr.File(label="Sube tu archivo PDF", type="filepath")
   
    with gr.Row():
        text_output = gr.Textbox(label="Resumen")
        audio_output = gr.Audio(label="Resumen en Audio")
 
    submit_btn = gr.Button("Resumir y Convertir a Voz")
   
    input_type = gr.components.Radio(choices=["text", "pdf"], label="Tipo de entrada")
    input_type.value = "text"  
 
    submit_btn.click(fn=summarize_and_speak,
                     inputs=[input_type, text_input, pdf_input],
                     outputs=[text_output, audio_output])
 
demo.launch(share=True)