sango07 commited on
Commit
448c349
Β·
verified Β·
1 Parent(s): 77e0e4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -170
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import os
4
- import io
5
 
6
  # Import evaluation modules
7
  from phoenix_code import phoenix_eval
@@ -16,106 +16,89 @@ st.set_page_config(
16
  initial_sidebar_state="expanded"
17
  )
18
 
19
- # Function to create sample templates for each evaluation method
20
- def create_sample_template(evaluation_type):
21
- """
22
- Create a sample template DataFrame based on the evaluation type
23
- """
24
- templates = {
25
- "Phoenix": pd.DataFrame({
26
- 'question': ['What is machine learning?', 'Explain Python programming'],
27
- 'answer': ['Machine learning is...', 'Python is a programming language...'],
28
- 'cleaned_context': ['Context about machine learning', 'Context about Python programming']
29
- }),
30
- "RAGAS": pd.DataFrame({
31
- 'question': ['What is AI?', 'Describe data science'],
32
- 'answer': ['Artificial Intelligence is...', 'Data science involves...'],
33
- 'contexts': ['Detailed context about AI', 'Comprehensive context on data science'],
34
- 'ground_truth': ['Verified definition of AI', 'Verified explanation of data science']
35
- }),
36
- "Traditional Metrics": pd.DataFrame({
37
- 'question': ['What is deep learning?', 'Explain neural networks'],
38
- 'answer': ['Deep learning is...', 'Neural networks are...'],
39
- 'contexts': ['Context about deep learning', 'Context about neural networks']
40
- })
41
- }
42
-
43
- return templates.get(evaluation_type, pd.DataFrame())
44
 
45
- # Function to create a downloadable Excel file
46
- def create_downloadable_excel(evaluation_type):
47
- """
48
- Create and return a downloadable Excel file for the specified evaluation type
49
- """
50
- # Create a sample template
51
- template_df = create_sample_template(evaluation_type)
 
 
 
 
52
 
53
- # Save to a bytes buffer
54
- output = io.BytesIO()
55
- with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
56
- template_df.to_excel(writer, index=False, sheet_name='Sample Template')
57
-
58
- # Add a README sheet with column descriptions
59
- workbook = writer.book
60
- worksheet = workbook.add_worksheet('README')
61
-
62
- # Write column descriptions
63
- readme_text = [
64
- f"Sample Template for {evaluation_type} Evaluation",
65
- "",
66
- "Column Descriptions:",
67
- ]
68
-
69
- if evaluation_type == "Phoenix":
70
- readme_text.extend([
71
- "- 'question': The input query or prompt",
72
- "- 'answer': The generated response to the question",
73
- "- 'cleaned_context': The context used to generate the answer"
74
- ])
75
- elif evaluation_type == "RAGAS":
76
- readme_text.extend([
77
- "- 'question': The input query or prompt",
78
- "- 'answer': The generated response to the question",
79
- "- 'contexts': The context used to generate the answer",
80
- "- 'ground_truth': The verified or gold standard answer"
81
- ])
82
- else: # Traditional Metrics
83
- readme_text.extend([
84
- "- 'question': The input query or prompt",
85
- "- 'answer': The generated response to the question",
86
- "- 'contexts': The context used to generate the answer"
87
- ])
88
-
89
- # Write README text
90
- for i, line in enumerate(readme_text):
91
- worksheet.write(i, 0, line)
92
-
93
- output.seek(0)
94
- return output
95
 
 
96
  def main():
97
- # Custom CSS (keep the previous CSS from the last implementation)
98
  st.markdown("""
99
  <style>
100
- /* Previous CSS styles remain the same */
101
- .template-section {
102
- background-color: #F0F4F8;
103
- border-radius: 10px;
104
- padding: 20px;
105
- margin-bottom: 20px;
106
- border: 1px solid #E2E8F0;
107
  }
108
- .template-header {
109
  color: #2C3E50;
110
- margin-bottom: 15px;
 
 
 
 
111
  }
112
- .download-btn {
113
- background-color: #48BB78;
114
  color: white;
115
  border: none;
116
  border-radius: 6px;
117
  padding: 10px 20px;
118
- margin: 10px 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  }
120
  </style>
121
  """, unsafe_allow_html=True)
@@ -123,49 +106,10 @@ def main():
123
  # App Title
124
  st.markdown("<h1 class='stTitle'>πŸ” RAG Evaluation Toolkit</h1>", unsafe_allow_html=True)
125
 
