Ryan commited on
Commit
daf2b71
·
1 Parent(s): e29951e
.idea/workspace.xml CHANGED
@@ -53,7 +53,7 @@
53
  <option name="presentableId" value="Default" />
54
  <updated>1745170754325</updated>
55
  <workItem from="1745170755404" duration="245000" />
56
- <workItem from="1745172030020" duration="1408000" />
57
  </task>
58
  <servers />
59
  </component>
 
53
  <option name="presentableId" value="Default" />
54
  <updated>1745170754325</updated>
55
  <workItem from="1745170755404" duration="245000" />
56
+ <workItem from="1745172030020" duration="2043000" />
57
  </task>
58
  <servers />
59
  </component>
app.py CHANGED
@@ -100,32 +100,26 @@ def create_app():
100
  analysis_options, analysis_params, run_analysis_btn, analysis_output, bow_top_slider, visualization_container = create_analysis_screen()
101
 
102
  # Define a helper function to extract parameter values and call process_analysis_request
103
- def run_analysis(dataset, selected_analyses, bow_top_value):
104
- # Check if dataset exists
105
- if not dataset or "entries" not in dataset or not dataset["entries"]:
106
- error_components = [gr.Markdown("❌ **Error:** No dataset provided. Please create a dataset in the Dataset Input tab first.")]
107
- return {}, gr.update(visible=False), gr.update(visible=True, value=error_components)
108
-
109
- # Create parameters dictionary with the slider value
110
- params = {"bow_top": bow_top_value}
111
-
112
- # Call the process_analysis_request function with proper parameters
113
  try:
114
- results, _ = process_analysis_request(dataset, selected_analyses, params)
115
- print(f"Analysis completed successfully")
 
 
116
 
117
- # Process and visualize the results
118
- visualization_components = process_and_visualize_analysis(results)
119
 
120
- return results, gr.update(visible=False, value=results), gr.update(visible=True, value=visualization_components)
121
-
 
 
 
122
  except Exception as e:
123
  import traceback
124
- error_trace = traceback.format_exc()
125
- print(f"Error in analysis: {e}")
126
- print(f"Full traceback: {error_trace}")
127
- error_components = [gr.Markdown(f"❌ **Error during analysis:** {str(e)}")]
128
- return {}, gr.update(visible=False), gr.update(visible=True, value=error_components)
129
 
130
  # Run analysis with proper parameters
