Spaces:
Sleeping
Sleeping
Ryan
commited on
Commit
·
bb4ec07
1
Parent(s):
f533950
update
Browse files- app.py +11 -5
- ui/analysis_screen.py +11 -48
app.py
CHANGED
@@ -111,8 +111,15 @@ def create_app():
|
|
111 |
|
112 |
# Analysis Tab
|
113 |
with gr.Tab("Analysis"):
|
114 |
-
#
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
# Pre-create visualization components (initially hidden)
|
118 |
visualization_area_visible = gr.Checkbox(value=False, visible=False, label="Visualization Visible")
|
@@ -137,7 +144,7 @@ def create_app():
|
|
137 |
status_message = gr.Markdown(visible=False)
|
138 |
|
139 |
# Define a helper function to extract parameter values and run the analysis
|
140 |
-
def run_analysis(dataset, selected_analysis,
|
141 |
try:
|
142 |
if not dataset or "entries" not in dataset or not dataset["entries"]:
|
143 |
return (
|
@@ -158,7 +165,6 @@ def create_app():
|
|
158 |
)
|
159 |
|
160 |
parameters = {
|
161 |
-
"bow_top": bow_top,
|
162 |
"ngram_n": ngram_n,
|
163 |
"ngram_top": ngram_top,
|
164 |
"topic_count": topic_count
|
@@ -558,7 +564,7 @@ def create_app():
|
|
558 |
# Connect the run button to the analysis function
|
559 |
run_analysis_btn.click(
|
560 |
fn=run_analysis,
|
561 |
-
inputs=[dataset_state, analysis_options,
|
562 |
outputs=[
|
563 |
analysis_results_state,
|
564 |
analysis_output,
|
|
|
111 |
|
112 |
# Analysis Tab
|
113 |
with gr.Tab("Analysis"):
|
114 |
+
# Fix the value unpacking to match the actual return values from create_analysis_screen()
|
115 |
+
analysis_components = create_analysis_screen()
|
116 |
+
analysis_options = analysis_components[0]
|
117 |
+
analysis_params = analysis_components[1]
|
118 |
+
run_analysis_btn = analysis_components[2]
|
119 |
+
analysis_output = analysis_components[3]
|
120 |
+
ngram_n = analysis_components[4]
|
121 |
+
ngram_top = analysis_components[5]
|
122 |
+
topic_count = analysis_components[6]
|
123 |
|
124 |
# Pre-create visualization components (initially hidden)
|
125 |
visualization_area_visible = gr.Checkbox(value=False, visible=False, label="Visualization Visible")
|
|
|
144 |
status_message = gr.Markdown(visible=False)
|
145 |
|
146 |
# Define a helper function to extract parameter values and run the analysis
|
147 |
+
def run_analysis(dataset, selected_analysis, ngram_n, ngram_top, topic_count):
|
148 |
try:
|
149 |
if not dataset or "entries" not in dataset or not dataset["entries"]:
|
150 |
return (
|
|
|
165 |
)
|
166 |
|
167 |
parameters = {
|
|
|
168 |
"ngram_n": ngram_n,
|
169 |
"ngram_top": ngram_top,
|
170 |
"topic_count": topic_count
|
|
|
564 |
# Connect the run button to the analysis function
|
565 |
run_analysis_btn.click(
|
566 |
fn=run_analysis,
|
567 |
+
inputs=[dataset_state, analysis_options, ngram_n, ngram_top, topic_count],
|
568 |
outputs=[
|
569 |
analysis_results_state,
|
570 |
analysis_output,
|
ui/analysis_screen.py
CHANGED
@@ -14,7 +14,7 @@ def create_analysis_screen():
|
|
14 |
Create the analysis options screen
|
15 |
|
16 |
Returns:
|
17 |
-
tuple: (analysis_options, analysis_params, run_analysis_btn, analysis_output,
|
18 |
"""
|
19 |
with gr.Column() as analysis_screen:
|
20 |
gr.Markdown("## Analysis Options")
|
@@ -35,14 +35,6 @@ def create_analysis_screen():
|
|
35 |
label="Select Analysis Type"
|
36 |
)
|
37 |
|
38 |
-
# Create slider directly here for easier access
|
39 |
-
gr.Markdown("### Bag of Words Parameters")
|
40 |
-
bow_top_slider = gr.Slider(
|
41 |
-
minimum=10, maximum=100, value=25, step=5,
|
42 |
-
label="Top Words to Compare",
|
43 |
-
elem_id="bow_top_slider"
|
44 |
-
)
|
45 |
-
|
46 |
# Create N-gram parameters accessible at top level
|
47 |
ngram_n = gr.Radio(
|
48 |
choices=["1", "2", "3"], value="2",
|
@@ -61,15 +53,6 @@ def create_analysis_screen():
|
|
61 |
label="Number of Topics",
|
62 |
visible=False
|
63 |
)
|
64 |
-
|
65 |
-
bias_methods = gr.CheckboxGroup(
|
66 |
-
choices=["sentiment", "partisan", "framing"],
|
67 |
-
label="Bias Detection Methods",
|
68 |
-
value=["sentiment", "partisan"],
|
69 |
-
visible=False, # Initially hidden, will be shown when Bias Detection is selected
|
70 |
-
interactive=True
|
71 |
-
)
|
72 |
-
|
73 |
|
74 |
# Parameters for each analysis type
|
75 |
with gr.Group() as analysis_params:
|
@@ -83,15 +66,10 @@ def create_analysis_screen():
|
|
83 |
gr.Markdown("### N-gram Parameters")
|
84 |
# We're already using ngram_n and ngram_top defined above
|
85 |
|
86 |
-
# Bias detection parameters
|
87 |
with gr.Group(visible=False) as bias_params:
|
88 |
gr.Markdown("### Bias Detection Parameters")
|
89 |
-
|
90 |
-
choices=["Sentiment Analysis", "Partisan Leaning", "Framing Analysis"],
|
91 |
-
value=["Sentiment Analysis", "Partisan Leaning"],
|
92 |
-
label="Bias Detection Methods",
|
93 |
-
interactive=True # Ensure this is interactive
|
94 |
-
)
|
95 |
|
96 |
# Classifier parameters
|
97 |
with gr.Group(visible=False) as classifier_params:
|
@@ -108,7 +86,6 @@ def create_analysis_screen():
|
|
108 |
ngram_n: gr.update(visible=selected == "N-gram Analysis"),
|
109 |
ngram_top: gr.update(visible=selected == "N-gram Analysis"),
|
110 |
topic_count: gr.update(visible=selected == "Topic Modeling"),
|
111 |
-
bow_top_slider: gr.update(visible=selected == "Bag of Words")
|
112 |
}
|
113 |
|
114 |
# Set up event handler for analysis selection
|
@@ -123,7 +100,6 @@ def create_analysis_screen():
|
|
123 |
ngram_n,
|
124 |
ngram_top,
|
125 |
topic_count,
|
126 |
-
bow_top_slider
|
127 |
]
|
128 |
)
|
129 |
|
@@ -133,8 +109,8 @@ def create_analysis_screen():
|
|
133 |
# Analysis output area - hidden JSON component to store raw results
|
134 |
analysis_output = gr.JSON(label="Analysis Results", visible=False)
|
135 |
|
136 |
-
# Return the components needed by app.py,
|
137 |
-
return analysis_options, analysis_params, run_analysis_btn, analysis_output,
|
138 |
|
139 |
# Add the implementation of these helper functions
|
140 |
def extract_important_words(text, top_n=20):
|
@@ -370,12 +346,8 @@ def process_analysis_request(dataset, selected_analysis, parameters):
|
|
370 |
|
371 |
# Process based on the selected analysis type
|
372 |
if selected_analysis == "Bag of Words":
|
373 |
-
#
|
374 |
-
top_n =
|
375 |
-
if isinstance(top_n, str):
|
376 |
-
top_n = int(top_n)
|
377 |
-
|
378 |
-
print(f"Using top_n value: {top_n}") # Debug print
|
379 |
|
380 |
# Perform Bag of Words analysis using the processor
|
381 |
bow_results = compare_bow(
|
@@ -448,10 +420,7 @@ def process_analysis_request(dataset, selected_analysis, parameters):
|
|
448 |
}
|
449 |
|
450 |
elif selected_analysis == "Bias Detection":
|
451 |
-
#
|
452 |
-
bias_methods = parameters.get("bias_methods",
|
453 |
-
["Sentiment Analysis", "Partisan Leaning", "Framing Analysis"])
|
454 |
-
|
455 |
try:
|
456 |
# Perform bias detection analysis
|
457 |
bias_results = compare_bias(
|
@@ -460,25 +429,19 @@ def process_analysis_request(dataset, selected_analysis, parameters):
|
|
460 |
model_names=[model1_name, model2_name]
|
461 |
)
|
462 |
|
463 |
-
# Filter results
|
464 |
filtered_results = {"models": [model1_name, model2_name]}
|
465 |
|
466 |
# Always include comparative data
|
467 |
if "comparative" in bias_results:
|
468 |
filtered_results["comparative"] = bias_results["comparative"]
|
469 |
|
470 |
-
# Include
|
471 |
for model in [model1_name, model2_name]:
|
472 |
filtered_results[model] = {}
|
473 |
|
474 |
-
if
|
475 |
-
filtered_results[model]["sentiment"] = bias_results[model]["sentiment"]
|
476 |
-
|
477 |
-
if "Partisan Leaning" in bias_methods and model in bias_results:
|
478 |
filtered_results[model]["partisan"] = bias_results[model]["partisan"]
|
479 |
-
|
480 |
-
if "Framing Analysis" in bias_methods and model in bias_results:
|
481 |
-
filtered_results[model]["framing"] = bias_results[model]["framing"]
|
482 |
|
483 |
results["analyses"][prompt_text]["bias_detection"] = filtered_results
|
484 |
|
|
|
14 |
Create the analysis options screen
|
15 |
|
16 |
Returns:
|
17 |
+
tuple: (analysis_options, analysis_params, run_analysis_btn, analysis_output, ngram_n, ngram_top, topic_count)
|
18 |
"""
|
19 |
with gr.Column() as analysis_screen:
|
20 |
gr.Markdown("## Analysis Options")
|
|
|
35 |
label="Select Analysis Type"
|
36 |
)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Create N-gram parameters accessible at top level
|
39 |
ngram_n = gr.Radio(
|
40 |
choices=["1", "2", "3"], value="2",
|
|
|
53 |
label="Number of Topics",
|
54 |
visible=False
|
55 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# Parameters for each analysis type
|
58 |
with gr.Group() as analysis_params:
|
|
|
66 |
gr.Markdown("### N-gram Parameters")
|
67 |
# We're already using ngram_n and ngram_top defined above
|
68 |
|
69 |
+
# Bias detection parameters - simplified with no checkboxes
|
70 |
with gr.Group(visible=False) as bias_params:
|
71 |
gr.Markdown("### Bias Detection Parameters")
|
72 |
+
gr.Markdown("Using partisan leaning bias detection")
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
# Classifier parameters
|
75 |
with gr.Group(visible=False) as classifier_params:
|
|
|
86 |
ngram_n: gr.update(visible=selected == "N-gram Analysis"),
|
87 |
ngram_top: gr.update(visible=selected == "N-gram Analysis"),
|
88 |
topic_count: gr.update(visible=selected == "Topic Modeling"),
|
|
|
89 |
}
|
90 |
|
91 |
# Set up event handler for analysis selection
|
|
|
100 |
ngram_n,
|
101 |
ngram_top,
|
102 |
topic_count,
|
|
|
103 |
]
|
104 |
)
|
105 |
|
|
|
109 |
# Analysis output area - hidden JSON component to store raw results
|
110 |
analysis_output = gr.JSON(label="Analysis Results", visible=False)
|
111 |
|
112 |
+
# Return the components needed by app.py, with bow_top_slider removed
|
113 |
+
return analysis_options, analysis_params, run_analysis_btn, analysis_output, ngram_n, ngram_top, topic_count
|
114 |
|
115 |
# Add the implementation of these helper functions
|
116 |
def extract_important_words(text, top_n=20):
|
|
|
346 |
|
347 |
# Process based on the selected analysis type
|
348 |
if selected_analysis == "Bag of Words":
|
349 |
+
# Use fixed default value of 25 for top_n
|
350 |
+
top_n = 25
|
|
|
|
|
|
|
|
|
351 |
|
352 |
# Perform Bag of Words analysis using the processor
|
353 |
bow_results = compare_bow(
|
|
|
420 |
}
|
421 |
|
422 |
elif selected_analysis == "Bias Detection":
|
423 |
+
# Use partisan leaning bias detection by default
|
|
|
|
|
|
|
424 |
try:
|
425 |
# Perform bias detection analysis
|
426 |
bias_results = compare_bias(
|
|
|
429 |
model_names=[model1_name, model2_name]
|
430 |
)
|
431 |
|
432 |
+
# Filter results to only include partisan leaning
|
433 |
filtered_results = {"models": [model1_name, model2_name]}
|
434 |
|
435 |
# Always include comparative data
|
436 |
if "comparative" in bias_results:
|
437 |
filtered_results["comparative"] = bias_results["comparative"]
|
438 |
|
439 |
+
# Include only partisan leaning for each model
|
440 |
for model in [model1_name, model2_name]:
|
441 |
filtered_results[model] = {}
|
442 |
|
443 |
+
if model in bias_results:
|
|
|
|
|
|
|
444 |
filtered_results[model]["partisan"] = bias_results[model]["partisan"]
|
|
|
|
|
|
|
445 |
|
446 |
results["analyses"][prompt_text]["bias_detection"] = filtered_results
|
447 |
|