126
- # Template Section
127
- st.markdown("<div class='template-section'>", unsafe_allow_html=True)
128
- st.markdown("<h2 class='template-header'>πŸ“ Data Template Guidelines</h2>", unsafe_allow_html=True)
129
-
130
- # Expandable sections for each evaluation type
131
- with st.expander("πŸ“Š Phoenix Evaluation Template"):
132
- st.write("Required Columns: 'question', 'answer', 'cleaned_context'")
133
- if st.button("Download Phoenix Template", key="phoenix_template"):
134
- phoenix_template = create_downloadable_excel("Phoenix")
135
- st.download_button(
136
- label="Save Phoenix Template",
137
- data=phoenix_template,
138
- file_name="phoenix_evaluation_template.xlsx",
139
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
140
- )
141
-
142
- with st.expander("πŸ“ˆ RAGAS Evaluation Template"):
143
- st.write("Required Columns: 'question', 'answer', 'contexts', 'ground_truth'")
144
- if st.button("Download RAGAS Template", key="ragas_template"):
145
- ragas_template = create_downloadable_excel("RAGAS")
146
- st.download_button(
147
- label="Save RAGAS Template",
148
- data=ragas_template,
149
- file_name="ragas_evaluation_template.xlsx",
150
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
151
- )
152
-
153
- with st.expander("πŸ“ Traditional Metrics Template"):
154
- st.write("Required Columns: 'question', 'answer', 'contexts'")
155
- if st.button("Download Traditional Metrics Template", key="traditional_template"):
156
- traditional_template = create_downloadable_excel("Traditional Metrics")
157
- st.download_button(
158
- label="Save Traditional Metrics Template",
159
- data=traditional_template,
160
- file_name="traditional_metrics_template.xlsx",
161
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
162
- )
163
- st.markdown("</div>", unsafe_allow_html=True)
164
-
165
- # Sidebar for Configuration (keep previous sidebar code)
166
  st.sidebar.header("πŸ“‹ Evaluation Configuration")
167
 
168
- # API Key Input
169
  st.sidebar.subheader("OpenAI API Key")
170
  openai_api_key = st.sidebar.text_input(
171
  "Enter your OpenAI API Key",
@@ -175,13 +119,7 @@ def main():
175
 
176
  # File Upload Section
177
  st.markdown("### πŸ“Š Upload Your Dataset")
178
- uploaded_file = st.file_uploader(
179
- "Upload Dataset",
180
- type=["csv", "xls", "xlsx"]
181
- )
182
-
183
- # Rest of the previous implementation follows...
184
- # (Keep the rest of the previous main() function code)
185
 
186
  # Evaluation Type Selection
187
  st.sidebar.subheader("πŸ›  Evaluation Methods")
@@ -220,42 +158,72 @@ def main():
220
  metrics
221
  )
222
 
223
- # Validation function for DataFrame columns
224
- def validate_dataframe(df, evaluation_type):
225
- """
226
- Validate DataFrame columns based on the evaluation type
227
- """
228
- required_columns = {
229
- "Phoenix": ['question', 'answer', 'cleaned_context'],
230
- "RAGAS": ['question', 'answer', 'contexts', 'ground_truth'],
231
- "Traditional Metrics": ['question', 'answer', 'contexts']
232
- }
233
-
234
- # Check columns for the selected evaluation methods
235
- for method in selected_metrics.keys():
236
- missing_columns = [col for col in required_columns.get(method, []) if col not in df.columns]
237
-
238
- if missing_columns:
239
- st.error(f"Missing required columns for {method}: {', '.join(missing_columns)}")
240
- return False
241
-
242
- return True
243
-
244
  # Evaluation Button
245
  if uploaded_file and openai_api_key and selected_metrics:
246
- # Load data
247
- file_extension = os.path.splitext(uploaded_file.name)[1]
248
- if file_extension.lower() == ".csv":
249
- df = pd.read_csv(uploaded_file)
250
- elif file_extension.lower() in [".xls", ".xlsx"]:
251
- df = pd.read_excel(uploaded_file)
252
-
253
- # Validate DataFrame
254
- if validate_dataframe(df, selected_metrics):
255
- if st.button("πŸš€ Run Evaluation"):
256
- # Rest of the evaluation code remains the same as in the previous implementation
257
- # (Keep the existing evaluation logic)
258
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
 
260
  # Run the app
261
  if __name__ == "__main__":
 
1
  import streamlit as st
2
  import pandas as pd
3
  import os
4
+ import base64
5
 
6
  # Import evaluation modules
7
  from phoenix_code import phoenix_eval
 
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)
 
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",
 
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")
 
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__":