File size: 9,930 Bytes
e26ea16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import gradio as gr
import os
from typing import List, Dict
import numpy as np
from datasets import load_dataset
from langchain.text_splitter import (
    RecursiveCharacterTextSplitter,
    CharacterTextSplitter,
    TokenTextSplitter
)
from langchain_community.vectorstores import FAISS, Chroma
from langchain_community.document_loaders import PyPDFLoader
from langchain.chains import ConversationalRetrievalChain
from langchain_community.embeddings import HuggingFaceEmbeddings 
from langchain_community.llms import HuggingFaceEndpoint
from langchain.memory import ConversationBufferMemory
from sentence_transformers import SentenceTransformer, util
import torch
from ragas import evaluate
from ragas.metrics import (
    ContextRecall,
    AnswerRelevancy,
    Faithfulness,
    ContextPrecision
)
import pandas as pd

# Constants and configurations
CHUNK_SIZES = {
    "small": {"recursive": 512, "fixed": 512, "token": 256},
    "medium": {"recursive": 1024, "fixed": 1024, "token": 512}
}

class RAGEvaluator:
    def __init__(self):
        self.datasets = {
            "squad": "squad_v2",
            "msmarco": "ms_marco"
        }
        self.current_dataset = None
        self.test_samples = []
        
    def load_dataset(self, dataset_name: str, num_samples: int = 50):
        if dataset_name == "squad":
            dataset = load_dataset("squad_v2", split="validation")
            samples = dataset.select(range(num_samples))
            self.test_samples = [
                {
                    "question": sample["question"],
                    "ground_truth": sample["answers"]["text"][0] if sample["answers"]["text"] else "",
                    "context": sample["context"]
                }
                for sample in samples
                if sample["answers"]["text"]  # Filter out samples without answers
            ]
        elif dataset_name == "msmarco":
            dataset = load_dataset("ms_marco", "v2.1", split="train")
            samples = dataset.select(range(num_samples))
            self.test_samples = [
                {
                    "question": sample["query"],
                    "ground_truth": sample["answers"][0] if sample["answers"] else "",
                    "context": sample["passages"]["passage_text"][0]
                }
                for sample in samples
                if sample["answers"]  # Filter out samples without answers
            ]
        self.current_dataset = dataset_name
        return self.test_samples
    
    def evaluate_configuration(self, 
                             vector_db,
                             qa_chain,
                             splitting_strategy: str,
                             chunk_size: str) -> Dict:
        if not self.test_samples:
            return {"error": "No dataset loaded"}
            
        results = []
        for sample in self.test_samples:
            response = qa_chain.invoke({
                "question": sample["question"],
                "chat_history": []
            })
            
            results.append({
                "question": sample["question"],
                "answer": response["answer"],
                "contexts": [doc.page_content for doc in response["source_documents"]],
                "ground_truths": [sample["ground_truth"]]
            })
        
        # Convert to RAGAS dataset format
        eval_dataset = Dataset.from_list(results)
        
        # Calculate RAGAS metrics
        metrics = [
            ContextRecall(),
            AnswerRelevancy(),
            Faithfulness(),
            ContextPrecision()
        ]
        
        scores = evaluate(
            eval_dataset,
            metrics=metrics
        )
        
        return {
            "configuration": f"{splitting_strategy}_{chunk_size}",
            "context_recall": float(scores['context_recall']),
            "answer_relevancy": float(scores['answer_relevancy']),
            "faithfulness": float(scores['faithfulness']),
            "context_precision": float(scores['context_precision']),
            "average_score": float(np.mean([
                scores['context_recall'],
                scores['answer_relevancy'],
                scores['faithfulness'],
                scores['context_precision']
            ]))
        }

def demo():
    evaluator = RAGEvaluator()
    
    with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="sky")) as demo:
        vector_db = gr.State()
        qa_chain = gr.State()
        
        gr.HTML("<center><h1>Enhanced RAG PDF Chatbot with Evaluation</h1></center>")
        
        with gr.Tabs():
            # Custom PDF Tab
            with gr.Tab("Custom PDF Chat"):
                # Your existing UI components here
                with gr.Row():
                    with gr.Column(scale=86):
                        gr.Markdown("<b>Step 1 - Configure and Initialize RAG Pipeline</b>")
                        with gr.Row():
                            document = gr.Files(height=300, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF documents")
                        
                        with gr.Row():
                            splitting_strategy = gr.Radio(
                                ["recursive", "fixed", "token"],
                                label="Text Splitting Strategy",
                                value="recursive"
                            )
                            db_choice = gr.Dropdown(
                                ["faiss", "chroma"],
                                label="Vector Database",
                                value="faiss"
                            )
                            chunk_size = gr.Radio(
                                ["small", "medium"],
                                label="Chunk Size",
                                value="medium"
                            )
                        
                        # Rest of your existing UI components...

            # Evaluation Tab
            with gr.Tab("RAG Evaluation"):
                with gr.Row():
                    dataset_choice = gr.Dropdown(
                        choices=list(evaluator.datasets.keys()),
                        label="Select Evaluation Dataset",
                        value="squad"
                    )
                    load_dataset_btn = gr.Button("Load Dataset")
                
                with gr.Row():
                    dataset_info = gr.JSON(label="Dataset Information")
                
                with gr.Row():
                    eval_splitting_strategy = gr.Radio(
                        ["recursive", "fixed", "token"],
                        label="Text Splitting Strategy",
                        value="recursive"
                    )
                    eval_chunk_size = gr.Radio(
                        ["small", "medium"],
                        label="Chunk Size",
                        value="medium"
                    )
                
                with gr.Row():
                    evaluate_btn = gr.Button("Run Evaluation")
                    evaluation_results = gr.DataFrame(label="Evaluation Results")

        # Event handlers
        def load_dataset_handler(dataset_name):
            samples = evaluator.load_dataset(dataset_name)
            return {
                "dataset": dataset_name,
                "num_samples": len(samples),
                "sample_questions": [s["question"] for s in samples[:3]]
            }
        
        def run_evaluation(dataset_choice, splitting_strategy, chunk_size, vector_db, qa_chain):
            if not evaluator.current_dataset:
                return pd.DataFrame()
                
            results = evaluator.evaluate_configuration(
                vector_db=vector_db,
                qa_chain=qa_chain,
                splitting_strategy=splitting_strategy,
                chunk_size=chunk_size
            )
            
            # Convert results to DataFrame
            df = pd.DataFrame([results])
            return df
        
        # Connect event handlers
        load_dataset_btn.click(
            load_dataset_handler,
            inputs=[dataset_choice],
            outputs=[dataset_info]
        )
        
        evaluate_btn.click(
            run_evaluation,
            inputs=[
                dataset_choice,
                eval_splitting_strategy,
                eval_chunk_size,
                vector_db,
                qa_chain
            ],
            outputs=[evaluation_results]
        )
        
        qachain_btn.click(
            initialize_llmchain,  # Fixed function name here
            inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db],
            outputs=[qa_chain, llm_progress]
        ).then(
            lambda: [None, "", 0, "", 0, "", 0],
            inputs=None,
            outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
            queue=False
        )
        
        msg.submit(conversation,
            inputs=[qa_chain, msg, chatbot],
            outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
            queue=False
        )
        
        submit_btn.click(conversation,
            inputs=[qa_chain, msg, chatbot],
            outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
            queue=False
        )
        
        clear_btn.click(
            lambda: [None, "", 0, "", 0, "", 0],
            inputs=None,
            outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
            queue=False
        )
    demo.queue().launch(debug=True)

if __name__ == "__main__":
    demo()