SuchithTissera commited on
Commit
c662032
·
verified ·
1 Parent(s): 9ac327d

Update pages/4_Mini_Game.py

Browse files
Files changed (1) hide show
  1. pages/4_Mini_Game.py +163 -4
pages/4_Mini_Game.py CHANGED
@@ -3,14 +3,15 @@
3
  import streamlit as st
4
  import pandas as pd
5
  from transformers import pipeline
6
- #for the pre-processing of uploaded data
7
- import string
8
  from utils.styles import load_css
9
  load_css()
10
  from datasets import load_dataset
11
 
 
12
  dataset = load_dataset("SuchithTissera/News_dataset")
13
- st.write(dataset["train"])
 
 
14
 
15
  #writing the content for the page
16
  st.markdown('<div class="main_title">🧩 Classification Mini-Game </div>',unsafe_allow_html=True)
@@ -21,4 +22,162 @@ st.markdown("""
21
  <ul>
22
  <li> Users may play the game with 5 or 10 rounds to test their skills </li>
23
  </ul>
24
- </div>""",unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import streamlit as st
4
  import pandas as pd
5
  from transformers import pipeline
 
 
6
  from utils.styles import load_css
7
  load_css()
8
  from datasets import load_dataset
9
 
10
+ #loading the training dataset
11
  dataset = load_dataset("SuchithTissera/News_dataset")
12
+ news_dataset = dataset["train"]
13
+
14
+ st.set_page_config(page_title="Mini Game", layout="wide")
15
 
16
  #writing the content for the page
17
  st.markdown('<div class="main_title">🧩 Classification Mini-Game </div>',unsafe_allow_html=True)
 
22
  <ul>
23
  <li> Users may play the game with 5 or 10 rounds to test their skills </li>
24
  </ul>
25
+ </div>""",unsafe_allow_html=True)
26
+
27
+ #coding the mini-game
28
+ CATEGORIES = [
29
+ "Business",
30
+ "Opinions",
31
+ "Political_gossip",
32
+ "Sports",
33
+ "Worlds_news"
34
+ ]
35
+
36
+ # ---------------------------
37
+ # Session state setup
38
+ # ---------------------------
39
+ if "game_started" not in st.session_state:
40
+ st.session_state.game_started = False
41
+
42
+ if "questions" not in st.session_state:
43
+ st.session_state.questions = None
44
+
45
+ if "current_round" not in st.session_state:
46
+ st.session_state.current_round = 0
47
+
48
+ if "score" not in st.session_state:
49
+ st.session_state.score = 0
50
+
51
+ if "total_rounds" not in st.session_state:
52
+ st.session_state.total_rounds = 5
53
+
54
+ if "answered" not in st.session_state:
55
+ st.session_state.answered = False
56
+
57
+ if "selected_option" not in st.session_state:
58
+ st.session_state.selected_option = None
59
+
60
+ if "game_finished" not in st.session_state:
61
+ st.session_state.game_finished = False
62
+
63
+
64
+ # ---------------------------
65
+ # Functions
66
+ # ---------------------------
67
+ def start_game(round_count):
68
+ sampled = news_dataset.sample(n=round_count).reset_index(drop=True)
69
+
70
+ st.session_state.questions = sampled
71
+ st.session_state.total_rounds = round_count
72
+ st.session_state.current_round = 0
73
+ st.session_state.score = 0
74
+ st.session_state.answered = False
75
+ st.session_state.selected_option = None
76
+ st.session_state.game_started = True
77
+ st.session_state.game_finished = False
78
+
79
+
80
+ def next_question():
81
+ st.session_state.current_round += 1
82
+ st.session_state.answered = False
83
+ st.session_state.selected_option = None
84
+
85
+ if st.session_state.current_round >= st.session_state.total_rounds:
86
+ st.session_state.game_finished = True
87
+
88
+
89
+ def restart_game():
90
+ st.session_state.game_started = False
91
+ st.session_state.questions = None
92
+ st.session_state.current_round = 0
93
+ st.session_state.score = 0
94
+ st.session_state.total_rounds = 5
95
+ st.session_state.answered = False
96
+ st.session_state.selected_option = None
97
+ st.session_state.game_finished = False
98
+
99
+
100
+ # ---------------------------
101
+ # UI
102
+ # ---------------------------
103
+ st.title("🎮 Classification Mini Game")
104
+
105
+ if not st.session_state.game_started:
106
+ st.subheader("Choose game length")
107
+ round_choice = st.radio(
108
+ "Select number of rounds:",
109
+ [5, 10],
110
+ horizontal=True
111
+ )
112
+
113
+ if st.button("Start Game"):
114
+ start_game(round_choice)
115
+ st.rerun()
116
+
117
+ # ---------------------------
118
+ # Game in progress
119
+ # ---------------------------
120
+ elif st.session_state.game_started and not st.session_state.game_finished:
121
+ q_index = st.session_state.current_round
122
+ row = st.session_state.questions.iloc[q_index]
123
+
124
+ st.markdown(f"### Round {q_index + 1} of {st.session_state.total_rounds}")
125
+ st.write("**Content:**")
126
+ st.info(row["content"])
127
+
128
+ selected = st.radio(
129
+ "Choose the correct category:",
130
+ CATEGORIES,
131
+ key=f"question_{q_index}"
132
+ )
133
+
134
+ if not st.session_state.answered:
135
+ if st.button("Submit Answer"):
136
+ st.session_state.selected_option = selected
137
+ st.session_state.answered = True
138
+
139
+ if selected == row["class"]:
140
+ st.session_state.score += 1
141
+
142
+ st.rerun()
143
+
144
+ else:
145
+ correct_answer = row["class"]
146
+
147
+ if st.session_state.selected_option == correct_answer:
148
+ st.success(f"Correct! The answer is {correct_answer}.")
149
+ else:
150
+ st.error(
151
+ f"Wrong. You selected {st.session_state.selected_option}, "
152
+ f"but the correct answer is {correct_answer}."
153
+ )
154
+
155
+ if st.button("Next Round"):
156
+ next_question()
157
+ st.rerun()
158
+
159
+ # ---------------------------
160
+ # Game finished
161
+ # ---------------------------
162
+ if st.session_state.game_finished:
163
+ score = st.session_state.score
164
+ total = st.session_state.total_rounds
165
+ percentage = (score / total) * 100
166
+
167
+ st.markdown("## Game Over")
168
+ st.write(f"Your score: **{score}/{total}**")
169
+
170
+ if percentage >= 75:
171
+ st.markdown(
172
+ f"<h3 style='color: green;'>Percentage Score: {percentage:.2f}%</h3>",
173
+ unsafe_allow_html=True
174
+ )
175
+ else:
176
+ st.markdown(
177
+ f"<h3 style='color: red;'>Percentage Score: {percentage:.2f}%</h3>",
178
+ unsafe_allow_html=True
179
+ )
180
+
181
+ if st.button("Play Again"):
182
+ restart_game()
183
+ st.rerun()