Spaces:
Sleeping
Sleeping
Ryan
commited on
Commit
·
daf2b71
1
Parent(s):
e29951e
update
Browse files- .idea/workspace.xml +1 -1
- app.py +15 -21
- ui/analysis_screen.py +40 -30
- visualization/bow_visualizer.py +62 -25
.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="
|
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,
|
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 |
-
|
115 |
-
|
|
|
|
|
116 |
|
117 |
-
# Process
|
118 |
-
|
119 |
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
122 |
except Exception as e:
|
123 |
import traceback
|
124 |
-
|
125 |
-
print(
|
126 |
-
|
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 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
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 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
|
|
|
|
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
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
|
152 |
|
153 |
Args:
|
154 |
-
analysis_results (dict):
|
155 |
|
156 |
Returns:
|
157 |
-
list: List of gradio components
|
158 |
"""
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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```")]
|