Emileo21 commited on
Commit
49cba45
·
verified ·
1 Parent(s): a2bb360

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -1,46 +1,46 @@
 
 
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:
@@ -60,12 +60,9 @@ with gr.Blocks() as demo:
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])
 
1
+ # Realizado por Leonardo Vannoni Lorenzo para el curso de Deep Learning de INTEC, 1105795
2
+
3
  import gradio as gr
4
  from transformers import pipeline
5
  import torch
6
  from gtts import gTTS
7
  from PyPDF2 import PdfReader
8
 
9
+ # --- Usar GPU si esta disponible ---
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
+ # --- Resumidor multilingual ---
13
  summarizer = pipeline(
14
  "summarization",
15
  model="csebuetnlp/mT5_multilingual_XLSum",
16
  tokenizer="csebuetnlp/mT5_multilingual_XLSum",
17
  device=0 if torch.cuda.is_available() else -1
18
  )
 
 
19
 
20
  def summarize_and_speak(input_type, text_input, pdf_input):
21
  """
22
+ Resumir el input y devolver mensaje hablado.
23
  """
24
  try:
25
+ if input_type == "text": # Resumir el cuadro de texto
26
  text = text_input
27
  elif input_type == "pdf":
28
+ reader = PdfReader(pdf_input.name) # Resumir el PDF
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 modelo de summarize ---
36
  summary = summarizer(
37
  text,
38
+ max_length=2500,
39
+ min_length=500,
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:
 
60
 
61
  submit_btn = gr.Button("Resumir y Convertir a Voz")
62
 
 
 
63
  input_type = gr.components.Radio(choices=["text", "pdf"], label="Tipo de entrada")
64
+ input_type.value = "text"
65
 
 
66
  submit_btn.click(fn=summarize_and_speak,
67
  inputs=[input_type, text_input, pdf_input],
68
  outputs=[text_output, audio_output])