Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install dependencies
|
| 2 |
+
!pip install -q langchain transformers faiss-cpu huggingface-hub gradio pypdf
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.document_loaders import PyPDFLoader
|
| 7 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 8 |
+
from langchain.vectorstores import FAISS
|
| 9 |
+
from langchain.chains import RetrievalQA
|
| 10 |
+
from langchain.llms import HuggingFacePipeline
|
| 11 |
+
from transformers import pipeline
|
| 12 |
+
|
| 13 |
+
# Global vars to be set after PDF upload
|
| 14 |
+
qa = None
|
| 15 |
+
|
| 16 |
+
# Step 1: Handle PDF upload and prepare the QA chain
|
| 17 |
+
def load_pdf(pdf_file):
|
| 18 |
+
global qa
|
| 19 |
+
# Load PDF
|
| 20 |
+
loader = PyPDFLoader(pdf_file.name)
|
| 21 |
+
pages = loader.load()
|
| 22 |
+
|
| 23 |
+
# Split pages
|
| 24 |
+
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 25 |
+
docs = splitter.split_documents(pages)
|
| 26 |
+
|
| 27 |
+
# Embed & create vector store
|
| 28 |
+
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 29 |
+
db = FAISS.from_documents(docs, embedding_model)
|
| 30 |
+
|
| 31 |
+
# Use a lightweight model (CPU friendly)
|
| 32 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base", max_new_tokens=512)
|
| 33 |
+
llm = HuggingFacePipeline(pipeline=qa_pipeline)
|
| 34 |
+
|
| 35 |
+
# Build QA chain
|
| 36 |
+
qa = RetrievalQA.from_chain_type(llm=llm, retriever=db.as_retriever())
|
| 37 |
+
|
| 38 |
+
return "✅ PDF loaded! Now ask me anything."
|
| 39 |
+
|
| 40 |
+
# Step 2: Ask questions
|
| 41 |
+
def ask_question(query):
|
| 42 |
+
if qa is None:
|
| 43 |
+
return "⚠️ Upload a PDF first!"
|
| 44 |
+
return qa.run(query)
|
| 45 |
+
|
| 46 |
+
# Gradio Interface
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## 🧠 Chat With Your PDF")
|
| 49 |
+
with gr.Row():
|
| 50 |
+
pdf_input = gr.File(label="📄 Upload a PDF", file_types=[".pdf"])
|
| 51 |
+
upload_output = gr.Textbox(label="Upload Status", interactive=False)
|
| 52 |
+
with gr.Row():
|
| 53 |
+
question_input = gr.Textbox(label="❓ Ask a question", placeholder="Ask me anything...")
|
| 54 |
+
answer_output = gr.Textbox(label="🧠 Answer")
|
| 55 |
+
|
| 56 |
+
pdf_input.change(load_pdf, inputs=pdf_input, outputs=upload_output)
|
| 57 |
+
question_input.submit(ask_question, inputs=question_input, outputs=answer_output)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|