Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
# Load summarization pipeline
|
| 5 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
demo = gr.Interface(
|
| 16 |
-
fn=
|
| 17 |
-
inputs=gr.
|
| 18 |
outputs=gr.Textbox(label="Summarized Notes"),
|
| 19 |
-
title="
|
| 20 |
-
description="
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Launch the app
|
| 24 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
|
| 5 |
# Load summarization pipeline
|
| 6 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 7 |
|
| 8 |
+
# Function to extract text from PDF
|
| 9 |
+
def extract_text_from_pdf(pdf_file):
|
| 10 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 11 |
+
text = ""
|
| 12 |
+
for page in doc:
|
| 13 |
+
text += page.get_text()
|
| 14 |
+
return text
|
| 15 |
|
| 16 |
+
# Combine everything into one function
|
| 17 |
+
def summarize_pdf(pdf_file):
|
| 18 |
+
try:
|
| 19 |
+
text = extract_text_from_pdf(pdf_file)
|
| 20 |
+
if len(text.strip()) == 0:
|
| 21 |
+
return "The PDF seems empty or text is not extractable."
|
| 22 |
+
# Truncate long text (BART model has ~1024 token limit)
|
| 23 |
+
text = text[:3000]
|
| 24 |
+
summary = summarizer(text, max_length=150, min_length=40, do_sample=False)
|
| 25 |
+
return summary[0]['summary_text']
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Gradio Interface
|
| 30 |
demo = gr.Interface(
|
| 31 |
+
fn=summarize_pdf,
|
| 32 |
+
inputs=gr.File(label="Upload PDF of Academic Notes"),
|
| 33 |
outputs=gr.Textbox(label="Summarized Notes"),
|
| 34 |
+
title="📄 Academic Note Summarizer (PDF)",
|
| 35 |
+
description="Upload your academic notes in PDF format. The app will extract and summarize the content using a Hugging Face model."
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
demo.launch()
|
| 39 |
+
|