Spaces:
Sleeping
Sleeping
Create app.py
#1
by
sepp81
- opened
app.py
CHANGED
@@ -6,142 +6,117 @@ import os
|
|
6 |
from datetime import datetime
|
7 |
import ast
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
class RoFTGame:
|
10 |
-
|
11 |
-
|
12 |
-
Initialize the RoFT Game with the dataset
|
13 |
-
|
14 |
-
:param dataset_path: Path to the roft.csv file
|
15 |
-
"""
|
16 |
-
self.df = pd.read_csv(dataset_path)
|
17 |
-
self.current_sample = None
|
18 |
-
self.current_sentences = None
|
19 |
-
self.true_boundary_index = None
|
20 |
-
self.current_guess_index = None
|
21 |
-
|
22 |
-
# Predefined reasons from the dataset description
|
23 |
-
self.predefined_reasons = [
|
24 |
-
"grammar",
|
25 |
-
"repetition",
|
26 |
-
"irrelevant",
|
27 |
-
"contradicts_sentence",
|
28 |
-
"contradicts_knowledge",
|
29 |
-
"common_sense",
|
30 |
-
"coreference",
|
31 |
-
"generic"
|
32 |
-
]
|
33 |
-
|
34 |
-
def load_random_sample(self):
|
35 |
-
"""
|
36 |
-
Load a random sample from the dataset
|
37 |
-
"""
|
38 |
-
# Filter for samples with valid generations and reasons
|
39 |
-
valid_samples = self.df[
|
40 |
-
(self.df['gen_body'].notna()) &
|
41 |
-
(self.df['reason'].notna()) &
|
42 |
-
(self.df['reason'] != '[]')
|
43 |
-
]
|
44 |
-
|
45 |
-
# Select a random sample
|
46 |
-
self.current_sample = valid_samples.sample(n=1).iloc[0]
|
47 |
-
|
48 |
-
# Prepare sentences
|
49 |
-
prompt_sentences = self.current_sample['prompt_body'].split('_SEP_')
|
50 |
-
gen_sentences = self.current_sample['gen_body'].split('_SEP_')
|
51 |
-
|
52 |
-
# Combine and truncate to 10 sentences
|
53 |
-
combined_sentences = prompt_sentences + gen_sentences
|
54 |
-
self.current_sentences = combined_sentences[:10]
|
55 |
-
|
56 |
-
# Store true boundary
|
57 |
-
self.true_boundary_index = self.current_sample['true_boundary_index']
|
58 |
-
|
59 |
-
# Parse reasons from the dataset
|
60 |
-
try:
|
61 |
-
self.current_reasons = ast.literal_eval(self.current_sample['reason'])
|
62 |
-
except:
|
63 |
-
self.current_reasons = []
|
64 |
-
|
65 |
-
# Reset current guess
|
66 |
-
self.current_guess_index = None
|
67 |
-
|
68 |
-
def check_guess(self, guess_index):
|
69 |
-
"""
|
70 |
-
Check if the guess is correct
|
71 |
-
|
72 |
-
:param guess_index: Index of the guessed boundary
|
73 |
-
:return: Points earned
|
74 |
-
"""
|
75 |
-
self.current_guess_index = guess_index
|
76 |
-
|
77 |
-
# Calculate points based on closeness to true boundary
|
78 |
-
if guess_index == self.true_boundary_index:
|
79 |
-
return 5
|
80 |
-
elif guess_index > self.true_boundary_index:
|
81 |
-
return max(5 - (guess_index - self.true_boundary_index), 0)
|
82 |
-
else:
|
83 |
-
return 0
|
84 |
-
|
85 |
-
def validate_reason(self, user_reason):
|
86 |
-
"""
|
87 |
-
Validate user's reason against dataset reasons
|
88 |
-
|
89 |
-
:param user_reason: Reason provided by user
|
90 |
-
:return: Tuple of (is_valid, matching_reasons)
|
91 |
-
"""
|
92 |
-
# Convert user reason to lowercase for matching
|
93 |
-
user_reason_lower = user_reason.lower()
|
94 |
-
|
95 |
-
# Check against predefined reasons and current sample's reasons
|
96 |
-
matching_reasons = []
|
97 |
-
|
98 |
-
# Check predefined reasons
|
99 |
-
for reason in self.predefined_reasons:
|
100 |
-
if reason.lower() in user_reason_lower:
|
101 |
-
matching_reasons.append(reason)
|
102 |
-
|
103 |
-
# Check original sample's reasons
|
104 |
-
for orig_reason in self.current_reasons:
|
105 |
-
if orig_reason.lower() in user_reason_lower:
|
106 |
-
matching_reasons.append(orig_reason)
|
107 |
-
|
108 |
-
return len(matching_reasons) > 0, matching_reasons
|
109 |
-
|
110 |
-
def save_annotation(self, guess_index, reason, reason_validity):
|
111 |
-
"""
|
112 |
-
Save annotation to a text file
|
113 |
-
|
114 |
-
:param guess_index: Index of the guessed boundary
|
115 |
-
:param reason: Reason for the guess
|
116 |
-
:param reason_validity: Validity of the reason
|
117 |
-
"""
|
118 |
-
# Ensure logs directory exists
|
119 |
-
os.makedirs('logs', exist_ok=True)
|
120 |
-
|
121 |
-
# Generate unique filename with timestamp
|
122 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
123 |
-
filename = f'logs/annotation_{timestamp}.txt'
|
124 |
-
|
125 |
-
# Prepare annotation details
|
126 |
-
annotation_details = [
|
127 |
-
f"Timestamp: {timestamp}",
|
128 |
-
f"Model: {self.current_sample['model']}",
|
129 |
-
f"Dataset: {self.current_sample['dataset']}",
|
130 |
-
f"Guess Index: {guess_index + 1}",
|
131 |
-
f"True Boundary Index: {self.true_boundary_index + 1}",
|
132 |
-
f"Original Dataset Reasons: {self.current_reasons}",
|
133 |
-
f"User Reason: {reason}",
|
134 |
-
f"Reason Validity: {reason_validity[0]}",
|
135 |
-
f"Matching Reasons: {reason_validity[1]}",
|
136 |
-
"\nFull Text:\n" + "\n".join(f"{i+1}. {sent}" for i, sent in enumerate(self.current_sentences))
|
137 |
-
]
|
138 |
-
|
139 |
-
# Write to file
|
140 |
-
with open(filename, 'w') as f:
|
141 |
-
f.write("\n".join(annotation_details))
|
142 |
|
143 |
def main():
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
# Initialize game session state
|
147 |
if 'game' not in st.session_state:
|
@@ -150,82 +125,65 @@ def main():
|
|
150 |
st.session_state.total_points = 0
|
151 |
st.session_state.rounds_played = 0
|
152 |
|
153 |
-
# Game
|
154 |
-
st.
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
157 |
|
158 |
-
#
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
for i, sentence in enumerate(st.session_state.game.current_sentences):
|
161 |
-
st.
|
|
|
|
|
|
|
|
|
162 |
|
163 |
-
# Boundary selection
|
|
|
164 |
guess = st.radio(
|
165 |
"Where do you think the AI-generated text begins?",
|
166 |
options=[str(i+1) for i in range(len(st.session_state.game.current_sentences))]
|
167 |
)
|
168 |
|
169 |
-
# Reason input with predefined options
|
170 |
-
st.
|
171 |
reason_options = st.session_state.game.predefined_reasons
|
172 |
selected_predefined_reasons = st.multiselect(
|
173 |
-
"Select
|
174 |
options=reason_options
|
175 |
)
|
176 |
|
177 |
# Custom reason input
|
178 |
-
custom_reason = st.text_area("Additional
|
179 |
|
180 |
# Combine predefined and custom reasons
|
181 |
full_reason = " ".join(selected_predefined_reasons)
|
182 |
if custom_reason:
|
183 |
full_reason += f" {custom_reason}"
|
184 |
|
185 |
-
#
|
186 |
-
if st.button("Submit
|
187 |
if not full_reason.strip():
|
188 |
-
st.warning("Please provide a reason
|
189 |
else:
|
190 |
-
#
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
points_earned = st.session_state.game.check_guess(guess_index)
|
195 |
-
st.session_state.total_points += points_earned
|
196 |
-
st.session_state.rounds_played += 1
|
197 |
-
|
198 |
-
# Validate reason
|
199 |
-
reason_validity = st.session_state.game.validate_reason(full_reason)
|
200 |
-
|
201 |
-
# Save annotation
|
202 |
-
st.session_state.game.save_annotation(guess_index, full_reason, reason_validity)
|
203 |
-
|
204 |
-
# Show results
|
205 |
-
st.subheader("Results")
|
206 |
-
st.write(f"Your Guess: Sentence {guess}")
|
207 |
-
st.write(f"Actual Boundary: Sentence {st.session_state.game.true_boundary_index + 1}")
|
208 |
-
st.write(f"Points Earned: {points_earned}")
|
209 |
-
|
210 |
-
# Display reason validation
|
211 |
-
st.write("Reason Validation:")
|
212 |
-
st.write(f"Valid Reason: {reason_validity[0]}")
|
213 |
-
if reason_validity[1]:
|
214 |
-
st.write("Matching Reasons:", ", ".join(reason_validity[1]))
|
215 |
-
|
216 |
-
# Option to continue
|
217 |
-
if st.button("Next Round"):
|
218 |
-
st.session_state.game.load_random_sample()
|
219 |
-
st.experimental_rerun()
|
220 |
-
|
221 |
-
# Optional: Show metadata for current sample
|
222 |
-
if st.checkbox("Show Sample Metadata"):
|
223 |
-
st.write("Current Sample Details:")
|
224 |
-
sample = st.session_state.game.current_sample
|
225 |
-
st.write(f"Model: {sample['model']}")
|
226 |
-
st.write(f"Dataset: {sample['dataset']}")
|
227 |
-
st.write(f"Sampling Strategy (p): {sample['dec_strat_value']}")
|
228 |
-
st.write(f"Original Reasons: {st.session_state.game.current_reasons}")
|
229 |
|
230 |
if __name__ == "__main__":
|
231 |
main()
|
|
|
6 |
from datetime import datetime
|
7 |
import ast
|
8 |
|
9 |
+
# Custom CSS for enhanced styling
|
10 |
+
def local_css():
|
11 |
+
st.markdown("""
|
12 |
+
<style>
|
13 |
+
/* Global Styling */
|
14 |
+
body {
|
15 |
+
background-color: #f0f2f6;
|
16 |
+
color: #333;
|
17 |
+
}
|
18 |
+
|
19 |
+
/* Game Container */
|
20 |
+
.game-container {
|
21 |
+
background-color: white;
|
22 |
+
border-radius: 15px;
|
23 |
+
padding: 20px;
|
24 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
25 |
+
margin-bottom: 20px;
|
26 |
+
}
|
27 |
+
|
28 |
+
/* Sentences Styling */
|
29 |
+
.sentence-container {
|
30 |
+
background-color: #f8f9fa;
|
31 |
+
border-left: 4px solid #3498db;
|
32 |
+
padding: 10px;
|
33 |
+
margin-bottom: 10px;
|
34 |
+
transition: all 0.3s ease;
|
35 |
+
}
|
36 |
+
|
37 |
+
.sentence-container:hover {
|
38 |
+
background-color: #e9ecef;
|
39 |
+
transform: translateX(5px);
|
40 |
+
}
|
41 |
+
|
42 |
+
/* Buttons */
|
43 |
+
.stButton>button {
|
44 |
+
background-color: #3498db;
|
45 |
+
color: white;
|
46 |
+
border-radius: 20px;
|
47 |
+
border: none;
|
48 |
+
padding: 10px 20px;
|
49 |
+
transition: all 0.3s ease;
|
50 |
+
}
|
51 |
+
|
52 |
+
.stButton>button:hover {
|
53 |
+
background-color: #2980b9;
|
54 |
+
transform: scale(1.05);
|
55 |
+
}
|
56 |
+
|
57 |
+
/* Radio Buttons */
|
58 |
+
.stRadio>div {
|
59 |
+
display: flex;
|
60 |
+
flex-wrap: wrap;
|
61 |
+
gap: 10px;
|
62 |
+
}
|
63 |
+
|
64 |
+
.stRadio>div>label {
|
65 |
+
background-color: #ecf0f1;
|
66 |
+
padding: 10px;
|
67 |
+
border-radius: 10px;
|
68 |
+
transition: all 0.3s ease;
|
69 |
+
}
|
70 |
+
|
71 |
+
.stRadio>div>label:hover {
|
72 |
+
background-color: #3498db;
|
73 |
+
color: white;
|
74 |
+
}
|
75 |
+
|
76 |
+
/* Sidebar */
|
77 |
+
.css-1aumxhk {
|
78 |
+
background-color: #2c3e50;
|
79 |
+
color: white;
|
80 |
+
}
|
81 |
+
|
82 |
+
/* Reason Validation */
|
83 |
+
.reason-valid {
|
84 |
+
color: #2ecc71;
|
85 |
+
font-weight: bold;
|
86 |
+
}
|
87 |
+
|
88 |
+
.reason-invalid {
|
89 |
+
color: #e74c3c;
|
90 |
+
font-weight: bold;
|
91 |
+
}
|
92 |
+
</style>
|
93 |
+
""", unsafe_allow_html=True)
|
94 |
+
|
95 |
class RoFTGame:
|
96 |
+
# [Previous implementation remains the same]
|
97 |
+
# ... [Copy the entire RoFTGame class from the previous implementation] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
def main():
|
100 |
+
# Apply custom CSS
|
101 |
+
local_css()
|
102 |
+
|
103 |
+
# Fancy title with animation
|
104 |
+
st.markdown("""
|
105 |
+
<h1 style='text-align: center; color: #2c3e50;
|
106 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
107 |
+
animation: fadeIn 2s;'>
|
108 |
+
🕵️ Real or Fake Text Detective 🕵️♀️
|
109 |
+
</h1>
|
110 |
+
""", unsafe_allow_html=True)
|
111 |
+
|
112 |
+
# Game introduction
|
113 |
+
st.markdown("""
|
114 |
+
<div class='game-container'>
|
115 |
+
<p style='text-align: center; font-style: italic;'>
|
116 |
+
Sharpen your AI detection skills! Read carefully and identify where human writing transforms into machine-generated text.
|
117 |
+
</p>
|
118 |
+
</div>
|
119 |
+
""", unsafe_allow_html=True)
|
120 |
|
121 |
# Initialize game session state
|
122 |
if 'game' not in st.session_state:
|
|
|
125 |
st.session_state.total_points = 0
|
126 |
st.session_state.rounds_played = 0
|
127 |
|
128 |
+
# Game container
|
129 |
+
st.markdown("<div class='game-container'>", unsafe_allow_html=True)
|
130 |
+
|
131 |
+
# Game information in sidebar with icons
|
132 |
+
st.sidebar.markdown("## 🎮 Game Stats")
|
133 |
+
st.sidebar.markdown(f"### 🏆 Total Points: {st.session_state.total_points}")
|
134 |
+
st.sidebar.markdown(f"### 🎲 Rounds Played: {st.session_state.rounds_played}")
|
135 |
|
136 |
+
# Animated difficulty indicator
|
137 |
+
difficulty_map = {
|
138 |
+
'gpt2': '🟢 Easy',
|
139 |
+
'gpt2-xl': '🟠 Medium',
|
140 |
+
'ctrl': '🔴 Hard'
|
141 |
+
}
|
142 |
+
current_model = st.session_state.game.current_sample['model']
|
143 |
+
difficulty = difficulty_map.get(current_model, '⚪ Unknown')
|
144 |
+
st.sidebar.markdown(f"### 🎯 Difficulty: {difficulty}")
|
145 |
+
|
146 |
+
# Display sentences with enhanced styling
|
147 |
+
st.subheader("🔍 Examine the Text Carefully")
|
148 |
for i, sentence in enumerate(st.session_state.game.current_sentences):
|
149 |
+
st.markdown(f"""
|
150 |
+
<div class='sentence-container'>
|
151 |
+
<strong>{i+1}.</strong> {sentence}
|
152 |
+
</div>
|
153 |
+
""", unsafe_allow_html=True)
|
154 |
|
155 |
+
# Boundary selection with visual improvements
|
156 |
+
st.markdown("### 🚨 Detect AI Transition Point")
|
157 |
guess = st.radio(
|
158 |
"Where do you think the AI-generated text begins?",
|
159 |
options=[str(i+1) for i in range(len(st.session_state.game.current_sentences))]
|
160 |
)
|
161 |
|
162 |
+
# Reason input with predefined options and visual enhancements
|
163 |
+
st.markdown("### 🧐 Explain Your Reasoning")
|
164 |
reason_options = st.session_state.game.predefined_reasons
|
165 |
selected_predefined_reasons = st.multiselect(
|
166 |
+
"Select indicators of AI generation",
|
167 |
options=reason_options
|
168 |
)
|
169 |
|
170 |
# Custom reason input
|
171 |
+
custom_reason = st.text_area("Additional detective notes (optional)")
|
172 |
|
173 |
# Combine predefined and custom reasons
|
174 |
full_reason = " ".join(selected_predefined_reasons)
|
175 |
if custom_reason:
|
176 |
full_reason += f" {custom_reason}"
|
177 |
|
178 |
+
# Submit button with game metaphor
|
179 |
+
if st.button("🕵️ Submit Detective Report"):
|
180 |
if not full_reason.strip():
|
181 |
+
st.warning("🚨 Every good detective needs evidence! Please provide a reason.")
|
182 |
else:
|
183 |
+
# [Rest of the submission logic remains the same as in previous implementation]
|
184 |
+
# ... [Copy the entire submission block from previous implementation] ...
|
185 |
+
|
186 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
if __name__ == "__main__":
|
189 |
main()
|