Spaces:
Sleeping
Sleeping
Ryan
commited on
Commit
·
360146a
1
Parent(s):
056e0ad
update
Browse files- ui/dataset_input.py +87 -0
- ui/main_screen.py +47 -0
- utils/text_dataset_parser.py +66 -0
ui/dataset_input.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from utils.text_dataset_parser import load_text_file, load_builtin_datasets
|
4 |
+
|
5 |
+
def create_dataset_input():
|
6 |
+
"""
|
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 |
+
text_datasets = [file.name for file in os.scandir(text_datasets_dir) if file.is_file() and file.name.endswith(".txt")]
|
15 |
+
|
16 |
+
with gr.Column() as dataset_inputs:
|
17 |
+
gr.Markdown("## LLM Response Comparison Dataset")
|
18 |
+
gr.Markdown("""
|
19 |
+
Enter a prompt and responses from two different LLMs for comparison,
|
20 |
+
or load one of the built-in datasets.
|
21 |
+
""")
|
22 |
+
|
23 |
+
# Example dataset selection
|
24 |
+
with gr.Row():
|
25 |
+
example_dropdown = gr.Dropdown(
|
26 |
+
choices=text_datasets,
|
27 |
+
value=text_datasets[0] if text_datasets else None,
|
28 |
+
label="Built-in Datasets",
|
29 |
+
info="Select a pre-made dataset to load"
|
30 |
+
)
|
31 |
+
load_example_btn = gr.Button("Load Dataset", variant="secondary")
|
32 |
+
|
33 |
+
# User input fields
|
34 |
+
gr.Markdown("### Create Your Own Dataset")
|
35 |
+
with gr.Row():
|
36 |
+
prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Enter a prompt/question...")
|
37 |
+
with gr.Row():
|
38 |
+
response1 = gr.Textbox(label="Response 1", lines=4, placeholder="Enter the first model's response...")
|
39 |
+
model1 = gr.Textbox(label="Model 1", placeholder="Enter the first model's name...")
|
40 |
+
with gr.Row():
|
41 |
+
response2 = gr.Textbox(label="Response 2", lines=4, placeholder="Enter the second model's response...")
|
42 |
+
model2 = gr.Textbox(label="Model 2", placeholder="Enter the second model's name...")
|
43 |
+
|
44 |
+
create_btn = gr.Button("Create Dataset", variant="primary")
|
45 |
+
|
46 |
+
return dataset_inputs, example_dropdown, load_example_btn, create_btn, prompt, response1, model1, response2, model2
|
47 |
+
|
48 |
+
|
49 |
+
def load_example_dataset(file_name):
|
50 |
+
"""
|
51 |
+
Load a built-in dataset from a text file.
|
52 |
+
|
53 |
+
Args:
|
54 |
+
file_name (str): Name of the text file to load.
|
55 |
+
|
56 |
+
Returns:
|
57 |
+
dict: Dataset entry with prompt, response1, model1, response2, and model2.
|
58 |
+
"""
|
59 |
+
file_path = os.path.join("dataset", file_name)
|
60 |
+
if os.path.exists(file_path):
|
61 |
+
return load_text_file(file_path)
|
62 |
+
else:
|
63 |
+
print(f"File not found: {file_path}")
|
64 |
+
return {"prompt": "", "response1": "", "model1": "", "response2": "", "model2": ""}
|
65 |
+
|
66 |
+
|
67 |
+
def create_user_dataset(prompt, response1, model1, response2, model2):
|
68 |
+
"""
|
69 |
+
Create a user-defined dataset entry.
|
70 |
+
|
71 |
+
Args:
|
72 |
+
prompt (str): The prompt text.
|
73 |
+
response1 (str): The first model's response.
|
74 |
+
model1 (str): The first model's name.
|
75 |
+
response2 (str): The second model's response.
|
76 |
+
model2 (str): The second model's name.
|
77 |
+
|
78 |
+
Returns:
|
79 |
+
dict: Dataset entry with prompt, response1, model1, response2, and model2.
|
80 |
+
"""
|
81 |
+
return {
|
82 |
+
"prompt": prompt.strip(),
|
83 |
+
"response1": response1.strip(),
|
84 |
+
"model1": model1.strip(),
|
85 |
+
"response2": response2.strip(),
|
86 |
+
"model2": model2.strip()
|
87 |
+
}
|
ui/main_screen.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
def create_main_screen():
|
4 |
+
"""
|
5 |
+
Create the main landing screen with app description and navigation
|
6 |
+
|
7 |
+
Returns:
|
8 |
+
tuple: (welcome_msg, about_info, get_started_btn)
|
9 |
+
"""
|
10 |
+
with gr.Column() as main_screen:
|
11 |
+
welcome_msg = gr.Markdown(
|
12 |
+
"""
|
13 |
+
# LLM Response Comparator
|
14 |
+
|
15 |
+
## Analyze and Compare Responses from Different LLMs on Political Topics
|
16 |
+
"""
|
17 |
+
)
|
18 |
+
|
19 |
+
about_info = gr.Markdown(
|
20 |
+
"""
|
21 |
+
### About This Tool
|
22 |
+
|
23 |
+
This application allows you to compare how different Large Language Models (LLMs) respond
|
24 |
+
to the same political prompts or questions. Using various NLP techniques, the tool analyzes:
|
25 |
+
|
26 |
+
- **Topic Modeling**: What key topics do different LLMs emphasize?
|
27 |
+
- **N-gram Analysis**: What phrases and word patterns are characteristic of each LLM?
|
28 |
+
- **Bias Detection**: Are there detectable biases in how LLMs approach political topics?
|
29 |
+
- **Text Classification**: How do responses cluster or differentiate?
|
30 |
+
- **Key Differences**: What specific content varies between models?
|
31 |
+
|
32 |
+
### How to Use
|
33 |
+
|
34 |
+
1. Navigate to the **Dataset Input** tab
|
35 |
+
2. Enter prompts and corresponding LLM responses, or load an example dataset
|
36 |
+
3. Run various analyses to see how the responses compare
|
37 |
+
4. Explore visualizations of the differences
|
38 |
+
5. Generate a comprehensive report of findings
|
39 |
+
|
40 |
+
This tool is for educational and research purposes to better understand how LLMs handle
|
41 |
+
politically sensitive topics.
|
42 |
+
"""
|
43 |
+
)
|
44 |
+
|
45 |
+
get_started_btn = gr.Button("Get Started", variant="primary", size="large")
|
46 |
+
|
47 |
+
return welcome_msg, about_info, get_started_btn
|
utils/text_dataset_parser.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Utility functions for parsing text-based dataset files for LLM response comparator.
|
3 |
+
"""
|
4 |
+
import re
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
def parse_text_file(file_path):
|
8 |
+
"""
|
9 |
+
Parse a text file to extract prompt, response1, model1, response2, and model2.
|
10 |
+
|
11 |
+
Format:
|
12 |
+
- \prompt= followed by the prompt text
|
13 |
+
- \response1= followed by the first model's response
|
14 |
+
- \model1= followed by the first model's name
|
15 |
+
- \response2= followed by the second model's response
|
16 |
+
- \model2= followed by the second model's name
|
17 |
+
|
18 |
+
Args:
|
19 |
+
file_path (str): Path to the text file.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
dict: Dictionary with prompt, response1, model1, response2, and model2.
|
23 |
+
"""
|
24 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
25 |
+
content = file.read()
|
26 |
+
|
27 |
+
# Extract sections using regular expressions
|
28 |
+
prompt = re.search(r'\\prompt=(.*?)(?=\\response1=|$)', content, re.DOTALL)
|
29 |
+
response1 = re.search(r'\\response1=(.*?)(?=\\model1=|$)', content, re.DOTALL)
|
30 |
+
model1 = re.search(r'\\model1=(.*?)(?=\\response2=|$)', content, re.DOTALL)
|
31 |
+
response2 = re.search(r'\\response2=(.*?)(?=\\model2=|$)', content, re.DOTALL)
|
32 |
+
model2 = re.search(r'\\model2=(.*?)(?=$)', content, re.DOTALL)
|
33 |
+
|
34 |
+
return {
|
35 |
+
"prompt": prompt.group(1).strip() if prompt else "",
|
36 |
+
"response1": response1.group(1).strip() if response1 else "",
|
37 |
+
"model1": model1.group(1).strip() if model1 else "",
|
38 |
+
"response2": response2.group(1).strip() if response2 else "",
|
39 |
+
"model2": model2.group(1).strip() if model2 else ""
|
40 |
+
}
|
41 |
+
|
42 |
+
def load_text_file(file_path):
|
43 |
+
"""
|
44 |
+
Load a single text file as a dataset entry.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
file_path (str): Path to the text file.
|
48 |
+
|
49 |
+
Returns:
|
50 |
+
dict: Dataset entry with prompt, response1, model1, response2, and model2.
|
51 |
+
"""
|
52 |
+
return parse_text_file(file_path)
|
53 |
+
|
54 |
+
def load_builtin_datasets(directory_path):
|
55 |
+
"""
|
56 |
+
Load all built-in datasets from a directory.
|
57 |
+
|
58 |
+
Args:
|
59 |
+
directory_path (str): Path to the directory containing text files.
|
60 |
+
|
61 |
+
Returns:
|
62 |
+
list: List of dataset entries.
|
63 |
+
"""
|
64 |
+
path = Path(directory_path)
|
65 |
+
text_files = list(path.glob('*.txt'))
|
66 |
+
return [parse_text_file(str(file_path)) for file_path in text_files]
|