Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
from PyPDF2 import PdfReader
|
4 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
5 |
from gtts import gTTS
|
6 |
from io import BytesIO
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Define model and tokenizer
|
9 |
model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
|
10 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
@@ -33,17 +39,29 @@ def summarize_pdf_abstract(pdf_data):
|
|
33 |
outputs = model.generate(**inputs)
|
34 |
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
return {"summary": summary, "audio": speech_bytes}
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Launch the Hugging Face Space
|
49 |
interface.launch(title="PDF Abstract Summarizer")
|
|
|
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 |
+
# Check if running in IPython environment
|
8 |
+
try:
|
9 |
+
from IPython.display import Audio
|
10 |
+
ipython_available = True
|
11 |
+
except ImportError:
|
12 |
+
ipython_available = False
|
13 |
+
|
14 |
# Define model and tokenizer
|
15 |
model_name = "ArtifactAI/led_large_16384_arxiv_summarization"
|
16 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
|
39 |
outputs = model.generate(**inputs)
|
40 |
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
|
42 |
+
if ipython_available:
|
43 |
+
speech = gTTS(summary, lang="en")
|
44 |
+
speech_bytes = speech.get_wav_data()
|
45 |
+
else:
|
46 |
+
speech_bytes = None
|
47 |
|
48 |
return {"summary": summary, "audio": speech_bytes}
|
49 |
|
50 |
+
# Modify the Gradio interface based on IPython availability
|
51 |
+
if ipython_available:
|
52 |
+
# If running in IPython, include the Audio component
|
53 |
+
interface = gr.Interface(
|
54 |
+
fn=summarize_pdf_abstract,
|
55 |
+
inputs=[gr.File(label="Upload PDF", mimetypes=["application/pdf"])],
|
56 |
+
outputs=[gr.Text(label="One-sentence summary"), gr.Audio(label="Summary audio")],
|
57 |
+
)
|
58 |
+
else:
|
59 |
+
# If not running in IPython, exclude the Audio component
|
60 |
+
interface = gr.Interface(
|
61 |
+
fn=summarize_pdf_abstract,
|
62 |
+
inputs=[gr.File(label="Upload PDF", mimetypes=["application/pdf"])],
|
63 |
+
outputs=[gr.Text(label="One-sentence summary")],
|
64 |
+
)
|
65 |
|
66 |
# Launch the Hugging Face Space
|
67 |
interface.launch(title="PDF Abstract Summarizer")
|