|
|
|
|
|
import gradio as gr |
|
from transformers import pipeline |
|
import torch |
|
from gtts import gTTS |
|
from PyPDF2 import PdfReader |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
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": |
|
text = text_input |
|
elif input_type == "pdf": |
|
reader = PdfReader(pdf_input.name) |
|
text = "" |
|
for page in reader.pages: |
|
text += page.extract_text() |
|
else: |
|
raise ValueError("Invalid input type. Choose 'text' or 'pdf'.") |
|
|
|
|
|
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) |