Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,50 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PyPDF2 import PdfReader
|
3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
from gtts import gTTS
|
5 |
from io import BytesIO
|
6 |
|
7 |
-
#
|
8 |
-
try:
|
9 |
-
|
10 |
-
|
11 |
-
except ImportError:
|
12 |
-
|
13 |
|
14 |
-
#
|
15 |
-
model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
|
16 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
18 |
|
19 |
def summarize_pdf_abstract(pdf_bytes):
|
20 |
-
"""
|
21 |
-
Reads a PDF file, extracts the abstract, and summarizes it in one sentence.
|
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 |
-
else:
|
48 |
-
speech_bytes = None
|
49 |
-
|
50 |
-
return {"summary": summary, "audio": speech_bytes}
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
raise Exception(str(e))
|
54 |
-
|
55 |
-
# Modify the Gradio interface based on IPython availability
|
56 |
if ipython_available:
|
57 |
-
|
58 |
-
interface = gr.Interface(
|
59 |
-
fn=summarize_pdf_abstract,
|
60 |
-
inputs=[gr.File(label="Upload PDF", type="binary")],
|
61 |
-
outputs=[gr.Text(label="One-sentence summary"), gr.Audio(label="Summary audio")],
|
62 |
-
)
|
63 |
else:
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
inputs=[gr.File(label="Upload PDF", type="binary")],
|
68 |
-
outputs=[gr.Text(label="One-sentence summary")],
|
69 |
-
)
|
70 |
-
|
71 |
-
# Launch the Gradio interface
|
72 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
from gtts import gTTS
|
5 |
from io import BytesIO
|
6 |
|
7 |
+
# IPython check
|
8 |
+
try:
|
9 |
+
from IPython.display import Audio
|
10 |
+
ipython_available = True
|
11 |
+
except ImportError:
|
12 |
+
ipython_available = False
|
13 |
|
14 |
+
# Model
|
15 |
+
model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
|
16 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
18 |
|
19 |
def summarize_pdf_abstract(pdf_bytes):
|
|
|
|
|
20 |
|
21 |
+
try:
|
22 |
+
reader = PdfReader(pdf_bytes)
|
23 |
+
|
24 |
+
abstract_text = ""
|
25 |
+
for page in reader.pages:
|
26 |
+
if "Abstract" in page.extract_text() or "Introduction" in page.extract_text():
|
27 |
+
abstract_text = page.extract_text()
|
28 |
+
break
|
29 |
+
|
30 |
+
inputs = tokenizer(abstract_text, return_tensors="pt")
|
31 |
+
outputs = model.generate(**inputs)
|
32 |
+
summary = tokenizer.decode(outputs[0])
|
33 |
+
|
34 |
+
if ipython_available:
|
35 |
+
speech = gTTS(text=summary, lang="en")
|
36 |
+
speech_bytes = speech.get_wav_data()
|
37 |
+
else:
|
38 |
+
speech_bytes = None
|
39 |
+
|
40 |
+
return {"summary": summary, "audio": speech_bytes}
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
raise Exception(str(e))
|
44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if ipython_available:
|
46 |
+
interface = gr.Interface(...)
|
|
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
+
interface = gr.Interface(...)
|
49 |
+
|
50 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|