Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,94 +1,116 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
3 |
import random
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
true_boundary_index = text_data['true_boundary_index']
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Check if the answer is correct
|
40 |
-
if user_answer == correct_answer:
|
41 |
-
st.session_state.score += 5
|
42 |
-
st.session_state.index += 1
|
43 |
-
if st.session_state.index >= len(df):
|
44 |
-
st.session_state.game_over = True
|
45 |
-
st.success("Congratulations! You've completed the quiz.")
|
46 |
-
else:
|
47 |
-
st.session_state.game_over = True
|
48 |
-
st.error("Game Over! Incorrect Answer.")
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
st.
|
64 |
-
st.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
st.
|
70 |
-
st.write(f"{
|
71 |
-
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
if user_answer == 'machine':
|
77 |
-
user_reason = st.selectbox("Why do you think it's machine-generated?", [
|
78 |
-
"grammar", "repetition", "irrelevant", "contradicts_sentence", "contradicts_knowledge",
|
79 |
-
"common_sense", "coreference", "generic"
|
80 |
-
])
|
81 |
-
|
82 |
-
# Button to submit answer
|
83 |
-
if st.button('Submit'):
|
84 |
-
correct_answer = 'machine' if true_boundary_index != 0 else 'human'
|
85 |
-
update_game_state(user_answer, correct_answer, user_reason)
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
st.write(f"Final Score: {st.session_state.score}")
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
import random
|
5 |
|
6 |
+
class RoFTGame:
|
7 |
+
def __init__(self, dataset_path):
|
8 |
+
"""
|
9 |
+
Initialize the RoFT Game with the dataset
|
10 |
+
|
11 |
+
:param dataset_path: Path to the roft.csv file
|
12 |
+
"""
|
13 |
+
self.df = pd.read_csv(dataset_path)
|
14 |
+
self.current_sample = None
|
15 |
+
self.current_sentences = None
|
16 |
+
self.true_boundary_index = None
|
17 |
+
self.current_guess_index = None
|
18 |
|
19 |
+
def load_random_sample(self):
|
20 |
+
"""
|
21 |
+
Load a random sample from the dataset
|
22 |
+
"""
|
23 |
+
# Filter for samples with valid generations
|
24 |
+
valid_samples = self.df[self.df['gen_body'].notna()]
|
25 |
+
|
26 |
+
# Select a random sample
|
27 |
+
self.current_sample = valid_samples.sample(n=1).iloc[0]
|
28 |
+
|
29 |
+
# Prepare sentences
|
30 |
+
prompt_sentences = self.current_sample['prompt_body'].split('_SEP_')
|
31 |
+
gen_sentences = self.current_sample['gen_body'].split('_SEP_')
|
32 |
+
|
33 |
+
# Combine and truncate to 10 sentences
|
34 |
+
combined_sentences = prompt_sentences + gen_sentences
|
35 |
+
self.current_sentences = combined_sentences[:10]
|
36 |
+
|
37 |
+
# Store true boundary
|
38 |
+
self.true_boundary_index = self.current_sample['true_boundary_index']
|
39 |
+
|
40 |
+
# Reset current guess
|
41 |
+
self.current_guess_index = None
|
42 |
|
43 |
+
def check_guess(self, guess_index):
|
44 |
+
"""
|
45 |
+
Check if the guess is correct
|
46 |
+
|
47 |
+
:param guess_index: Index of the guessed boundary
|
48 |
+
:return: Points earned
|
49 |
+
"""
|
50 |
+
self.current_guess_index = guess_index
|
51 |
+
|
52 |
+
# Calculate points based on closeness to true boundary
|
53 |
+
if guess_index == self.true_boundary_index:
|
54 |
+
return 5
|
55 |
+
elif guess_index > self.true_boundary_index:
|
56 |
+
return max(5 - (guess_index - self.true_boundary_index), 0)
|
57 |
+
else:
|
58 |
+
return 0
|
59 |
+
|
60 |
+
def main():
|
61 |
+
st.title("Real or Fake Text (RoFT) Game")
|
62 |
|
63 |
+
# Initialize game session state
|
64 |
+
if 'game' not in st.session_state:
|
65 |
+
st.session_state.game = RoFTGame('roft.csv')
|
66 |
+
st.session_state.game.load_random_sample()
|
67 |
+
st.session_state.total_points = 0
|
68 |
+
st.session_state.rounds_played = 0
|
69 |
|
70 |
+
# Game information
|
71 |
+
st.sidebar.header("Game Info")
|
72 |
+
st.sidebar.write(f"Total Points: {st.session_state.total_points}")
|
73 |
+
st.sidebar.write(f"Rounds Played: {st.session_state.rounds_played}")
|
|
|
74 |
|
75 |
+
# Display sentences
|
76 |
+
st.subheader("Read the following text carefully:")
|
77 |
+
for i, sentence in enumerate(st.session_state.game.current_sentences):
|
78 |
+
st.write(f"{i+1}. {sentence}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Boundary selection
|
81 |
+
guess = st.radio(
|
82 |
+
"Where do you think the AI-generated text begins?",
|
83 |
+
options=[f"Sentence {i+1}" for i in range(len(st.session_state.game.current_sentences))]
|
84 |
+
)
|
85 |
+
|
86 |
+
# Guess submission
|
87 |
+
if st.button("Submit Guess"):
|
88 |
+
# Convert guess to index
|
89 |
+
guess_index = int(guess.split()[-1]) - 1
|
90 |
+
|
91 |
+
# Check guess and update points
|
92 |
+
points_earned = st.session_state.game.check_guess(guess_index)
|
93 |
+
st.session_state.total_points += points_earned
|
94 |
+
st.session_state.rounds_played += 1
|
95 |
+
|
96 |
+
# Show results
|
97 |
+
st.subheader("Results")
|
98 |
+
st.write(f"Your Guess: {guess}")
|
99 |
+
st.write(f"Actual Boundary: Sentence {st.session_state.game.true_boundary_index + 1}")
|
100 |
+
st.write(f"Points Earned: {points_earned}")
|
101 |
+
|
102 |
+
# Option to continue
|
103 |
+
if st.button("Next Round"):
|
104 |
+
st.session_state.game.load_random_sample()
|
105 |
+
st.experimental_rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# Optional: Show metadata for current sample
|
108 |
+
if st.checkbox("Show Sample Metadata"):
|
109 |
+
st.write("Current Sample Details:")
|
110 |
+
sample = st.session_state.game.current_sample
|
111 |
+
st.write(f"Model: {sample['model']}")
|
112 |
+
st.write(f"Dataset: {sample['dataset']}")
|
113 |
+
st.write(f"Sampling Strategy (p): {sample['dec_strat_value']}")
|
114 |
|
115 |
+
if __name__ == "__main__":
|
116 |
+
main()
|
|