Emileo21 commited on
Commit
bc3f282
·
verified ·
1 Parent(s): 49b5b01

Update app.py

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