Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# LangChain imports
|
| 4 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
+
|
| 9 |
+
# LLM
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 12 |
+
|
| 13 |
+
# -----------------------------
|
| 14 |
+
# Step 1: Load docs
|
| 15 |
+
loader1 = PyPDFLoader("AST-1.pdf")
|
| 16 |
+
loader2 = PyPDFLoader("AST-2.pdf")
|
| 17 |
+
docs = loader1.load() + loader2.load()
|
| 18 |
+
|
| 19 |
+
# Step 2: Split
|
| 20 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
| 21 |
+
chunks = splitter.split_documents(docs)
|
| 22 |
+
|
| 23 |
+
# Step 3: Embeddings + DB
|
| 24 |
+
embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
+
db = FAISS.from_documents(chunks, embedding)
|
| 26 |
+
|
| 27 |
+
# Step 4: Retriever (MMR)
|
| 28 |
+
retriever = db.as_retriever(search_type="mmr", search_kwargs={"k": 4})
|
| 29 |
+
|
| 30 |
+
# Step 5: LLM
|
| 31 |
+
pipe = pipeline("text-generation", model="google/flan-t5-base", max_new_tokens=200)
|
| 32 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 33 |
+
|
| 34 |
+
# RAG function
|
| 35 |
+
def rag_query(query):
|
| 36 |
+
docs = retriever.invoke(query)
|
| 37 |
+
context = "\n\n".join([d.page_content for d in docs])
|
| 38 |
+
|
| 39 |
+
prompt = f"""
|
| 40 |
+
Answer the question based only on the context below.
|
| 41 |
+
|
| 42 |
+
Context:
|
| 43 |
+
{context}
|
| 44 |
+
|
| 45 |
+
Question:
|
| 46 |
+
{query}
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
return llm.invoke(prompt)
|
| 50 |
+
|
| 51 |
+
# -----------------------------
|
| 52 |
+
# Gradio UI
|
| 53 |
+
with gr.Blocks() as app:
|
| 54 |
+
gr.Markdown("# 📚 RAG Q&A System")
|
| 55 |
+
|
| 56 |
+
chatbot = gr.Chatbot(height=500)
|
| 57 |
+
msg = gr.Textbox(placeholder="Ask your question...")
|
| 58 |
+
|
| 59 |
+
def respond(message, history):
|
| 60 |
+
response = rag_query(message)
|
| 61 |
+
history.append((message, response))
|
| 62 |
+
return "", history
|
| 63 |
+
|
| 64 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 65 |
+
|
| 66 |
+
app.launch()
|