Spaces:
Runtime error
Runtime error
Commit ·
dde5f03
1
Parent(s): c5bf023
first commit
Browse files- .gitignore +2 -0
- app.py +128 -4
- requirements.txt +10 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore all files within the documents directory
|
| 2 |
+
documents/*
|
app.py
CHANGED
|
@@ -1,7 +1,131 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
+
from langchain.chains import create_retrieval_chain
|
| 11 |
+
from langchain.retrievers import ContextualCompressionRetriever
|
| 12 |
+
from langchain_huggingface.cross_encoders import HuggingFaceCrossEncoder
|
| 13 |
|
| 14 |
+
# --- Configuration ---
|
| 15 |
+
# Make sure your GROQ_API_KEY is set in your Hugging Face Space secrets
|
| 16 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 17 |
|
| 18 |
+
# --- 1. Load Documents ---
|
| 19 |
+
def load_documents(file_path):
|
| 20 |
+
"""Loads documents from a file path."""
|
| 21 |
+
loader = PyPDFLoader(file_path)
|
| 22 |
+
return loader.load()
|
| 23 |
+
|
| 24 |
+
# --- 2. Text Splitting & Vector Store Creation ---
|
| 25 |
+
def create_vector_store(documents, chunk_size=1000, chunk_overlap=200):
|
| 26 |
+
"""Splits documents and creates a FAISS vector store."""
|
| 27 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
|
| 28 |
+
docs = text_splitter.split_documents(documents)
|
| 29 |
+
|
| 30 |
+
# Use a popular sentence-transformer model for embeddings
|
| 31 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 32 |
+
|
| 33 |
+
# Create the vector store
|
| 34 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 35 |
+
return vectorstore
|
| 36 |
+
|
| 37 |
+
# --- 3. RAG Chain Creation ---
|
| 38 |
+
def create_rag_chain(vectorstore, llm, retriever_k=4, use_reranker=False):
|
| 39 |
+
"""Creates a simple or advanced RAG chain."""
|
| 40 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": retriever_k})
|
| 41 |
+
|
| 42 |
+
# Advanced RAG: Add a reranker for more relevant results
|
| 43 |
+
if use_reranker:
|
| 44 |
+
# Using a powerful cross-encoder for reranking
|
| 45 |
+
cross_encoder = HuggingFaceCrossEncoder(model_name="BAAI/bge-reranker-large")
|
| 46 |
+
# The compression retriever will fetch more docs (e.g., 10) and the reranker will pick the top 3
|
| 47 |
+
compressor = ContextualCompressionRetriever(
|
| 48 |
+
base_compressor=cross_encoder, base_retriever=retriever
|
| 49 |
+
)
|
| 50 |
+
retriever = compressor
|
| 51 |
+
|
| 52 |
+
prompt = ChatPromptTemplate.from_template("""
|
| 53 |
+
You are a helpful assistant for question-answering tasks.
|
| 54 |
+
Use the following retrieved context to answer the question.
|
| 55 |
+
If you don't know the answer, just say that you don't know.
|
| 56 |
+
Use three sentences maximum and keep the answer concise.
|
| 57 |
+
|
| 58 |
+
Question: {input}
|
| 59 |
+
Context: {context}
|
| 60 |
+
Answer:
|
| 61 |
+
""")
|
| 62 |
+
|
| 63 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
| 64 |
+
return create_retrieval_chain(retriever, document_chain)
|
| 65 |
+
|
| 66 |
+
# --- Gradio UI ---
|
| 67 |
+
def create_chat_interface(rag_chain_creator, use_reranker_flag):
|
| 68 |
+
"""A factory function to create the chat logic for a Gradio tab."""
|
| 69 |
+
def chat_logic(message, history, file_upload, chunk_size, k_retriever):
|
| 70 |
+
if rag_chain_creator.chain is None:
|
| 71 |
+
if file_upload is None:
|
| 72 |
+
return "Please upload a document first.", history
|
| 73 |
+
|
| 74 |
+
# Process the document and create the RAG chain
|
| 75 |
+
docs = load_documents(file_upload.name)
|
| 76 |
+
vector_store = create_vector_store(docs, chunk_size)
|
| 77 |
+
llm = ChatGroq(temperature=0, model_name="llama3-8b-8192", api_key=GROQ_API_KEY)
|
| 78 |
+
rag_chain_creator.chain = create_rag_chain(vector_store, llm, k_retriever, use_reranker=use_reranker_flag)
|
| 79 |
+
|
| 80 |
+
response = rag_chain_creator.chain.invoke({"input": message})
|
| 81 |
+
return response["answer"]
|
| 82 |
+
|
| 83 |
+
return chat_logic
|
| 84 |
+
|
| 85 |
+
class RAGChainState:
|
| 86 |
+
"""Simple class to hold the state of a RAG chain."""
|
| 87 |
+
def __init__(self):
|
| 88 |
+
self.chain = None
|
| 89 |
+
|
| 90 |
+
# Create separate state holders for each tab to keep them independent
|
| 91 |
+
simple_rag_state = RAGChainState()
|
| 92 |
+
advanced_rag_state = RAGChainState()
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo:
|
| 95 |
+
gr.Markdown("# 🚀 RAG Pipeline Demo with Groq & LangChain")
|
| 96 |
+
gr.Markdown("Upload a document and ask questions. Compare the 'Simple' and 'Advanced' RAG pipelines.")
|
| 97 |
+
|
| 98 |
+
with gr.Tabs():
|
| 99 |
+
# --- Simple RAG Tab ---
|
| 100 |
+
with gr.TabItem("Simple RAG"):
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column(scale=1):
|
| 103 |
+
file_upload_simple = gr.File(label="Upload your TXT, MD, or PDF")
|
| 104 |
+
with gr.Accordion("Settings", open=False):
|
| 105 |
+
chunk_size_simple = gr.Slider(500, 2000, value=1000, step=100, label="Chunk Size")
|
| 106 |
+
k_simple = gr.Slider(1, 10, value=4, step=1, label="Docs to Retrieve (k)")
|
| 107 |
+
with gr.Column(scale=2):
|
| 108 |
+
gr.ChatInterface(
|
| 109 |
+
fn=create_chat_interface(simple_rag_state, use_reranker_flag=False),
|
| 110 |
+
additional_inputs=[file_upload_simple, chunk_size_simple, k_simple],
|
| 111 |
+
title="Simple RAG",
|
| 112 |
+
description="Basic retriever. Good for general queries."
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# --- Advanced RAG Tab ---
|
| 116 |
+
with gr.TabItem("Advanced RAG (with Reranker)"):
|
| 117 |
+
with gr.Row():
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
file_upload_advanced = gr.File(label="Upload your TXT, MD, or PDF")
|
| 120 |
+
with gr.Accordion("Settings", open=False):
|
| 121 |
+
chunk_size_advanced = gr.Slider(500, 2000, value=1000, step=100, label="Chunk Size")
|
| 122 |
+
k_advanced = gr.Slider(1, 10, value=4, step=1, label="Docs to Retrieve (k)")
|
| 123 |
+
with gr.Column(scale=2):
|
| 124 |
+
gr.ChatInterface(
|
| 125 |
+
fn=create_chat_interface(advanced_rag_state, use_reranker_flag=True),
|
| 126 |
+
additional_inputs=[file_upload_advanced, chunk_size_advanced, k_advanced],
|
| 127 |
+
title="Advanced RAG",
|
| 128 |
+
description="Uses a Cross-Encoder to rerank results for higher accuracy."
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.29.0
|
| 2 |
+
langchain
|
| 3 |
+
langchain-groq
|
| 4 |
+
langchain-community
|
| 5 |
+
sentence-transformers
|
| 6 |
+
faiss-cpu
|
| 7 |
+
pypdf
|
| 8 |
+
langchain-huggingface
|
| 9 |
+
accelerate
|
| 10 |
+
bitsandbytes
|