Spaces:
Build error
Build error
Ryan
commited on
Commit
·
fd06f0b
1
Parent(s):
ab35b41
update
Browse files- app.py +12 -6
- dataset/person-harris.txt +2 -4
- dataset/person-trump.txt +2 -4
- dataset/topic-foreign_policy.txt +2 -4
- dataset/topic-the_economy.txt +2 -4
- processors/__init__.py +8 -0
- processors/metrics.py +107 -0
- ui/__init__.py +8 -0
- ui/analysis_screen.py +47 -84
- ui/dataset_input.py +18 -4
- utils/__init__.py +8 -0
- visualizers/__init__.py +8 -0
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from ui.dataset_input import create_dataset_input, load_example_dataset
|
3 |
-
from ui.analysis_screen import
|
4 |
|
5 |
def create_app():
|
6 |
"""
|
@@ -22,7 +22,7 @@ def create_app():
|
|
22 |
load_example_btn.click(
|
23 |
fn=load_example_dataset,
|
24 |
inputs=[example_dropdown],
|
25 |
-
outputs=[
|
26 |
)
|
27 |
|
28 |
# Save dataset to state
|
@@ -39,12 +39,18 @@ def create_app():
|
|
39 |
|
40 |
# Analysis Tab
|
41 |
with gr.Tab("Analysis"):
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Run analysis
|
45 |
run_analysis_btn.click(
|
46 |
-
fn=
|
47 |
-
inputs=[dataset_state, analysis_options, analysis_params],
|
48 |
outputs=[analysis_results_state, analysis_output]
|
49 |
)
|
50 |
|
@@ -52,4 +58,4 @@ def create_app():
|
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
app = create_app()
|
55 |
-
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from ui.dataset_input import create_dataset_input, load_example_dataset
|
3 |
+
from ui.analysis_screen import create_analysis_screen # Import create_analysis_screen instead
|
4 |
|
5 |
def create_app():
|
6 |
"""
|
|
|
22 |
load_example_btn.click(
|
23 |
fn=load_example_dataset,
|
24 |
inputs=[example_dropdown],
|
25 |
+
outputs=[prompt, response1, model1, response2, model2] # Update all field values
|
26 |
)
|
27 |
|
28 |
# Save dataset to state
|
|
|
39 |
|
40 |
# Analysis Tab
|
41 |
with gr.Tab("Analysis"):
|
42 |
+
# Use create_analysis_screen instead of process_analysis_request
|
43 |
+
analysis_options, analysis_params, run_analysis_btn, analysis_output = create_analysis_screen()
|
44 |
+
|
45 |
+
# Define a helper function to call process_analysis_request
|
46 |
+
def run_analysis(dataset, analysis_options, analysis_params):
|
47 |
+
from ui.analysis_screen import process_analysis_request
|
48 |
+
return process_analysis_request(dataset, analysis_options, analysis_params)
|
49 |
|
50 |
# Run analysis
|
51 |
run_analysis_btn.click(
|
52 |
+
fn=run_analysis,
|
53 |
+
inputs=[dataset_state, analysis_options, analysis_params],
|
54 |
outputs=[analysis_results_state, analysis_output]
|
55 |
)
|
56 |
|
|
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
app = create_app()
|
61 |
+
app.launch()
|
dataset/person-harris.txt
CHANGED
@@ -34,9 +34,7 @@ For the most current and detailed information, it's advisable to refer to recent
|
|
34 |
\llm=
|
35 |
ExaOne3.5
|
36 |
|
37 |
-
\
|
38 |
-
Tell me about the political views of Kamala Harris.
|
39 |
-
\response=
|
40 |
Kamala Harris, currently serving as the Vice President of the United States under Joe Biden, has held diverse political views throughout her career. Here's a broad overview:
|
41 |
|
42 |
1. **Democratic Party**: As a senator from California and now Vice President, Harris is affiliated with the Democratic Party. She generally supports progressive policies within this framework.
|
@@ -56,5 +54,5 @@ Kamala Harris, currently serving as the Vice President of the United States unde
|
|
56 |
5. **Foreign Policy**: As Vice President, Harris continues to develop her foreign policy views under the Biden administration's direction, but she has previously emphasized diplomacy over military intervention where possible. She's also focused on human rights and international women's issues in her global outlook.
|
57 |
|
58 |
Remember, political views can evolve over time based on experiences, new information, or shifts within one's party. This is a general summary and may not reflect every stance she has taken throughout her career. For the most accurate and detailed understanding of her political views at any given moment, it's best to refer to her recent public statements and policy proposals.
|
59 |
-
\
|
60 |
Granite3.2
|
|
|
34 |
\llm=
|
35 |
ExaOne3.5
|
36 |
|
37 |
+
\response2=
|
|
|
|
|
38 |
Kamala Harris, currently serving as the Vice President of the United States under Joe Biden, has held diverse political views throughout her career. Here's a broad overview:
|
39 |
|
40 |
1. **Democratic Party**: As a senator from California and now Vice President, Harris is affiliated with the Democratic Party. She generally supports progressive policies within this framework.
|
|
|
54 |
5. **Foreign Policy**: As Vice President, Harris continues to develop her foreign policy views under the Biden administration's direction, but she has previously emphasized diplomacy over military intervention where possible. She's also focused on human rights and international women's issues in her global outlook.
|
55 |
|
56 |
Remember, political views can evolve over time based on experiences, new information, or shifts within one's party. This is a general summary and may not reflect every stance she has taken throughout her career. For the most accurate and detailed understanding of her political views at any given moment, it's best to refer to her recent public statements and policy proposals.
|
57 |
+
\llm2=
|
58 |
Granite3.2
|
dataset/person-trump.txt
CHANGED
@@ -38,9 +38,7 @@ It's important to note that political views can evolve over time, and interpreta
|
|
38 |
\llm=
|
39 |
ExaOne3.5
|
40 |
|
41 |
-
\
|
42 |
-
Tell me about the political views of Donald Trump.
|
43 |
-
\response=
|
44 |
Donald Trump, who served as the 45th President of the United States from 2017 to 2021, is known for his populist and conservative political views. Here are some key aspects of his political stance:
|
45 |
|
46 |
1. **Economy**: Trump generally advocates for low taxes, deregulation, and policies that favor business interests. He believes in reducing corporate income tax rates to stimulate economic growth and job creation.
|
@@ -56,5 +54,5 @@ Donald Trump, who served as the 45th President of the United States from 2017 to
|
|
56 |
6. **Healthcare**: Trump opposed the Affordable Care Act (Obamacare), advocating for its repeal and replace with a plan that would offer lower-cost, high-quality health insurance to Americans. His administration took steps to weaken certain aspects of Obamacare through executive actions.
|
57 |
|
58 |
Remember, political views can be complex and multifaceted, evolving over time. This summary provides a broad overview but does not capture all nuances or changes in Trump's stated positions throughout his career.
|
59 |
-
\
|
60 |
Granite3.2
|
|
|
38 |
\llm=
|
39 |
ExaOne3.5
|
40 |
|
41 |
+
\response2=
|
|
|
|
|
42 |
Donald Trump, who served as the 45th President of the United States from 2017 to 2021, is known for his populist and conservative political views. Here are some key aspects of his political stance:
|
43 |
|
44 |
1. **Economy**: Trump generally advocates for low taxes, deregulation, and policies that favor business interests. He believes in reducing corporate income tax rates to stimulate economic growth and job creation.
|
|
|
54 |
6. **Healthcare**: Trump opposed the Affordable Care Act (Obamacare), advocating for its repeal and replace with a plan that would offer lower-cost, high-quality health insurance to Americans. His administration took steps to weaken certain aspects of Obamacare through executive actions.
|
55 |
|
56 |
Remember, political views can be complex and multifaceted, evolving over time. This summary provides a broad overview but does not capture all nuances or changes in Trump's stated positions throughout his career.
|
57 |
+
\llm2=
|
58 |
Granite3.2
|
dataset/topic-foreign_policy.txt
CHANGED
@@ -53,9 +53,7 @@ These perspectives can vary widely among individuals within each ideological spe
|
|
53 |
\llm=
|
54 |
ExaOne3.5
|
55 |
|
56 |
-
\
|
57 |
-
tell me about the left and ring wing views in the united states on the topic of 'foreign policy'
|
58 |
-
\response=
|
59 |
In U.S. politics, "left" and "right" are broad terms that often translate to liberal (or progressive) and conservative viewpoints, respectively. These ideological differences significantly influence perspectives on foreign policy. Here’s a general overview of how left-leaning and right-leaning views might approach key aspects of U.S. foreign policy:
|
60 |
|
61 |
### Left-Wing/Progressive Views on Foreign Policy:
|
@@ -83,5 +81,5 @@ In U.S. politics, "left" and "right" are broad terms that often translate to lib
|
|
83 |
5. **Border Control and Immigration:** Conservative foreign policy often includes strict border control measures and immigration policies aimed at preserving national security and cultural identity, which can sometimes manifest in more restrictive stances on refugee intake or legal immigration paths.
|
84 |
|
85 |
It's crucial to remember that these are broad generalizations. Within both camps (left/progressive and right/conservative), there is significant diversity of opinion on specific foreign policy issues, with many nuanced positions and variations among politicians and policymakers.
|
86 |
-
\
|
87 |
Granite3.2
|
|
|
53 |
\llm=
|
54 |
ExaOne3.5
|
55 |
|
56 |
+
\response2=
|
|
|
|
|
57 |
In U.S. politics, "left" and "right" are broad terms that often translate to liberal (or progressive) and conservative viewpoints, respectively. These ideological differences significantly influence perspectives on foreign policy. Here’s a general overview of how left-leaning and right-leaning views might approach key aspects of U.S. foreign policy:
|
58 |
|
59 |
### Left-Wing/Progressive Views on Foreign Policy:
|
|
|
81 |
5. **Border Control and Immigration:** Conservative foreign policy often includes strict border control measures and immigration policies aimed at preserving national security and cultural identity, which can sometimes manifest in more restrictive stances on refugee intake or legal immigration paths.
|
82 |
|
83 |
It's crucial to remember that these are broad generalizations. Within both camps (left/progressive and right/conservative), there is significant diversity of opinion on specific foreign policy issues, with many nuanced positions and variations among politicians and policymakers.
|
84 |
+
\llm2=
|
85 |
Granite3.2
|
dataset/topic-the_economy.txt
CHANGED
@@ -56,9 +56,7 @@ These perspectives reflect broader ideological differences but also highlight ar
|
|
56 |
\llm=
|
57 |
ExaOne3.5
|
58 |
|
59 |
-
\
|
60 |
-
tell me about the left and ring wing views in the united states on the topic of 'the economy'
|
61 |
-
\response=
|
62 |
In the United States, political ideologies are typically categorized into a broad spectrum ranging from conservative (or right-wing) to liberal (or left-wing). Here's how these perspectives generally align regarding key economic issues:
|
63 |
|
64 |
### Right-Wing/Conservative Views on the Economy:
|
@@ -93,5 +91,5 @@ In the United States, political ideologies are typically categorized into a broa
|
|
93 |
- **Inequality vs. Efficiency**: Liberals often prioritize addressing income and wealth disparities, while conservatives generally place a higher emphasis on economic efficiency and growth.
|
94 |
|
95 |
These broad strokes do not capture the nuances within each political group; many individuals may hold views that blend or contradict these generalizations. Additionally, specific policy proposals can vary widely based on factors like regional economics, industry-specific interests, and personal experiences with economic conditions.
|
96 |
-
\
|
97 |
Granite3.2
|
|
|
56 |
\llm=
|
57 |
ExaOne3.5
|
58 |
|
59 |
+
\response2=
|
|
|
|
|
60 |
In the United States, political ideologies are typically categorized into a broad spectrum ranging from conservative (or right-wing) to liberal (or left-wing). Here's how these perspectives generally align regarding key economic issues:
|
61 |
|
62 |
### Right-Wing/Conservative Views on the Economy:
|
|
|
91 |
- **Inequality vs. Efficiency**: Liberals often prioritize addressing income and wealth disparities, while conservatives generally place a higher emphasis on economic efficiency and growth.
|
92 |
|
93 |
These broad strokes do not capture the nuances within each political group; many individuals may hold views that blend or contradict these generalizations. Additionally, specific policy proposals can vary widely based on factors like regional economics, industry-specific interests, and personal experiences with economic conditions.
|
94 |
+
\llm2=
|
95 |
Granite3.2
|
processors/__init__.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# processors/__init__.py
|
2 |
+
# Empty file to make the directory a Python package
|
3 |
+
|
4 |
+
# ui/__init__.py
|
5 |
+
# Empty file to make the directory a Python package
|
6 |
+
|
7 |
+
# utils/__init__.py
|
8 |
+
# Empty file to make the directory a Python package
|
processors/metrics.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Similarity metrics for text comparison
|
3 |
+
"""
|
4 |
+
import numpy as np
|
5 |
+
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
|
6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
+
|
8 |
+
def calculate_cosine_similarity(text1, text2):
|
9 |
+
"""
|
10 |
+
Calculate cosine similarity between two texts using TF-IDF vectorization.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
text1 (str): First text
|
14 |
+
text2 (str): Second text
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
float: Cosine similarity score between 0 and 1
|
18 |
+
"""
|
19 |
+
# Create TF-IDF vectorizer
|
20 |
+
vectorizer = TfidfVectorizer()
|
21 |
+
|
22 |
+
try:
|
23 |
+
# Transform texts into TF-IDF vectors
|
24 |
+
tfidf_matrix = vectorizer.fit_transform([text1, text2])
|
25 |
+
|
26 |
+
# Calculate cosine similarity
|
27 |
+
similarity = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix[1:2])[0][0]
|
28 |
+
return float(similarity)
|
29 |
+
except Exception as e:
|
30 |
+
print(f"Error calculating cosine similarity: {e}")
|
31 |
+
return 0.0
|
32 |
+
|
33 |
+
def calculate_jaccard_similarity(text1, text2):
|
34 |
+
"""
|
35 |
+
Calculate Jaccard similarity between two texts (word-level).
|
36 |
+
|
37 |
+
Args:
|
38 |
+
text1 (str): First text
|
39 |
+
text2 (str): Second text
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
float: Jaccard similarity score between 0 and 1
|
43 |
+
"""
|
44 |
+
# Convert to sets of words
|
45 |
+
words1 = set(text1.lower().split())
|
46 |
+
words2 = set(text2.lower().split())
|
47 |
+
|
48 |
+
# Calculate Jaccard similarity
|
49 |
+
if not words1 and not words2:
|
50 |
+
return 1.0 # If both are empty, they're identical
|
51 |
+
|
52 |
+
try:
|
53 |
+
intersection = len(words1.intersection(words2))
|
54 |
+
union = len(words1.union(words2))
|
55 |
+
return intersection / union
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error calculating Jaccard similarity: {e}")
|
58 |
+
return 0.0
|
59 |
+
|
60 |
+
def calculate_semantic_similarity(text1, text2):
|
61 |
+
"""
|
62 |
+
Calculate pseudo-semantic similarity by comparing word overlap patterns.
|
63 |
+
|
64 |
+
This is a simplified approach that doesn't use embedding models like Word2Vec or BERT.
|
65 |
+
|
66 |
+
Args:
|
67 |
+
text1 (str): First text
|
68 |
+
text2 (str): Second text
|
69 |
+
|
70 |
+
Returns:
|
71 |
+
float: Semantic similarity score between 0 and 1
|
72 |
+
"""
|
73 |
+
# For now, this is a weighted combination of cosine and Jaccard similarity
|
74 |
+
# In a real app, you'd use a proper semantic model
|
75 |
+
cosine = calculate_cosine_similarity(text1, text2)
|
76 |
+
jaccard = calculate_jaccard_similarity(text1, text2)
|
77 |
+
|
78 |
+
# Weight more towards cosine similarity
|
79 |
+
return 0.7 * cosine + 0.3 * jaccard
|
80 |
+
|
81 |
+
def calculate_similarity(text1, text2, metrics=None):
|
82 |
+
"""
|
83 |
+
Calculate various similarity metrics between two texts.
|
84 |
+
|
85 |
+
Args:
|
86 |
+
text1 (str): First text
|
87 |
+
text2 (str): Second text
|
88 |
+
metrics (list): List of metrics to calculate
|
89 |
+
|
90 |
+
Returns:
|
91 |
+
dict: Dictionary of similarity scores
|
92 |
+
"""
|
93 |
+
if metrics is None:
|
94 |
+
metrics = ["Cosine Similarity", "Jaccard Similarity", "Semantic Similarity"]
|
95 |
+
|
96 |
+
results = {}
|
97 |
+
|
98 |
+
if "Cosine Similarity" in metrics:
|
99 |
+
results["cosine_similarity"] = calculate_cosine_similarity(text1, text2)
|
100 |
+
|
101 |
+
if "Jaccard Similarity" in metrics:
|
102 |
+
results["jaccard_similarity"] = calculate_jaccard_similarity(text1, text2)
|
103 |
+
|
104 |
+
if "Semantic Similarity" in metrics:
|
105 |
+
results["semantic_similarity"] = calculate_semantic_similarity(text1, text2)
|
106 |
+
|
107 |
+
return results
|
ui/__init__.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# processors/__init__.py
|
2 |
+
# Empty file to make the directory a Python package
|
3 |
+
|
4 |
+
# ui/__init__.py
|
5 |
+
# Empty file to make the directory a Python package
|
6 |
+
|
7 |
+
# utils/__init__.py
|
8 |
+
# Empty file to make the directory a Python package
|
ui/analysis_screen.py
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
|
4 |
-
# Import analysis modules
|
5 |
-
#
|
6 |
-
#from processors.
|
7 |
-
#from processors.
|
|
|
8 |
from processors.bow_analysis import compare_bow
|
9 |
-
#from processors.metrics import calculate_similarity
|
10 |
-
#from processors.diff_highlighter import highlight_differences
|
11 |
|
12 |
def create_analysis_screen():
|
13 |
"""
|
@@ -32,24 +33,22 @@ def create_analysis_screen():
|
|
32 |
"Difference Highlighting"
|
33 |
],
|
34 |
value=[
|
35 |
-
"Topic Modeling",
|
36 |
-
"N-gram Analysis",
|
37 |
"Bag of Words",
|
38 |
-
"Similarity Metrics"
|
39 |
],
|
40 |
label="Select Analyses to Run"
|
41 |
)
|
42 |
|
43 |
# Parameters for each analysis type
|
44 |
-
|
|
|
45 |
# Topic modeling parameters
|
46 |
-
with gr.Group():
|
47 |
gr.Markdown("### Topic Modeling Parameters")
|
48 |
topic_count = gr.Slider(minimum=2, maximum=10, value=3, step=1,
|
49 |
label="Number of Topics")
|
50 |
|
51 |
# N-gram parameters
|
52 |
-
with gr.Group():
|
53 |
gr.Markdown("### N-gram Parameters")
|
54 |
ngram_n = gr.Radio(choices=["1", "2", "3"], value="2",
|
55 |
label="N-gram Size")
|
@@ -57,7 +56,7 @@ def create_analysis_screen():
|
|
57 |
label="Top N-grams to Display")
|
58 |
|
59 |
# Bias detection parameters
|
60 |
-
with gr.Group():
|
61 |
gr.Markdown("### Bias Detection Parameters")
|
62 |
bias_methods = gr.CheckboxGroup(
|
63 |
choices=["Sentiment Analysis", "Partisan Leaning", "Framing Analysis"],
|
@@ -66,19 +65,36 @@ def create_analysis_screen():
|
|
66 |
)
|
67 |
|
68 |
# Bag of Words parameters
|
69 |
-
with gr.Group():
|
70 |
gr.Markdown("### Bag of Words Parameters")
|
71 |
bow_top = gr.Slider(minimum=10, maximum=100, value=25, step=5,
|
72 |
label="Top Words to Compare")
|
73 |
|
74 |
# Similarity metrics parameters
|
75 |
-
with gr.Group():
|
76 |
gr.Markdown("### Similarity Metrics Parameters")
|
77 |
similarity_metrics = gr.CheckboxGroup(
|
78 |
choices=["Cosine Similarity", "Jaccard Similarity", "Semantic Similarity"],
|
79 |
value=["Cosine Similarity", "Semantic Similarity"],
|
80 |
label="Similarity Metrics to Calculate"
|
81 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# Run analysis button
|
84 |
run_analysis_btn = gr.Button("Run Analysis", variant="primary", size="large")
|
@@ -105,76 +121,23 @@ def process_analysis_request(dataset, selected_analyses, parameters):
|
|
105 |
|
106 |
analysis_results = {"analyses": {}}
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
for entry in dataset["entries"]
|
111 |
-
|
112 |
-
prompts[entry["prompt"]] = []
|
113 |
-
prompts[entry["prompt"]].append({
|
114 |
-
"model": entry["model"],
|
115 |
-
"response": entry["response"]
|
116 |
-
})
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
# Run Topic Modeling
|
127 |
-
if "Topic Modeling" in selected_analyses:
|
128 |
-
num_topics = parameters.get("topic_count", 3)
|
129 |
-
topic_results = compare_topics(response_texts, model_names, num_topics)
|
130 |
-
analysis_results["analyses"][prompt]["topic_modeling"] = topic_results
|
131 |
-
|
132 |
-
# Run N-gram Analysis
|
133 |
-
if "N-gram Analysis" in selected_analyses:
|
134 |
-
n = int(parameters.get("ngram_n", 2))
|
135 |
-
top_n = parameters.get("ngram_top", 10)
|
136 |
-
ngram_results = compare_ngrams(response_texts, model_names, n, top_n)
|
137 |
-
analysis_results["analyses"][prompt]["ngram_analysis"] = ngram_results
|
138 |
-
|
139 |
-
# Run Bias Detection
|
140 |
-
if "Bias Detection" in selected_analyses:
|
141 |
-
bias_methods = parameters.get("bias_methods", ["Sentiment Analysis", "Partisan Leaning"])
|
142 |
-
bias_results = compare_bias(response_texts, model_names, bias_methods)
|
143 |
-
analysis_results["analyses"][prompt]["bias_detection"] = bias_results
|
144 |
-
|
145 |
-
# Run Bag of Words Analysis
|
146 |
-
if "Bag of Words" in selected_analyses:
|
147 |
-
top_words = parameters.get("bow_top", 25)
|
148 |
-
bow_results = compare_bow(response_texts, model_names, top_words)
|
149 |
-
analysis_results["analyses"][prompt]["bag_of_words"] = bow_results
|
150 |
-
|
151 |
-
# Run Similarity Metrics
|
152 |
-
if "Similarity Metrics" in selected_analyses:
|
153 |
-
metrics = parameters.get("similarity_metrics", ["Cosine Similarity"])
|
154 |
-
similarity_results = {}
|
155 |
-
|
156 |
-
# Calculate pairwise similarities
|
157 |
-
for i in range(len(responses)):
|
158 |
-
for j in range(i+1, len(responses)):
|
159 |
-
model_pair = f"{model_names[i]} vs {model_names[j]}"
|
160 |
-
similarity_results[model_pair] = calculate_similarity(
|
161 |
-
response_texts[i], response_texts[j], metrics
|
162 |
-
)
|
163 |
-
|
164 |
-
analysis_results["analyses"][prompt]["similarity_metrics"] = similarity_results
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
# Calculate pairwise differences
|
171 |
-
for i in range(len(responses)):
|
172 |
-
for j in range(i+1, len(responses)):
|
173 |
-
model_pair = f"{model_names[i]} vs {model_names[j]}"
|
174 |
-
diff_results[model_pair] = highlight_differences(
|
175 |
-
response_texts[i], response_texts[j]
|
176 |
-
)
|
177 |
-
|
178 |
-
analysis_results["analyses"][prompt]["difference_highlighting"] = diff_results
|
179 |
|
180 |
return analysis_results, gr.update(visible=True, value=json.dumps(analysis_results, indent=2))
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
|
4 |
+
# Import analysis modules
|
5 |
+
# Uncomment these when implemented
|
6 |
+
# from processors.topic_modeling import extract_topics, compare_topics
|
7 |
+
# from processors.ngram_analysis import compare_ngrams
|
8 |
+
# from processors.bias_detection import compare_bias
|
9 |
from processors.bow_analysis import compare_bow
|
10 |
+
# from processors.metrics import calculate_similarity
|
11 |
+
# from processors.diff_highlighter import highlight_differences
|
12 |
|
13 |
def create_analysis_screen():
|
14 |
"""
|
|
|
33 |
"Difference Highlighting"
|
34 |
],
|
35 |
value=[
|
|
|
|
|
36 |
"Bag of Words",
|
|
|
37 |
],
|
38 |
label="Select Analyses to Run"
|
39 |
)
|
40 |
|
41 |
# Parameters for each analysis type
|
42 |
+
analysis_params = gr.Group()
|
43 |
+
with analysis_params:
|
44 |
# Topic modeling parameters
|
45 |
+
with gr.Group(visible=False) as topic_params:
|
46 |
gr.Markdown("### Topic Modeling Parameters")
|
47 |
topic_count = gr.Slider(minimum=2, maximum=10, value=3, step=1,
|
48 |
label="Number of Topics")
|
49 |
|
50 |
# N-gram parameters
|
51 |
+
with gr.Group(visible=False) as ngram_params:
|
52 |
gr.Markdown("### N-gram Parameters")
|
53 |
ngram_n = gr.Radio(choices=["1", "2", "3"], value="2",
|
54 |
label="N-gram Size")
|
|
|
56 |
label="Top N-grams to Display")
|
57 |
|
58 |
# Bias detection parameters
|
59 |
+
with gr.Group(visible=False) as bias_params:
|
60 |
gr.Markdown("### Bias Detection Parameters")
|
61 |
bias_methods = gr.CheckboxGroup(
|
62 |
choices=["Sentiment Analysis", "Partisan Leaning", "Framing Analysis"],
|
|
|
65 |
)
|
66 |
|
67 |
# Bag of Words parameters
|
68 |
+
with gr.Group() as bow_params:
|
69 |
gr.Markdown("### Bag of Words Parameters")
|
70 |
bow_top = gr.Slider(minimum=10, maximum=100, value=25, step=5,
|
71 |
label="Top Words to Compare")
|
72 |
|
73 |
# Similarity metrics parameters
|
74 |
+
with gr.Group(visible=False) as similarity_params:
|
75 |
gr.Markdown("### Similarity Metrics Parameters")
|
76 |
similarity_metrics = gr.CheckboxGroup(
|
77 |
choices=["Cosine Similarity", "Jaccard Similarity", "Semantic Similarity"],
|
78 |
value=["Cosine Similarity", "Semantic Similarity"],
|
79 |
label="Similarity Metrics to Calculate"
|
80 |
)
|
81 |
+
|
82 |
+
# Function to update parameter visibility based on selected analyses
|
83 |
+
def update_params_visibility(selected):
|
84 |
+
return {
|
85 |
+
topic_params: gr.update(visible="Topic Modeling" in selected),
|
86 |
+
ngram_params: gr.update(visible="N-gram Analysis" in selected),
|
87 |
+
bias_params: gr.update(visible="Bias Detection" in selected),
|
88 |
+
bow_params: gr.update(visible="Bag of Words" in selected),
|
89 |
+
similarity_params: gr.update(visible="Similarity Metrics" in selected)
|
90 |
+
}
|
91 |
+
|
92 |
+
# Set up event handler for analysis selection
|
93 |
+
analysis_options.change(
|
94 |
+
fn=update_params_visibility,
|
95 |
+
inputs=[analysis_options],
|
96 |
+
outputs=[topic_params, ngram_params, bias_params, bow_params, similarity_params]
|
97 |
+
)
|
98 |
|
99 |
# Run analysis button
|
100 |
run_analysis_btn = gr.Button("Run Analysis", variant="primary", size="large")
|
|
|
121 |
|
122 |
analysis_results = {"analyses": {}}
|
123 |
|
124 |
+
# Extract prompt and responses
|
125 |
+
prompt = dataset["entries"][0]["prompt"]
|
126 |
+
response_texts = [entry["response"] for entry in dataset["entries"]]
|
127 |
+
model_names = [entry["model"] for entry in dataset["entries"]]
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
+
analysis_results["analyses"][prompt] = {}
|
130 |
+
|
131 |
+
# Currently only implement Bag of Words since it's the most complete
|
132 |
+
if "Bag of Words" in selected_analyses:
|
133 |
+
top_words = 25 # Default value if not in parameters
|
134 |
+
if parameters and hasattr(parameters, "get"):
|
135 |
+
if "bow_top" in parameters:
|
136 |
+
top_words = parameters["bow_top"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
bow_results = compare_bow(response_texts, model_names, top_words)
|
139 |
+
analysis_results["analyses"][prompt]["bag_of_words"] = bow_results
|
140 |
+
|
141 |
+
# Future implementations for other analysis types would go here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
return analysis_results, gr.update(visible=True, value=json.dumps(analysis_results, indent=2))
|
ui/dataset_input.py
CHANGED
@@ -7,11 +7,18 @@ def create_dataset_input():
|
|
7 |
Create the dataset input interface with prompt, response, and model fields.
|
8 |
|
9 |
Returns:
|
10 |
-
tuple: (dataset_inputs, example_dropdown, load_example_btn, create_btn)
|
11 |
"""
|
12 |
# Get built-in text datasets
|
13 |
text_datasets_dir = os.path.join("dataset")
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
with gr.Column() as dataset_inputs:
|
17 |
gr.Markdown("## LLM Response Comparison Dataset")
|
@@ -58,10 +65,17 @@ def load_example_dataset(file_name):
|
|
58 |
"""
|
59 |
file_path = os.path.join("dataset", file_name)
|
60 |
if os.path.exists(file_path):
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
else:
|
63 |
print(f"File not found: {file_path}")
|
64 |
-
return
|
65 |
|
66 |
|
67 |
def create_user_dataset(prompt, response1, model1, response2, model2):
|
|
|
7 |
Create the dataset input interface with prompt, response, and model fields.
|
8 |
|
9 |
Returns:
|
10 |
+
tuple: (dataset_inputs, example_dropdown, load_example_btn, create_btn, prompt, response1, model1, response2, model2)
|
11 |
"""
|
12 |
# Get built-in text datasets
|
13 |
text_datasets_dir = os.path.join("dataset")
|
14 |
+
# Check if directory exists
|
15 |
+
if os.path.exists(text_datasets_dir):
|
16 |
+
text_datasets = [file.name for file in os.scandir(text_datasets_dir)
|
17 |
+
if file.is_file() and file.name.endswith(".txt")]
|
18 |
+
else:
|
19 |
+
# Create directory if it doesn't exist
|
20 |
+
os.makedirs(text_datasets_dir, exist_ok=True)
|
21 |
+
text_datasets = []
|
22 |
|
23 |
with gr.Column() as dataset_inputs:
|
24 |
gr.Markdown("## LLM Response Comparison Dataset")
|
|
|
65 |
"""
|
66 |
file_path = os.path.join("dataset", file_name)
|
67 |
if os.path.exists(file_path):
|
68 |
+
dataset = load_text_file(file_path)
|
69 |
+
return (
|
70 |
+
dataset.get("prompt", ""),
|
71 |
+
dataset.get("response1", ""),
|
72 |
+
dataset.get("model1", ""),
|
73 |
+
dataset.get("response2", ""),
|
74 |
+
dataset.get("model2", "")
|
75 |
+
)
|
76 |
else:
|
77 |
print(f"File not found: {file_path}")
|
78 |
+
return "", "", "", "", "" # Return empty strings for all fields
|
79 |
|
80 |
|
81 |
def create_user_dataset(prompt, response1, model1, response2, model2):
|
utils/__init__.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# processors/__init__.py
|
2 |
+
# Empty file to make the directory a Python package
|
3 |
+
|
4 |
+
# ui/__init__.py
|
5 |
+
# Empty file to make the directory a Python package
|
6 |
+
|
7 |
+
# utils/__init__.py
|
8 |
+
# Empty file to make the directory a Python package
|
visualizers/__init__.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# processors/__init__.py
|
2 |
+
# Empty file to make the directory a Python package
|
3 |
+
|
4 |
+
# ui/__init__.py
|
5 |
+
# Empty file to make the directory a Python package
|
6 |
+
|
7 |
+
# utils/__init__.py
|
8 |
+
# Empty file to make the directory a Python package
|