File size: 7,379 Bytes
2444fb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import os
import base64

# Import evaluation modules
from phoenix_code import phoenix_eval
from ragas_code import ragas_eval
from traditional_metrics_score import RAGEvaluator

# Set page configuration
st.set_page_config(
    page_title="RAG Evaluation Toolkit",
    page_icon="πŸ”",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS for improved styling
def local_css(file_name):
    with open(file_name) as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

# Function to create a more visually appealing file uploader
def custom_file_uploader():
    st.markdown("""
    <div class="file-upload-container">
        <div class="file-upload-icon">πŸ“‚</div>
        <div class="file-upload-text">
            Drag and Drop or <span class="file-upload-browse">Browse Files</span>
        </div>
        <small>Supports CSV, XLS, XLSX</small>
    </div>
    """, unsafe_allow_html=True)
    
    uploaded_file = st.file_uploader(
        "Upload Dataset", 
        type=["csv", "xls", "xlsx"], 
        label_visibility="collapsed"
    )
    return uploaded_file

# Main Streamlit App
def main():
    # Custom CSS for enhanced styling
    st.markdown("""
    <style>
    .stApp {
        background-color: #f0f2f6;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    .stTitle {
        color: #2C3E50;
        text-align: center;
        margin-bottom: 30px;
    }
    .stMarkdown {
        color: #34495E;
    }
    .stButton>button {
        background-color: #3498DB;
        color: white;
        border: none;
        border-radius: 6px;
        padding: 10px 20px;
        transition: all 0.3s ease;
    }
    .stButton>button:hover {
        background-color: #2980B9;
        transform: scale(1.05);
    }
    .sidebar .sidebar-content {
        background-color: #FFFFFF;
        border-radius: 10px;
        padding: 20px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }
    .file-upload-container {
        border: 2px dashed #3498DB;
        border-radius: 10px;
        padding: 30px;
        text-align: center;
        background-color: #FFFFFF;
        transition: all 0.3s ease;
    }
    .file-upload-container:hover {
        border-color: #2980B9;
        background-color: #F1F8FF;
    }
    .file-upload-icon {
        font-size: 50px;
        color: #3498DB;
        margin-bottom: 15px;
    }
    .file-upload-text {
        color: #2C3E50;
        font-size: 18px;
    }
    .file-upload-browse {
        color: #3498DB;
        font-weight: bold;
    }
    </style>
    """, unsafe_allow_html=True)

    # App Title
    st.markdown("<h1 class='stTitle'>πŸ” RAG Evaluation Toolkit</h1>", unsafe_allow_html=True)
    
    # Sidebar for Configuration
    st.sidebar.header("πŸ“‹ Evaluation Configuration")
    
    # API Key Input with improved styling
    st.sidebar.subheader("OpenAI API Key")
    openai_api_key = st.sidebar.text_input(
        "Enter your OpenAI API Key", 
        type="password", 
        help="Required for running evaluations"
    )
    
    # File Upload Section
    st.markdown("### πŸ“Š Upload Your Dataset")
    uploaded_file = custom_file_uploader()
    
    # Evaluation Type Selection
    st.sidebar.subheader("πŸ›  Evaluation Methods")
    evaluation_methods = {
        "Phoenix Evaluation": [
            "hallucination", 
            "toxicity", 
            "relevance", 
            "Q&A"
        ],
        "RAGAS Evaluation": [
            "answer_correctness", 
            "answer_relevancy", 
            "faithfulness", 
            "context_precision", 
            "context_recall", 
            "context_relevancy", 
            "answer_similarity"
        ],
        "Traditional Metrics": [
            "BLEU", 
            "ROUGE-1", 
            "BERT Score", 
            "Perplexity", 
            "Diversity", 
            "Racial Bias"
        ]
    }
    
    # Multiselect for each evaluation method
    selected_metrics = {}
    for method, metrics in evaluation_methods.items():
        if st.sidebar.checkbox(method):
            selected_metrics[method] = st.sidebar.multiselect(
                f"Select {method} Metrics", 
                metrics
            )
    
    # Evaluation Button
    if uploaded_file and openai_api_key and selected_metrics:
        if st.button("πŸš€ Run Evaluation"):
            # Load data
            file_extension = os.path.splitext(uploaded_file.name)[1]
            if file_extension.lower() == ".csv":
                df = pd.read_csv(uploaded_file)
            elif file_extension.lower() in [".xls", ".xlsx"]:
                df = pd.read_excel(uploaded_file)
            
            # Combine results
            combined_results = pd.DataFrame()
            
            # Progress bar
            progress_bar = st.progress(0)
            
            # Run evaluations
            with st.spinner("Processing evaluations..."):
                # Phoenix Evaluation
                if "Phoenix Evaluation" in selected_metrics:
                    progress_bar.progress(33)
                    phoenix_results = phoenix_eval(
                        selected_metrics.get("Phoenix Evaluation", []), 
                        openai_api_key, 
                        df.copy()
                    )
                    combined_results = pd.concat([combined_results, phoenix_results], axis=1)
                
                # RAGAS Evaluation
                if "RAGAS Evaluation" in selected_metrics:
                    progress_bar.progress(66)
                    ragas_results = ragas_eval(
                        selected_metrics.get("RAGAS Evaluation", []), 
                        openai_api_key, 
                        df.copy()
                    )
                    combined_results = pd.concat([combined_results, ragas_results], axis=1)
                
                # Traditional Metrics Evaluation
                if "Traditional Metrics" in selected_metrics:
                    progress_bar.progress(100)
                    traditional_results = RAGEvaluator(
                        df=df.copy(), 
                        selected_metrics=selected_metrics.get("Traditional Metrics", [])
                    )
                    combined_results = pd.concat([combined_results, traditional_results], axis=1)
                
                # Save results
                results_filename = "rag_evaluation_results.xlsx"
                combined_results.to_excel(results_filename, index=False)
                
                # Success message and download button
                st.success("Evaluation Completed Successfully!")
                
                # Create download button with improved styling
                with open(results_filename, "rb") as file:
                    btn = st.download_button(
                        label="πŸ“₯ Download Evaluation Results",
                        data=file,
                        file_name=results_filename,
                        mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                    )
                
                # Display results preview
                st.markdown("### πŸ“Š Results Preview")
                st.dataframe(combined_results)

# Run the app
if __name__ == "__main__":
    main()