Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import PyPDF2
|
| 4 |
+
|
| 5 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 6 |
+
|
| 7 |
+
def summarize_pdf(pdf_file):
|
| 8 |
+
if pdf_file is None:
|
| 9 |
+
return "❌ Please upload a PDF file first."
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
reader = PyPDF2.PdfReader(pdf_file)
|
| 13 |
+
text = ""
|
| 14 |
+
for page in reader.pages:
|
| 15 |
+
text += page.extract_text() or ""
|
| 16 |
+
|
| 17 |
+
text = text[:2000] # limit to avoid too long input
|
| 18 |
+
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
| 19 |
+
return summary
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"❌ Error reading PDF: {str(e)}"
|
| 22 |
+
|
| 23 |
+
with gr.Blocks(css="""
|
| 24 |
+
body {background: #f0f4f8; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}
|
| 25 |
+
#main_container { max-width: 720px; margin: auto; padding: 2rem; background: white;
|
| 26 |
+
box-shadow: 0 8px 24px rgba(149, 157, 165, 0.2); border-radius: 12px; }
|
| 27 |
+
.btn-primary {background: #3b82f6; color: white; font-weight: 600; padding: 0.8rem 1.6rem;
|
| 28 |
+
border-radius: 8px; border: none; transition: background 0.3s;}
|
| 29 |
+
.btn-primary:hover {background: #2563eb;}
|
| 30 |
+
.scroll-box {max-height: 220px; overflow-y: auto; padding: 1rem; border: 1px solid #ddd; border-radius: 8px; background: #fafafa;}
|
| 31 |
+
""") as demo:
|
| 32 |
+
|
| 33 |
+
with gr.Column(elem_id="main_container"):
|
| 34 |
+
gr.Markdown(
|
| 35 |
+
"""
|
| 36 |
+
<h1 style="text-align:center; color:#1e40af;">📄 PDF Summarizer</h1>
|
| 37 |
+
<p style="text-align:center; color:#475569;">
|
| 38 |
+
Upload your PDF and get a crisp summary generated by Hugging Face's BART model.
|
| 39 |
+
</p>
|
| 40 |
+
"""
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
pdf_input = gr.File(label="Upload PDF file", file_types=[".pdf"], interactive=True)
|
| 44 |
+
summary_output = gr.Textbox(label="Summary", interactive=False, elem_classes="scroll-box", lines=8)
|
| 45 |
+
summarize_btn = gr.Button("Generate Summary", elem_classes="btn-primary")
|
| 46 |
+
|
| 47 |
+
summarize_btn.click(fn=summarize_pdf, inputs=pdf_input, outputs=summary_output)
|
| 48 |
+
|
| 49 |
+
gr.Markdown(
|
| 50 |
+
"""
|
| 51 |
+
<footer style="text-align:center; margin-top:2rem; font-size:0.9rem; color:#94a3b8;">
|
| 52 |
+
Made with ❤️ by YourName | Powered by Hugging Face & Gradio
|
| 53 |
+
</footer>
|
| 54 |
+
"""
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
demo.launch()
|