Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
import PyPDF2 | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
def summarize_pdf(pdf_file): | |
if pdf_file is None: | |
return "β Please upload a PDF file first." | |
try: | |
reader = PyPDF2.PdfReader(pdf_file) | |
text = "" | |
for page in reader.pages: | |
text += page.extract_text() or "" | |
text = text[:2000] # limit to avoid too long input | |
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text'] | |
return summary | |
except Exception as e: | |
return f"β Error reading PDF: {str(e)}" | |
with gr.Blocks(css=""" | |
body {background: #f0f4f8; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;} | |
#main_container { max-width: 720px; margin: auto; padding: 2rem; background: white; | |
box-shadow: 0 8px 24px rgba(149, 157, 165, 0.2); border-radius: 12px; } | |
.btn-primary {background: #3b82f6; color: white; font-weight: 600; padding: 0.8rem 1.6rem; | |
border-radius: 8px; border: none; transition: background 0.3s;} | |
.btn-primary:hover {background: #2563eb;} | |
.scroll-box {max-height: 220px; overflow-y: auto; padding: 1rem; border: 1px solid #ddd; border-radius: 8px; background: #fafafa;} | |
""") as demo: | |
with gr.Column(elem_id="main_container"): | |
gr.Markdown( | |
""" | |
<h1 style="text-align:center; color:#1e40af;">π PDF Summarizer</h1> | |
<p style="text-align:center; color:#475569;"> | |
Upload your PDF and get a crisp summary generated by Hugging Face's BART model. | |
</p> | |
""" | |
) | |
pdf_input = gr.File(label="Upload PDF file", file_types=[".pdf"], interactive=True) | |
summary_output = gr.Textbox(label="Summary", interactive=False, elem_classes="scroll-box", lines=8) | |
summarize_btn = gr.Button("Generate Summary", elem_classes="btn-primary") | |
summarize_btn.click(fn=summarize_pdf, inputs=pdf_input, outputs=summary_output) | |
gr.Markdown( | |
""" | |
<footer style="text-align:center; margin-top:2rem; font-size:0.9rem; color:#94a3b8;"> | |
Made with β€οΈ by YourName | Powered by Hugging Face & Gradio | |
</footer> | |
""" | |
) | |
if __name__ == "__main__": | |
demo.launch() | |