Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +52 -0
- rag_pipeline.py +48 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from rag_pipeline import build_rag_chain
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
chain = None
|
| 8 |
+
|
| 9 |
+
def upload_file(files):
|
| 10 |
+
global chain
|
| 11 |
+
if files is None:
|
| 12 |
+
return "No file uploaded."
|
| 13 |
+
if isinstance(files, str):
|
| 14 |
+
paths = [files]
|
| 15 |
+
else:
|
| 16 |
+
paths = [f if isinstance(f, str) else f.name for f in files] if isinstance(files, list) else [files.name]
|
| 17 |
+
|
| 18 |
+
full_chain, _ = build_rag_chain(paths)
|
| 19 |
+
chain = full_chain
|
| 20 |
+
return f"โ
{len(paths)} document(s) loaded. Ask away!"
|
| 21 |
+
|
| 22 |
+
def ask_question(message, history):
|
| 23 |
+
if chain is None:
|
| 24 |
+
return "Upload a document first."
|
| 25 |
+
|
| 26 |
+
history_text = ""
|
| 27 |
+
for h in history:
|
| 28 |
+
if isinstance(h, (list, tuple)) and len(h) == 2:
|
| 29 |
+
history_text += f"User: {h[0]}\nAssistant: {h[1]}\n"
|
| 30 |
+
|
| 31 |
+
full_query = f"{history_text}Question: {message}" if history_text else message
|
| 32 |
+
|
| 33 |
+
result = chain.invoke(full_query)
|
| 34 |
+
answer = result["answer"]
|
| 35 |
+
pages = sorted(set(
|
| 36 |
+
d.metadata.get("page", 0) + 1
|
| 37 |
+
for d in result["source_documents"]
|
| 38 |
+
))
|
| 39 |
+
return f"{answer}\n\n๐ Source pages: {pages}"
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as full_app:
|
| 42 |
+
gr.Markdown("# ๐ RAG Document Q&A")
|
| 43 |
+
file_input = gr.File(label="Upload PDF(s)", file_types=[".pdf"], file_count="multiple")
|
| 44 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 45 |
+
file_input.upload(upload_file, inputs=file_input, outputs=status)
|
| 46 |
+
gr.Markdown("---")
|
| 47 |
+
question = gr.Textbox(label="Ask a question")
|
| 48 |
+
answer = gr.Textbox(label="Answer", interactive=False, lines=10)
|
| 49 |
+
btn = gr.Button("Ask")
|
| 50 |
+
btn.click(fn=ask_question, inputs=[question, answer], outputs=answer)
|
| 51 |
+
|
| 52 |
+
full_app.launch()
|
rag_pipeline.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 2 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 3 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain_core.prompts import PromptTemplate
|
| 7 |
+
from langchain_core.runnables import RunnablePassthrough, RunnableParallel
|
| 8 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
def build_rag_chain(file_paths):
|
| 12 |
+
if isinstance(file_paths, str):
|
| 13 |
+
file_paths = [file_paths]
|
| 14 |
+
|
| 15 |
+
all_chunks = []
|
| 16 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 17 |
+
|
| 18 |
+
for path in file_paths:
|
| 19 |
+
loader = PyPDFLoader(path)
|
| 20 |
+
docs = loader.load()
|
| 21 |
+
all_chunks.extend(splitter.split_documents(docs))
|
| 22 |
+
|
| 23 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 24 |
+
vectorstore = FAISS.from_documents(all_chunks, embeddings)
|
| 25 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
|
| 26 |
+
|
| 27 |
+
llm = ChatGroq(
|
| 28 |
+
groq_api_key=os.getenv("GROQ_API_KEY"),
|
| 29 |
+
model_name="llama-3.3-70b-versatile"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
prompt = PromptTemplate.from_template(
|
| 33 |
+
"Use the context below to answer the question.\n\nContext: {context}\n\nQuestion: {question}"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
answer_chain = (
|
| 37 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 38 |
+
| prompt
|
| 39 |
+
| llm
|
| 40 |
+
| StrOutputParser()
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
full_chain = RunnableParallel(
|
| 44 |
+
answer=answer_chain,
|
| 45 |
+
source_documents=retriever
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return full_chain, retriever
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain-community
|
| 3 |
+
langchain-groq
|
| 4 |
+
langchain-core
|
| 5 |
+
langchain-text-splitters
|
| 6 |
+
faiss-cpu
|
| 7 |
+
sentence-transformers
|
| 8 |
+
gradio
|
| 9 |
+
python-dotenv
|
| 10 |
+
pypdf
|