Spaces:
Running
Running
import os | |
import gradio as gr | |
from transformers import pipeline | |
from PyPDF2 import PdfReader | |
# Load free Hugging Face models | |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") | |
# Global variable to store PDF text | |
pdf_text_store = "" | |
def extract_text_from_pdf(pdf_file): | |
reader = PdfReader(pdf_file) | |
text = "" | |
for page in reader.pages: | |
page_text = page.extract_text() | |
if page_text: | |
text += page_text + "\n" | |
return text | |
def upload_pdf(file, history): | |
global pdf_text_store | |
pdf_text_store = extract_text_from_pdf(file) | |
if not pdf_text_store.strip(): | |
history.append(("System", "β No text could be extracted from the PDF. Please try another PDF.")) | |
return history | |
summary = summarizer(pdf_text_store[:1000], max_length=200, min_length=50, do_sample=False)[0]['summary_text'] | |
history.append(("System", f"β PDF processed!\n\n**Summary:**\n{summary}")) | |
return history | |
def chatbot(user_message, history): | |
if not pdf_text_store: | |
history.append((user_message, "β Please upload a PDF first.")) | |
return "", history | |
result = qa_pipeline({ | |
'context': pdf_text_store, | |
'question': user_message | |
}) | |
answer = result["answer"] | |
history.append((user_message, answer)) | |
return "", history | |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue")) as demo: | |
gr.HTML(""" | |
<style> | |
body { | |
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); | |
font-family: 'Helvetica Neue', sans-serif; | |
} | |
.title { | |
text-align: center; | |
font-size: 2.5rem; | |
font-weight: 700; | |
margin-bottom: 0.2em; | |
color: #4f46e5; | |
} | |
.subtitle { | |
text-align: center; | |
font-size: 1.1rem; | |
color: #4b5563; | |
margin-bottom: 2em; | |
} | |
.custom-btn { | |
background: #4f46e5; | |
color: white; | |
border-radius: 0.5em; | |
padding: 0.5em 1.2em; | |
transition: background 0.3s; | |
} | |
.custom-btn:hover { | |
background: #4338ca; | |
} | |
.upload-box { | |
background: white; | |
padding: 1em; | |
border-radius: 0.75em; | |
box-shadow: 0 2px 10px rgba(0,0,0,0.1); | |
margin-bottom: 1em; | |
} | |
.chat-area { | |
background: white; | |
border-radius: 0.75em; | |
box-shadow: 0 2px 10px rgba(0,0,0,0.1); | |
overflow: hidden; | |
} | |
</style> | |
""") | |
gr.HTML('<div class="title">π Chat with Your PDF</div>') | |
gr.HTML('<div class="subtitle">Upload a PDF, get a summary, and chat with it!</div>') | |
with gr.Row(): | |
with gr.Column(scale=1, min_width=250): | |
with gr.Group(elem_classes="upload-box"): | |
gr.Markdown("### π Upload Your PDF") | |
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"]) | |
upload_btn = gr.Button("π Process PDF", elem_classes="custom-btn") | |
with gr.Column(scale=2): | |
chatbot_ui = gr.Chatbot( | |
label="π¬ Chat with your PDF", | |
height=500, | |
show_copy_button=True, | |
elem_classes="chat-area" | |
) | |
user_input = gr.Textbox( | |
placeholder="Ask something about your PDF...", | |
show_label=False | |
) | |
upload_btn.click(upload_pdf, inputs=[pdf_upload, chatbot_ui], outputs=chatbot_ui) | |
user_input.submit(chatbot, [user_input, chatbot_ui], [user_input, chatbot_ui]) | |
gr.HTML(""" | |
<div style="text-align:center; font-size:0.9rem; color:#6b7280; margin-top:2em;"> | |
Made with β€οΈ β No API keys needed! | |
</div> | |
""") | |
if __name__ == "__main__": | |
demo.launch() | |