Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +110 -14
- app.py +168 -0
- requirements.txt +17 -0
README.md
CHANGED
|
@@ -1,14 +1,110 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: AI
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AI PDF Q&A Chatbot
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.31.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# π AI PDF Q&A Chatbot (RAG System)
|
| 13 |
+
|
| 14 |
+
An AI-powered PDF Question Answering chatbot built using:
|
| 15 |
+
|
| 16 |
+
- LangChain
|
| 17 |
+
- FAISS
|
| 18 |
+
- Hugging Face Transformers
|
| 19 |
+
- FLAN-T5
|
| 20 |
+
- Gradio
|
| 21 |
+
|
| 22 |
+
Upload PDF documents and ask questions from them using Retrieval-Augmented Generation (RAG).
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
# π Features
|
| 27 |
+
|
| 28 |
+
β
Extract and split document text
|
| 29 |
+
β
Semantic search using embeddings
|
| 30 |
+
β
Retrieval-Augmented Generation (RAG)
|
| 31 |
+
β
Detailed educational answers
|
| 32 |
+
β
Modern Gradio UI
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
# π§ Model Used
|
| 37 |
+
|
| 38 |
+
## Text Generation
|
| 39 |
+
- google/flan-t5-base
|
| 40 |
+
|
| 41 |
+
## Embeddings
|
| 42 |
+
- sentence-transformers/all-MiniLM-L6-v2
|
| 43 |
+
|
| 44 |
+
---
|
| 45 |
+
|
| 46 |
+
# π¦ Technologies
|
| 47 |
+
|
| 48 |
+
- Python
|
| 49 |
+
- Gradio
|
| 50 |
+
- LangChain
|
| 51 |
+
- FAISS
|
| 52 |
+
- Transformers
|
| 53 |
+
- PyTorch
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
# βΆοΈ Run Locally
|
| 58 |
+
|
| 59 |
+
Install dependencies:
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
pip install -r requirements.txt
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
Run app:
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
python app.py
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
---
|
| 72 |
+
|
| 73 |
+
# π Deploy on Hugging Face Spaces
|
| 74 |
+
|
| 75 |
+
1. Create a new Space
|
| 76 |
+
2. Select:
|
| 77 |
+
- SDK: Gradio
|
| 78 |
+
3. Upload:
|
| 79 |
+
- app.py
|
| 80 |
+
- requirements.txt
|
| 81 |
+
- README.md
|
| 82 |
+
|
| 83 |
+
The app will build automatically.
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
# β οΈ Important Notes
|
| 88 |
+
|
| 89 |
+
- First startup may take time because models download automatically.
|
| 90 |
+
- CPU Spaces are supported.
|
| 91 |
+
- No API key required.
|
| 92 |
+
- Recommended Space Hardware:
|
| 93 |
+
- CPU Basic or higher
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
# π How It Works
|
| 98 |
+
|
| 99 |
+
1. Upload PDFs
|
| 100 |
+
2. Documents are split into chunks
|
| 101 |
+
3. Embeddings are generated
|
| 102 |
+
4. FAISS stores vectors
|
| 103 |
+
5. Relevant chunks are retrieved
|
| 104 |
+
6. FLAN-T5 generates detailed answers
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
# π©βπ» Author
|
| 109 |
+
|
| 110 |
+
Built using Hugging Face + Gradio + LangChain.
|
app.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
# MODEL
|
| 12 |
+
model_name = "google/flan-t5-base"
|
| 13 |
+
|
| 14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
|
| 17 |
+
# 1. PROCESS PDF
|
| 18 |
+
def process_pdfs(files):
|
| 19 |
+
|
| 20 |
+
if not files:
|
| 21 |
+
return None, "No files uploaded."
|
| 22 |
+
|
| 23 |
+
all_docs = []
|
| 24 |
+
|
| 25 |
+
for file in files:
|
| 26 |
+
loader = PyPDFLoader(file.name)
|
| 27 |
+
docs = loader.load()
|
| 28 |
+
all_docs.extend(docs)
|
| 29 |
+
|
| 30 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 31 |
+
chunk_size=500,
|
| 32 |
+
chunk_overlap=100
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
split_docs = splitter.split_documents(all_docs)
|
| 36 |
+
|
| 37 |
+
embeddings = HuggingFaceEmbeddings(
|
| 38 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
vectorstore = FAISS.from_documents(split_docs, embeddings)
|
| 42 |
+
|
| 43 |
+
return vectorstore, f"β
{len(split_docs)} chunks processed"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# 2. ASK QUESTION
|
| 47 |
+
|
| 48 |
+
def ask_question(question, vectorstore):
|
| 49 |
+
|
| 50 |
+
if vectorstore is None:
|
| 51 |
+
return "β Please upload PDF files first."
|
| 52 |
+
|
| 53 |
+
docs = vectorstore.similarity_search(question, k=5)
|
| 54 |
+
context = "\n".join([d.page_content for d in docs])
|
| 55 |
+
|
| 56 |
+
prompt = f"""
|
| 57 |
+
You are a university professor teaching Machine Learning.
|
| 58 |
+
|
| 59 |
+
You MUST give a detailed, long and structured answer.
|
| 60 |
+
|
| 61 |
+
Format your answer EXACTLY like this:
|
| 62 |
+
|
| 63 |
+
1. Definition:
|
| 64 |
+
2. Explanation:
|
| 65 |
+
3. Intuition (why it is used):
|
| 66 |
+
4. Step-by-step working (if applicable):
|
| 67 |
+
5. Example:
|
| 68 |
+
6. Summary:
|
| 69 |
+
|
| 70 |
+
Do NOT give short answers.
|
| 71 |
+
|
| 72 |
+
Context:
|
| 73 |
+
{context}
|
| 74 |
+
|
| 75 |
+
Question:
|
| 76 |
+
{question}
|
| 77 |
+
|
| 78 |
+
Now write a complete educational answer:
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
#3. generated output
|
| 82 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 83 |
+
|
| 84 |
+
with torch.no_grad():
|
| 85 |
+
outputs = model.generate(
|
| 86 |
+
**inputs,
|
| 87 |
+
max_new_tokens=300,
|
| 88 |
+
temperature=0.8,
|
| 89 |
+
top_p=0.9,
|
| 90 |
+
do_sample=True,
|
| 91 |
+
repetition_penalty=1.2
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# GRADIO UI (FIX IS HERE)
|
| 98 |
+
import gradio as gr
|
| 99 |
+
|
| 100 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 101 |
+
#title {
|
| 102 |
+
text-align: center;
|
| 103 |
+
font-size: 28px;
|
| 104 |
+
font-weight: bold;
|
| 105 |
+
color: #4A90E2;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.container {
|
| 109 |
+
max-width: 900px;
|
| 110 |
+
margin: auto;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
.chatbox {
|
| 114 |
+
height: 400px;
|
| 115 |
+
overflow-y: auto;
|
| 116 |
+
border-radius: 12px;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
button {
|
| 120 |
+
border-radius: 10px !important;
|
| 121 |
+
}
|
| 122 |
+
""") as demo:
|
| 123 |
+
|
| 124 |
+
gr.Markdown("<h1 id='title'>π AI PDF Q&A Chatbot (RAG System)</h1>")
|
| 125 |
+
|
| 126 |
+
with gr.Row():
|
| 127 |
+
with gr.Column(scale=1):
|
| 128 |
+
gr.Markdown("### π Upload PDF")
|
| 129 |
+
pdf_input = gr.File(file_count="multiple", file_types=[".pdf"])
|
| 130 |
+
upload_btn = gr.Button(" Process PDFs", variant="primary")
|
| 131 |
+
upload_output = gr.Textbox(label="Status")
|
| 132 |
+
|
| 133 |
+
with gr.Column(scale=2):
|
| 134 |
+
gr.Markdown("### π¬ Ask Questions")
|
| 135 |
+
|
| 136 |
+
question_input = gr.Textbox(
|
| 137 |
+
placeholder="Ask anything from your PDF...",
|
| 138 |
+
label="Your Question"
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
ask_btn = gr.Button(" Ask AI", variant="primary")
|
| 142 |
+
|
| 143 |
+
answer_output = gr.Textbox(
|
| 144 |
+
label="AI Answer",
|
| 145 |
+
lines=10
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# -------------------------
|
| 149 |
+
# STATE
|
| 150 |
+
# -------------------------
|
| 151 |
+
vector_state = gr.State(None)
|
| 152 |
+
|
| 153 |
+
# -------------------------
|
| 154 |
+
# EVENTS
|
| 155 |
+
# -------------------------
|
| 156 |
+
upload_btn.click(
|
| 157 |
+
process_pdfs,
|
| 158 |
+
inputs=pdf_input,
|
| 159 |
+
outputs=[vector_state, upload_output]
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
ask_btn.click(
|
| 163 |
+
ask_question,
|
| 164 |
+
inputs=[question_input, vector_state],
|
| 165 |
+
outputs=answer_output
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.31.0
|
| 2 |
+
|
| 3 |
+
torch==2.7.0
|
| 4 |
+
transformers==4.52.3
|
| 5 |
+
sentence-transformers==4.1.0
|
| 6 |
+
|
| 7 |
+
langchain==0.3.25
|
| 8 |
+
langchain-community==0.3.24
|
| 9 |
+
langchain-text-splitters==0.3.8
|
| 10 |
+
|
| 11 |
+
faiss-cpu==1.11.0
|
| 12 |
+
|
| 13 |
+
pypdf==5.5.0
|
| 14 |
+
accelerate==1.7.0
|
| 15 |
+
|
| 16 |
+
huggingface-hub==0.32.3
|
| 17 |
+
safetensors==0.5.3
|