Spaces:
Running
Running
File size: 4,175 Bytes
6e2c6a7 86e4d35 6e2c6a7 b7eba91 86e4d35 6e2c6a7 15dfeb3 b7eba91 86e4d35 15dfeb3 86e4d35 67edaad b7eba91 86e4d35 b7eba91 86e4d35 b7eba91 6e2c6a7 86e4d35 b7eba91 86e4d35 67edaad 6e2c6a7 b7eba91 15dfeb3 b7eba91 67edaad 6e2c6a7 b7eba91 15dfeb3 b7eba91 86e4d35 b7eba91 6e2c6a7 b7eba91 6e2c6a7 67edaad b7eba91 86e4d35 b7eba91 18056d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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()
|