tejasashinde commited on
Commit
0539b50
·
1 Parent(s): 5fae58d

Initial commit

Browse files
Files changed (3) hide show
  1. README.md +2 -2
  2. app.py +359 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Guess The Word
3
- emoji: 👁
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
@@ -10,4 +10,4 @@ pinned: false
10
  short_description: Guess words from their definitions across multiple domains
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Guess The Word
3
+ emoji: 🧐
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
 
10
  short_description: Guess words from their definitions across multiple domains
11
  ---
12
 
13
+ This is the demo for https://huggingface.co/datasets/tejasashinde/a2z-multidomain-glossary dataset.
app.py ADDED
@@ -0,0 +1,359 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import random
4
+ from datasets import load_dataset
5
+
6
+ # Load dataset
7
+ dataset = load_dataset("tejasashinde/a2z-multidomain-glossary", split="train")
8
+ df = pd.DataFrame(dataset)
9
+
10
+ # Prepare dataset
11
+ df = (
12
+ df.dropna(subset=["word", "description"])
13
+ .sample(frac=1)
14
+ .reset_index(drop=True)
15
+ )
16
+
17
+ # Game settings
18
+ GAME_LENGTHS = {
19
+ "Quick Game (10 Qs)": 10,
20
+ "Medium Game (20 Qs)": 20,
21
+ "Endless Mode": None,
22
+ }
23
+
24
+ # Session state
25
+ session_state = {
26
+ "score": 0,
27
+ "question_idx": 0,
28
+ "game_length": 10,
29
+ "used_indices": [],
30
+ "current_correct_word": "",
31
+ "current_options": [],
32
+ "mode": None,
33
+ }
34
+
35
+ def generate_question():
36
+ while True:
37
+ idx = random.randint(0, len(df) - 1)
38
+ if idx not in session_state["used_indices"]:
39
+ session_state["used_indices"].append(idx)
40
+ break
41
+ row = df.loc[idx]
42
+ correct = row["word"]
43
+ description = row["description"]
44
+ domain = row["domain"]
45
+ prev = df[(df["domain"] == domain) & (df["word"] != correct)]
46
+ distractors = (
47
+ prev["word"].sample(3).tolist()
48
+ if len(prev) >= 3
49
+ else df[df["word"] != correct]["word"].sample(3).tolist()
50
+ )
51
+ options = distractors + [correct]
52
+ random.shuffle(options)
53
+ session_state.update(current_correct_word=correct, current_options=options)
54
+ return description, options
55
+
56
+ def show_mode_selection():
57
+ return (
58
+ gr.update(visible=False), # start_btn
59
+ gr.update(visible=True), # mode_selector
60
+ gr.update(visible=True), # proceed_btn
61
+ gr.update(visible=False), # level_text
62
+ gr.update(value="", visible=False), # question_display
63
+ *(gr.update(visible=False),)*4, # buttons b0,b1,b2,b3
64
+ "", # feedback cleared
65
+ "", # summary cleared
66
+ gr.update(visible=False), # play_again_btn
67
+ gr.update(visible=False), # end_game_btn
68
+ )
69
+
70
+ def start_game(mode):
71
+ session_state.update(
72
+ score=0,
73
+ question_idx=0,
74
+ used_indices=[],
75
+ game_length=GAME_LENGTHS[mode],
76
+ mode=mode,
77
+ )
78
+ desc, options = generate_question()
79
+ return (
80
+ gr.update(visible=False), # mode_selector
81
+ gr.update(visible=False), # proceed_btn
82
+ gr.update(value=f"Question 1", visible=True), # level_text
83
+ gr.update(value=desc, visible=True), # question_display
84
+ *[gr.update(value=opt, visible=True) for opt in options], # buttons
85
+ "", # feedback cleared
86
+ "", # summary cleared
87
+ gr.update(visible=False), # play_again_btn
88
+ gr.update(visible=(mode == "Endless Mode")), # end_game_btn only visible in endless mode
89
+ )
90
+
91
+ def answer_question(choice):
92
+ session_state["question_idx"] += 1
93
+ correct = session_state["current_correct_word"]
94
+
95
+ if choice == correct:
96
+ session_state["score"] += 1
97
+ feedback = '<div class="alert alert-success">✅ Correct!</div>'
98
+ else:
99
+ feedback = f'<div class="alert alert-danger">❌ Wrong! Correct was <b>{correct}</b></div>'
100
+
101
+ # Check if game ended (for finite mode)
102
+ if session_state["game_length"] and session_state["question_idx"] >= session_state["game_length"]:
103
+ summary_html = f'''
104
+ <div class="game-over-box">
105
+ <h2>🏁 Game Over</h2>
106
+ <p>You scored <strong>{session_state["score"]}</strong> out of <strong>{session_state["game_length"]}</strong></p>
107
+ </div>
108
+ '''
109
+ return (
110
+ gr.update(value=feedback, visible=True), # Show last feedback
111
+ gr.update(value="", visible=False), # Hide question_display
112
+ *(gr.update(visible=False),)*4, # Hide buttons
113
+ gr.update(value=summary_html, visible=True), # Show summary
114
+ gr.update(visible=False), # Hide level_text
115
+ gr.update(visible=True), # Show play_again_btn
116
+ gr.update(visible=False), # Hide end_game_btn
117
+ )
118
+
119
+ # For ongoing game (including endless mode)
120
+ desc, options = generate_question()
121
+ return (
122
+ gr.update(value=feedback, visible=True), # Show feedback
123
+ gr.update(value=desc, visible=True),
124
+ *[gr.update(value=o, visible=True) for o in options],
125
+ gr.update(value="", visible=False), # Clear summary
126
+ f"Question {session_state['question_idx'] + 1}",
127
+ gr.update(visible=False), # Hide play_again_btn during game
128
+ gr.update(visible=(session_state["mode"] == "Endless Mode")), # Show end_game_btn only in endless mode
129
+ )
130
+
131
+ def end_game():
132
+ summary_html = f'''
133
+ <div class="game-over-box">
134
+ <h2>🏁 Game Ended</h2>
135
+ <p>You scored <strong>{session_state["score"]}</strong> points.</p>
136
+ </div>
137
+ '''
138
+ return (
139
+ gr.update(value="", visible=False), # Clear feedback
140
+ gr.update(value="", visible=False), # Hide question_display
141
+ *(gr.update(visible=False),)*4, # Hide buttons
142
+ gr.update(value=summary_html, visible=True),
143
+ gr.update(visible=False), # Hide level_text
144
+ gr.update(visible=True), # Show play_again_btn
145
+ gr.update(visible=False), # Hide end_game_btn
146
+ )
147
+
148
+ # Styling (unchanged)
149
+ custom_css = """
150
+ .alert { padding:10px; border-radius:4px; margin-top:10px; font-weight:bold; }
151
+ .alert-success { background: #d4edda; color: #155724; }
152
+ .alert-danger { background: #f8d7da; color: #721c24; }
153
+ .alert b { color: black; }
154
+
155
+ body {
156
+ background: radial-gradient(ellipse at center, #001f3f 0%, #000 100%);
157
+ color: white; font-family: 'Open Sans', sans-serif;
158
+ margin:0; padding:0;
159
+ height: 100vh;
160
+ display: flex;
161
+ justify-content: center;
162
+ align-items: center;
163
+ }
164
+
165
+ .tv-screen {
166
+ position: relative;
167
+ display: flex;
168
+ flex-direction: column;
169
+ background: #000;
170
+ border: 20px solid #444;
171
+ border-radius: 30px;
172
+ width: 650px;
173
+ height: 600px;
174
+ padding: 25px;
175
+ box-shadow: inset 0 0 30px #0ff, 0 0 20px #0ff;
176
+ overflow: hidden;
177
+ }
178
+
179
+ .gr-button.option {
180
+ margin: 10px 5px;
181
+ background:#003366;
182
+ color:white;
183
+ border-radius:8px;
184
+ min-width: 140px;
185
+ font-weight: bold;
186
+ font-size: 16px;
187
+ }
188
+ .gr-button.option:hover { background:#0052cc; }
189
+
190
+ .question-header {
191
+ font-size: 28px;
192
+ font-weight: bold;
193
+ background: rgba(0, 0, 0, 0.5);
194
+ padding: 20px;
195
+ text-shadow: 1px 1px 5px gold;
196
+ border: 2px solid #ffaa00;
197
+ border-radius: 10px;
198
+ text-align: center;
199
+ margin-bottom: 5px;
200
+ }
201
+
202
+ .level-tracker {
203
+ text-align: center;
204
+ color: gold;
205
+ font-weight: bold;
206
+ font-size: 18px;
207
+ margin-bottom: 8px;
208
+ }
209
+
210
+ .game-over-box {
211
+ top: 0; left: 0; right: 0; bottom: 0;
212
+ background-color: rgba(255, 255, 255, 0.95);
213
+ color: #000;
214
+ border-radius: 30px;
215
+ padding: 40px;
216
+ display: flex;
217
+ flex-direction: column;
218
+ justify-content: center;
219
+ align-items: center;
220
+ font-family: 'Open Sans', sans-serif;
221
+ font-size: 20px;
222
+ text-align: center;
223
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
224
+ z-index: 10;
225
+ }
226
+ .game-over-box h2 {
227
+ font-size: 28px;
228
+ margin-bottom: 10px;
229
+ color: black;
230
+ }
231
+ .game-over-box p,strong {
232
+ font-size: 18px;
233
+ margin-bottom: 20px;
234
+ color: black;
235
+ }
236
+ """
237
+
238
+ with gr.Blocks(css=custom_css, title="Word Quiz") as demo:
239
+ with gr.Column(elem_classes="tv-screen"):
240
+ gr.Markdown("## 🧐 Guess the Word!")
241
+ gr.HTML('<p style="color: white;">This space uses <a href="https://huggingface.co/datasets/tejasashinde/a2z-multidomain-glossary" target="_blank">tejasashinde/a2z-multidomain-glossary</a> dataset.</p>')
242
+
243
+ start_btn = gr.Button("🎮 Start Game")
244
+ mode_selector = gr.Radio(
245
+ choices=list(GAME_LENGTHS.keys()), label="Choose Mode", visible=False
246
+ )
247
+ proceed_btn = gr.Button("Proceed", visible=False)
248
+
249
+ level_text = gr.Markdown("Question 1", elem_classes="level-tracker", visible=False)
250
+ question_display = gr.Markdown("", elem_classes="question-header", visible=False)
251
+
252
+ with gr.Row():
253
+ b0 = gr.Button("", elem_classes="option", visible=False)
254
+ b1 = gr.Button("", elem_classes="option", visible=False)
255
+ with gr.Row():
256
+ b2 = gr.Button("", elem_classes="option", visible=False)
257
+ b3 = gr.Button("", elem_classes="option", visible=False)
258
+
259
+ feedback = gr.Markdown()
260
+ summary = gr.HTML(visible=False)
261
+ play_again_btn = gr.Button("🔁 Play Again", visible=False)
262
+ end_game_btn = gr.Button("⏹️ End Game", visible=False)
263
+
264
+ # Logic connections
265
+ start_btn.click(
266
+ fn=show_mode_selection,
267
+ inputs=None,
268
+ outputs=[
269
+ start_btn,
270
+ mode_selector,
271
+ proceed_btn,
272
+ level_text,
273
+ question_display,
274
+ b0,
275
+ b1,
276
+ b2,
277
+ b3,
278
+ feedback,
279
+ summary,
280
+ play_again_btn,
281
+ end_game_btn,
282
+ ],
283
+ )
284
+
285
+ proceed_btn.click(
286
+ fn=start_game,
287
+ inputs=mode_selector,
288
+ outputs=[
289
+ mode_selector,
290
+ proceed_btn,
291
+ level_text,
292
+ question_display,
293
+ b0,
294
+ b1,
295
+ b2,
296
+ b3,
297
+ feedback,
298
+ summary,
299
+ play_again_btn,
300
+ end_game_btn,
301
+ ],
302
+ )
303
+
304
+ for btn in [b0, b1, b2, b3]:
305
+ btn.click(
306
+ fn=answer_question,
307
+ inputs=btn,
308
+ outputs=[
309
+ feedback,
310
+ question_display,
311
+ b0,
312
+ b1,
313
+ b2,
314
+ b3,
315
+ summary,
316
+ level_text,
317
+ play_again_btn,
318
+ end_game_btn,
319
+ ],
320
+ )
321
+
322
+ play_again_btn.click(
323
+ fn=show_mode_selection,
324
+ inputs=None,
325
+ outputs=[
326
+ start_btn,
327
+ mode_selector,
328
+ proceed_btn,
329
+ level_text,
330
+ question_display,
331
+ b0,
332
+ b1,
333
+ b2,
334
+ b3,
335
+ feedback,
336
+ summary,
337
+ play_again_btn,
338
+ end_game_btn,
339
+ ],
340
+ )
341
+
342
+ end_game_btn.click(
343
+ fn=end_game,
344
+ inputs=None,
345
+ outputs=[
346
+ feedback,
347
+ question_display,
348
+ b0,
349
+ b1,
350
+ b2,
351
+ b3,
352
+ summary,
353
+ level_text,
354
+ play_again_btn,
355
+ end_game_btn,
356
+ ],
357
+ )
358
+
359
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ pandas
3
+ datasets