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4 |
+
import base64
|
5 |
+
|
6 |
+
# Import evaluation modules
|
7 |
+
from phoenix_code import phoenix_eval
|
8 |
+
from ragas_code import ragas_eval
|
9 |
+
from traditional_metrics_score import RAGEvaluator
|
10 |
+
|
11 |
+
# Set page configuration
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="RAG Evaluation Toolkit",
|
14 |
+
page_icon="π",
|
15 |
+
layout="wide",
|
16 |
+
initial_sidebar_state="expanded"
|
17 |
+
)
|
18 |
+
|
19 |
+
# Custom CSS for improved styling
|
20 |
+
def local_css(file_name):
|
21 |
+
with open(file_name) as f:
|
22 |
+
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
23 |
+
|
24 |
+
# Function to create a more visually appealing file uploader
|
25 |
+
def custom_file_uploader():
|
26 |
+
st.markdown("""
|
27 |
+
<div class="file-upload-container">
|
28 |
+
<div class="file-upload-icon">π</div>
|
29 |
+
<div class="file-upload-text">
|
30 |
+
Drag and Drop or <span class="file-upload-browse">Browse Files</span>
|
31 |
+
</div>
|
32 |
+
<small>Supports CSV, XLS, XLSX</small>
|
33 |
+
</div>
|
34 |
+
""", unsafe_allow_html=True)
|
35 |
+
|
36 |
+
uploaded_file = st.file_uploader(
|
37 |
+
"Upload Dataset",
|
38 |
+
type=["csv", "xls", "xlsx"],
|
39 |
+
label_visibility="collapsed"
|
40 |
+
)
|
41 |
+
return uploaded_file
|
42 |
+
|
43 |
+
# Main Streamlit App
|
44 |
+
def main():
|
45 |
+
# Custom CSS for enhanced styling
|
46 |
+
st.markdown("""
|
47 |
+
<style>
|
48 |
+
.stApp {
|
49 |
+
background-color: #f0f2f6;
|
50 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
51 |
+
}
|
52 |
+
.stTitle {
|
53 |
+
color: #2C3E50;
|
54 |
+
text-align: center;
|
55 |
+
margin-bottom: 30px;
|
56 |
+
}
|
57 |
+
.stMarkdown {
|
58 |
+
color: #34495E;
|
59 |
+
}
|
60 |
+
.stButton>button {
|
61 |
+
background-color: #3498DB;
|
62 |
+
color: white;
|
63 |
+
border: none;
|
64 |
+
border-radius: 6px;
|
65 |
+
padding: 10px 20px;
|
66 |
+
transition: all 0.3s ease;
|
67 |
+
}
|
68 |
+
.stButton>button:hover {
|
69 |
+
background-color: #2980B9;
|
70 |
+
transform: scale(1.05);
|
71 |
+
}
|
72 |
+
.sidebar .sidebar-content {
|
73 |
+
background-color: #FFFFFF;
|
74 |
+
border-radius: 10px;
|
75 |
+
padding: 20px;
|
76 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
77 |
+
}
|
78 |
+
.file-upload-container {
|
79 |
+
border: 2px dashed #3498DB;
|
80 |
+
border-radius: 10px;
|
81 |
+
padding: 30px;
|
82 |
+
text-align: center;
|
83 |
+
background-color: #FFFFFF;
|
84 |
+
transition: all 0.3s ease;
|
85 |
+
}
|
86 |
+
.file-upload-container:hover {
|
87 |
+
border-color: #2980B9;
|
88 |
+
background-color: #F1F8FF;
|
89 |
+
}
|
90 |
+
.file-upload-icon {
|
91 |
+
font-size: 50px;
|
92 |
+
color: #3498DB;
|
93 |
+
margin-bottom: 15px;
|
94 |
+
}
|
95 |
+
.file-upload-text {
|
96 |
+
color: #2C3E50;
|
97 |
+
font-size: 18px;
|
98 |
+
}
|
99 |
+
.file-upload-browse {
|
100 |
+
color: #3498DB;
|
101 |
+
font-weight: bold;
|
102 |
+
}
|
103 |
+
</style>
|
104 |
+
""", unsafe_allow_html=True)
|
105 |
+
|
106 |
+
# App Title
|
107 |
+
st.markdown("<h1 class='stTitle'>π RAG Evaluation Toolkit</h1>", unsafe_allow_html=True)
|
108 |
+
|
109 |
+
# Sidebar for Configuration
|
110 |
+
st.sidebar.header("π Evaluation Configuration")
|
111 |
+
|
112 |
+
# API Key Input with improved styling
|
113 |
+
st.sidebar.subheader("OpenAI API Key")
|
114 |
+
openai_api_key = st.sidebar.text_input(
|
115 |
+
"Enter your OpenAI API Key",
|
116 |
+
type="password",
|
117 |
+
help="Required for running evaluations"
|
118 |
+
)
|
119 |
+
|
120 |
+
# File Upload Section
|
121 |
+
st.markdown("### π Upload Your Dataset")
|
122 |
+
uploaded_file = custom_file_uploader()
|
123 |
+
|
124 |
+
# Evaluation Type Selection
|
125 |
+
st.sidebar.subheader("π Evaluation Methods")
|
126 |
+
evaluation_methods = {
|
127 |
+
"Phoenix Evaluation": [
|
128 |
+
"hallucination",
|
129 |
+
"toxicity",
|
130 |
+
"relevance",
|
131 |
+
"Q&A"
|
132 |
+
],
|
133 |
+
"RAGAS Evaluation": [
|
134 |
+
"answer_correctness",
|
135 |
+
"answer_relevancy",
|
136 |
+
"faithfulness",
|
137 |
+
"context_precision",
|
138 |
+
"context_recall",
|
139 |
+
"context_relevancy",
|
140 |
+
"answer_similarity"
|
141 |
+
],
|
142 |
+
"Traditional Metrics": [
|
143 |
+
"BLEU",
|
144 |
+
"ROUGE-1",
|
145 |
+
"BERT Score",
|
146 |
+
"Perplexity",
|
147 |
+
"Diversity",
|
148 |
+
"Racial Bias"
|
149 |
+
]
|
150 |
+
}
|
151 |
+
|
152 |
+
# Multiselect for each evaluation method
|
153 |
+
selected_metrics = {}
|
154 |
+
for method, metrics in evaluation_methods.items():
|
155 |
+
if st.sidebar.checkbox(method):
|
156 |
+
selected_metrics[method] = st.sidebar.multiselect(
|
157 |
+
f"Select {method} Metrics",
|
158 |
+
metrics
|
159 |
+
)
|
160 |
+
|
161 |
+
# Evaluation Button
|
162 |
+
if uploaded_file and openai_api_key and selected_metrics:
|
163 |
+
if st.button("π Run Evaluation"):
|
164 |
+
# Load data
|
165 |
+
file_extension = os.path.splitext(uploaded_file.name)[1]
|
166 |
+
if file_extension.lower() == ".csv":
|
167 |
+
df = pd.read_csv(uploaded_file)
|
168 |
+
elif file_extension.lower() in [".xls", ".xlsx"]:
|
169 |
+
df = pd.read_excel(uploaded_file)
|
170 |
+
|
171 |
+
# Combine results
|
172 |
+
combined_results = pd.DataFrame()
|
173 |
+
|
174 |
+
# Progress bar
|
175 |
+
progress_bar = st.progress(0)
|
176 |
+
|
177 |
+
# Run evaluations
|
178 |
+
with st.spinner("Processing evaluations..."):
|
179 |
+
# Phoenix Evaluation
|
180 |
+
if "Phoenix Evaluation" in selected_metrics:
|
181 |
+
progress_bar.progress(33)
|
182 |
+
phoenix_results = phoenix_eval(
|
183 |
+
selected_metrics.get("Phoenix Evaluation", []),
|
184 |
+
openai_api_key,
|
185 |
+
df.copy()
|
186 |
+
)
|
187 |
+
combined_results = pd.concat([combined_results, phoenix_results], axis=1)
|
188 |
+
|
189 |
+
# RAGAS Evaluation
|
190 |
+
if "RAGAS Evaluation" in selected_metrics:
|
191 |
+
progress_bar.progress(66)
|
192 |
+
ragas_results = ragas_eval(
|
193 |
+
selected_metrics.get("RAGAS Evaluation", []),
|
194 |
+
openai_api_key,
|
195 |
+
df.copy()
|
196 |
+
)
|
197 |
+
combined_results = pd.concat([combined_results, ragas_results], axis=1)
|
198 |
+
|
199 |
+
# Traditional Metrics Evaluation
|
200 |
+
if "Traditional Metrics" in selected_metrics:
|
201 |
+
progress_bar.progress(100)
|
202 |
+
traditional_results = RAGEvaluator(
|
203 |
+
df=df.copy(),
|
204 |
+
selected_metrics=selected_metrics.get("Traditional Metrics", [])
|
205 |
+
)
|
206 |
+
combined_results = pd.concat([combined_results, traditional_results], axis=1)
|
207 |
+
|
208 |
+
# Save results
|
209 |
+
results_filename = "rag_evaluation_results.xlsx"
|
210 |
+
combined_results.to_excel(results_filename, index=False)
|
211 |
+
|
212 |
+
# Success message and download button
|
213 |
+
st.success("Evaluation Completed Successfully!")
|
214 |
+
|
215 |
+
# Create download button with improved styling
|
216 |
+
with open(results_filename, "rb") as file:
|
217 |
+
btn = st.download_button(
|
218 |
+
label="π₯ Download Evaluation Results",
|
219 |
+
data=file,
|
220 |
+
file_name=results_filename,
|
221 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
222 |
+
)
|
223 |
+
|
224 |
+
# Display results preview
|
225 |
+
st.markdown("### π Results Preview")
|
226 |
+
st.dataframe(combined_results)
|
227 |
+
|
228 |
+
# Run the app
|
229 |
+
if __name__ == "__main__":
|
230 |
+
main()
|