sepp81 commited on
Commit
80a1520
·
verified ·
1 Parent(s): 7f14c2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -82
app.py CHANGED
@@ -1,94 +1,116 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  import random
4
 
5
- # Load the dataset (ensure the file path is correct)
6
- df = pd.read_csv('roft.csv') # Replace with the actual dataset path
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Initialize session state variables
9
- if 'score' not in st.session_state:
10
- st.session_state.score = 0
11
- if 'index' not in st.session_state:
12
- st.session_state.index = 0
13
- if 'game_over' not in st.session_state:
14
- st.session_state.game_over = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Function to get the current text pair from the dataset
17
- def get_next_text():
18
- # If game is over, return None to stop
19
- if st.session_state.game_over:
20
- return None
21
- # Fetch the next text in sequence
22
- text_data = df.iloc[st.session_state.index]
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Extract prompt and generation text
25
- prompt_text = text_data['prompt_body']
26
- gen_text = text_data['gen_body']
27
- model_used = text_data['model']
 
 
28
 
29
- # Get the continuation text (sentence before "_SEP_")
30
- continuation_text = gen_text.split("_SEP_")[0].strip()
31
-
32
- # Get the true boundary index (where human text ends and machine text begins)
33
- true_boundary_index = text_data['true_boundary_index']
34
 
35
- return prompt_text, continuation_text, gen_text, model_used, true_boundary_index
36
-
37
- # Function to update the game state
38
- def update_game_state(user_answer, correct_answer, user_reason=None):
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
- # If user chose machine, display their reason
51
- if user_answer == 'machine' and user_reason:
52
- st.write(f"Reason selected: {user_reason}")
53
-
54
- # Display the score
55
- st.sidebar.text(f"Score: {st.session_state.score}")
56
-
57
- # Show the current text to classify
58
- if not st.session_state.game_over:
59
- prompt_text, continuation_text, gen_text, model_used, true_boundary_index = get_next_text()
60
-
61
- if prompt_text:
62
- # Display the human-generated text (more readable format)
63
- st.header("Human-Generated Text")
64
- st.write(f"Model used: {model_used}")
65
- st.write("Text: ")
66
- st.markdown(f"**{prompt_text}**")
67
-
68
- # Display the continuation text (sentence before _SEP_)
69
- st.header("Continuation Text")
70
- st.write(f"{continuation_text}")
71
-
72
- # User input (radio buttons for classification)
73
- user_answer = st.radio("Classify the continuation as:", ["human", "machine"])
74
-
75
- # If user selects 'machine', ask for reasoning
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
- # Button to submit answer if 'human' is selected
88
- elif st.button('Submit'):
89
- correct_answer = 'human' if true_boundary_index == 0 else 'machine'
90
- update_game_state(user_answer, correct_answer)
 
 
 
91
 
92
- else:
93
- # If the game is over, display the final score
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()