Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,73 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
from
|
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 |
-
text
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
gr.
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
#
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
submit_btn.click(fn=summarize_and_speak,
|
74 |
-
inputs=[input_type, text_input, pdf_input],
|
75 |
-
outputs=[text_output, audio_output])
|
76 |
-
|
77 |
demo.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import torch
|
4 |
+
from gtts import gTTS
|
5 |
+
from PyPDF2 import PdfReader
|
6 |
+
|
7 |
+
# --- Dispositivo ---
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
# --- Summarizer en español (modelo multilingüe compatible) ---
|
11 |
+
summarizer = pipeline(
|
12 |
+
"summarization",
|
13 |
+
model="csebuetnlp/mT5_multilingual_XLSum",
|
14 |
+
tokenizer="csebuetnlp/mT5_multilingual_XLSum",
|
15 |
+
device=0 if torch.cuda.is_available() else -1
|
16 |
+
)
|
17 |
+
|
18 |
+
# ... (other imports and functions remain the same)
|
19 |
+
|
20 |
+
def summarize_and_speak(input_type, text_input, pdf_input):
|
21 |
+
"""
|
22 |
+
Summarizes the input data (text or PDF) and converts the summary to speech.
|
23 |
+
"""
|
24 |
+
try:
|
25 |
+
if input_type == "text":
|
26 |
+
text = text_input
|
27 |
+
elif input_type == "pdf":
|
28 |
+
reader = PdfReader(pdf_input.name) # Assuming input_data is a file-like object
|
29 |
+
text = ""
|
30 |
+
for page in reader.pages:
|
31 |
+
text += page.extract_text()
|
32 |
+
else:
|
33 |
+
raise ValueError("Invalid input type. Choose 'text' or 'pdf'.")
|
34 |
+
|
35 |
+
# --- Usando el nuevo modelo de summarize ---
|
36 |
+
summary = summarizer(
|
37 |
+
text,
|
38 |
+
max_length=300,
|
39 |
+
min_length=80,
|
40 |
+
do_sample=False
|
41 |
+
)[0]["summary_text"]
|
42 |
+
|
43 |
+
tts = gTTS(text=summary, lang='es')
|
44 |
+
tts.save("summary.mp3")
|
45 |
+
return summary, "summary.mp3"
|
46 |
+
except Exception as e:
|
47 |
+
return f"An error occurred: {e}", None
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("## Resumen de Historias con Voz")
|
51 |
+
|
52 |
+
with gr.Tab("Texto"):
|
53 |
+
text_input = gr.Textbox(label="Introduce tu historia aquí")
|
54 |
+
with gr.Tab("PDF"):
|
55 |
+
pdf_input = gr.File(label="Sube tu archivo PDF", type="filepath")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
text_output = gr.Textbox(label="Resumen")
|
59 |
+
audio_output = gr.Audio(label="Resumen en Audio")
|
60 |
+
|
61 |
+
submit_btn = gr.Button("Resumir y Convertir a Voz")
|
62 |
+
|
63 |
+
# Updated to use gr.components for input elements
|
64 |
+
# The 'default' keyword argument is replaced by setting the value directly.
|
65 |
+
input_type = gr.components.Radio(choices=["text", "pdf"], label="Tipo de entrada")
|
66 |
+
input_type.value = "text" # Set the default value here
|
67 |
+
|
68 |
+
# Pass all inputs to the function, and the function will decide which one to use
|
69 |
+
submit_btn.click(fn=summarize_and_speak,
|
70 |
+
inputs=[input_type, text_input, pdf_input],
|
71 |
+
outputs=[text_output, audio_output])
|
72 |
+
|
|
|
|
|
|
|
|
|
73 |
demo.launch(share=True)
|