131
  run_analysis_btn.click(
 
100
  analysis_options, analysis_params, run_analysis_btn, analysis_output, bow_top_slider, visualization_container = create_analysis_screen()
101
 
102
  # Define a helper function to extract parameter values and call process_analysis_request
103
+ def run_analysis(dataset, selected_analyses, bow_top):
 
 
 
 
 
 
 
 
 
104
  try:
105
+ parameters = {
106
+ "bow_top": bow_top,
107
+ }
108
+ print("Running analysis with parameters:", parameters)
109
 
110
+ # Process the analysis request
111
+ analysis_results, output_update = process_analysis_request(dataset, selected_analyses, parameters)
112
 
113
+ # Generate visualization components
114
+ print("Generating visualization components...")
115
+ visualization_components = process_and_visualize_analysis(analysis_results)
116
+
117
+ return analysis_results, True, visualization_components
118
  except Exception as e:
119
  import traceback
120
+ error_msg = f"Error in run_analysis: {str(e)}\n{traceback.format_exc()}"
121
+ print(error_msg)
122
+ return {"error": error_msg}, True, [gr.Markdown(f"**Error:**\n\n```\n{error_msg}\n```")]
 
 
123
 
124
  # Run analysis with proper parameters
125
  run_analysis_btn.click(
ui/analysis_screen.py CHANGED
@@ -121,36 +121,46 @@ def process_analysis_request(dataset, selected_analyses, parameters):
121
  Returns:
122
  tuple: (analysis_results, analysis_output_display)
123
  """
124
- print(f"Processing analysis request with: {dataset}, {selected_analyses}, {parameters}")
125
-
126
- if not dataset or "entries" not in dataset or not dataset["entries"]:
127
- return {}, gr.update(visible=True, value=json.dumps({"error": "No dataset provided or dataset is empty"}, indent=2))
128
-
129
- analysis_results = {"analyses": {}}
130
-
131
- # Extract prompt and responses
132
- prompt = dataset["entries"][0]["prompt"]
133
- response_texts = [entry["response"] for entry in dataset["entries"]]
134
- model_names = [entry["model"] for entry in dataset["entries"]]
135
-
136
- analysis_results["analyses"][prompt] = {}
137
-
138
- # Currently only implement Bag of Words since it's the most complete
139
- if "Bag of Words" in selected_analyses:
140
- # Set a default value
141
- top_words = 25
142
 
143
- # Try to get the parameter from the parameters dict
144
- if parameters and isinstance(parameters, dict) and "bow_top" in parameters:
145
- top_words = parameters["bow_top"]
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
- print(f"Running BOW analysis with top_words={top_words}")
 
 
148
 
149
- # Call the BOW comparison function
150
- bow_results = compare_bow(response_texts, model_names, top_words)
151
- analysis_results["analyses"][prompt]["bag_of_words"] = bow_results
152
-
153
- print("Analysis complete - results:", analysis_results)
154
-
155
- # Return results and update the output component
156
- return analysis_results, gr.update(visible=False, value=analysis_results) # Hide the raw JSON
 
 
121
  Returns:
122
  tuple: (analysis_results, analysis_output_display)
123
  """
124
+ try:
125
+ print(f"Processing analysis request with: {selected_analyses}")
126
+ print(f"Parameters: {parameters}")
127
+
128
+ if not dataset or "entries" not in dataset or not dataset["entries"]:
129
+ return {}, gr.update(visible=True, value=json.dumps({"error": "No dataset provided or dataset is empty"}, indent=2))
130
+
131
+ analysis_results = {"analyses": {}}
132
+
133
+ # Extract prompt and responses
134
+ prompt = dataset["entries"][0]["prompt"]
135
+ response_texts = [entry["response"] for entry in dataset["entries"]]
136
+ model_names = [entry["model"] for entry in dataset["entries"]]
 
 
 
 
 
137
 
138
+ print(f"Analyzing prompt: '{prompt[:50]}...'")
139
+ print(f"Models: {model_names}")
140
+
141
+ analysis_results["analyses"][prompt] = {}
142
+
143
+ # Currently only implement Bag of Words since it's the most complete
144
+ if "Bag of Words" in selected_analyses:
145
+ # Set a default value
146
+ top_words = 25
147
+
148
+ # Try to get the parameter from the parameters dict
149
+ if parameters and isinstance(parameters, dict) and "bow_top" in parameters:
150
+ top_words = parameters["bow_top"]
151
+
152
+ print(f"Running BOW analysis with top_words={top_words}")
153
 
154
+ # Call the BOW comparison function
155
+ bow_results = compare_bow(response_texts, model_names, top_words)
156
+ analysis_results["analyses"][prompt]["bag_of_words"] = bow_results
157
 
158
+ print("Analysis complete - results:", analysis_results)
159
+
160
+ # Return results and update the output component
161
+ return analysis_results, gr.update(visible=False, value=analysis_results) # Hide the raw JSON
162
+ except Exception as e:
163
+ import traceback
164
+ error_msg = f"Analysis error: {str(e)}\n{traceback.format_exc()}"
165
+ print(error_msg)
166
+ return {}, gr.update(visible=True, value=json.dumps({"error": error_msg}, indent=2))
visualization/bow_visualizer.py CHANGED
@@ -146,35 +146,72 @@ def create_bow_visualization(analysis_results):
146
 
147
  return output_components
148
 
 
 
 
149
  def process_and_visualize_analysis(analysis_results):
150
  """
151
- Process analysis results and create visualizations
152
 
153
  Args:
154
- analysis_results (dict): Analysis results
155
 
156
  Returns:
157
- list: List of gradio components with visualizations
158
  """
159
- if not analysis_results:
160
- return [gr.Markdown("No analysis results available. Please run an analysis first.")]
161
-
162
- all_components = []
163
-
164
- # Display the JSON output in a collapsible section for debugging
165
- json_text = json.dumps(analysis_results, indent=2)
166
- all_components.append(gr.Markdown("### Raw Analysis Results (Expandable)"))
167
- all_components.append(gr.Markdown("<details><summary>Click to view raw JSON results</summary>\n\n```json\n" + json_text + "\n```\n\n</details>"))
168
-
169
- # Check if bag of words analysis is present in any prompt's results
170
- has_bow = False
171
- for prompt_results in analysis_results.get("analyses", {}).values():
172
- if "bag_of_words" in prompt_results:
173
- has_bow = True
174
- break
175
-
176
- # Create visualizations for Bag of Words if present
177
- if has_bow:
178
- all_components.extend(create_bow_visualization(analysis_results))
179
-
180
- return all_components
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  return output_components
148
 
149
+ import gradio as gr
150
+ import traceback
151
+
152
  def process_and_visualize_analysis(analysis_results):
153
  """
154
+ Process the analysis results and create visualization components
155
 
156
  Args:
157
+ analysis_results (dict): The analysis results
158
 
159
  Returns:
160
+ list: List of gradio components for visualization
161
  """
162
+ try:
163
+ print(f"Starting visualization of analysis results: {type(analysis_results)}")
164
+ components = []
165
+
166
+ if not analysis_results or "analyses" not in analysis_results:
167
+ print("Warning: Empty or invalid analysis results")
168
+ components.append(gr.Markdown("No analysis results to visualize."))
169
+ return components
170
+
171
+ # For each prompt in the analysis results
172
+ for prompt, analyses in analysis_results.get("analyses", {}).items():
173
+ print(f"Visualizing results for prompt: {prompt[:30]}...")
174
+ components.append(gr.Markdown(f"## Analysis for Prompt:\n\"{prompt}\""))
175
+
176
+ # Check for Bag of Words analysis
177
+ if "bag_of_words" in analyses:
178
+ print("Processing Bag of Words visualization")
179
+ components.append(gr.Markdown("### Bag of Words Analysis"))
180
+ bow_results = analyses["bag_of_words"]
181
+
182
+ # Display models compared
183
+ if "models" in bow_results:
184
+ models = bow_results["models"]
185
+ components.append(gr.Markdown(f"**Models compared**: {', '.join(models)}"))
186
+
187
+ # Display important words for each model
188
+ if "important_words" in bow_results:
189
+ components.append(gr.Markdown("#### Most Common Words by Model"))
190
+
191
+ for model, words in bow_results["important_words"].items():
192
+ print(f"Creating word list for model {model}")
193
+ word_list = [f"{item['word']} ({item['count']})" for item in words[:10]]
194
+ components.append(gr.Markdown(f"**{model}**: {', '.join(word_list)}"))
195
+
196
+ # Display comparison metrics
197
+ if "comparisons" in bow_results:
198
+ components.append(gr.Markdown("#### Similarity Metrics"))
199
+
200
+ for comparison, metrics in bow_results["comparisons"].items():
201
+ cosine = metrics.get("cosine_similarity", 0)
202
+ jaccard = metrics.get("jaccard_similarity", 0)
203
+ components.append(gr.Markdown(
204
+ f"**{comparison}**:\n"
205
+ f"- Cosine similarity: {cosine:.2f}\n"
206
+ f"- Jaccard similarity: {jaccard:.2f}"
207
+ ))
208
+
209
+ if not components:
210
+ components.append(gr.Markdown("No visualization components could be created from the analysis results."))
211
+
212
+ print(f"Visualization complete: generated {len(components)} components")
213
+ return components
214
+ except Exception as e:
215
+ error_msg = f"Visualization error: {str(e)}\n{traceback.format_exc()}"
216
+ print(error_msg)
217
+ return [gr.Markdown(f"**Error during visualization:**\n\n```\n{error_msg}\n```")]