File size: 14,299 Bytes
21e5561
85b22ea
5320625
2d20748
21e5561
 
d8a8064
e2b757a
85b22ea
 
 
 
5320625
85b22ea
5320625
 
85b22ea
5320625
85b22ea
 
5320625
 
85b22ea
 
5320625
 
85b22ea
 
 
 
 
 
 
5320625
85b22ea
 
5320625
 
 
85b22ea
 
 
 
 
 
 
 
 
 
 
 
 
5320625
 
 
 
 
 
85b22ea
 
 
 
 
 
5320625
e7e6aa8
 
85b22ea
 
5320625
 
85b22ea
5320625
e7e6aa8
5320625
e7e6aa8
5320625
 
 
e7e6aa8
 
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
85b22ea
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85b22ea
 
 
5320625
e7e6aa8
 
5320625
85b22ea
5320625
85b22ea
 
e7e6aa8
5320625
85b22ea
 
 
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
5320625
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
85b22ea
5320625
e7e6aa8
85b22ea
e7e6aa8
f9a3810
5320625
 
 
 
e7e6aa8
 
85b22ea
 
 
5320625
 
e7e6aa8
 
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
85b22ea
 
 
 
5320625
e7e6aa8
85b22ea
5320625
e7e6aa8
5320625
e7e6aa8
5320625
e7e6aa8
 
 
85b22ea
e7e6aa8
5320625
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
5320625
85b22ea
e7e6aa8
85b22ea
e7e6aa8
 
5320625
 
 
 
 
2d20748
 
 
 
 
 
5320625
 
 
 
 
e7e6aa8
85b22ea
 
 
 
2d20748
85b22ea
2d20748
 
 
 
 
85b22ea
 
5320625
85b22ea
 
 
 
5320625
 
 
85b22ea
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85b22ea
 
 
5320625
 
 
 
 
 
 
 
 
 
 
2d20748
5320625
 
85b22ea
e7e6aa8
 
5320625
 
 
 
 
 
 
 
 
 
 
 
 
e7e6aa8
 
5320625
 
85b22ea
 
 
5320625
 
f9a3810
e7e6aa8
5320625
 
85b22ea
5320625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2b757a
 
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import gradio as gr
import json
from game_word2vec import Semantrix as semantrix_word2vec
from game_transformer import Semantrix as semantrix_sentence_transformers


config_file_path = "config/lang.json"


with open(config_file_path, "r") as file:
    Config_full = json.load(file)

lang_conf = 0


if lang_conf == 1:
    Config = Config_full["ENG"]["Game"]
    Menu = Config_full["ENG"]["Menu"]
else:
    Config = Config_full["SPA"]["Game"]
    Menu = Config_full["SPA"]["Menu"]


def convert_to_markdown_centered(text):
    lines = text.strip().split("\n")

    if not lines:
        return ""

    last_attempt = lines[0]
    history_attempts = lines[2:12]
    markdown = '<div align="center">\n\n'

    markdown += "## " + Menu["Best_tries"] + "\n"
    markdown += "<table>\n"
    markdown += "  <tr>\n"
    markdown += "    <th>" + Menu["N_try"] + "</th>\n"
    markdown += "    <th>" + Menu["Word"] + "</th>\n"
    markdown += "    <th>" + Menu["Score"] + "</th>\n"
    markdown += "  </tr>\n"

    for line in history_attempts:
        items = eval(line.strip())
        markdown += "  <tr>\n"
        markdown += f"    <td><strong>{items[0]}</strong></td>\n"
        markdown += f"    <td>{items[1]}</td>\n"
        markdown += f"    <td>{items[2]}</td>\n"
        markdown += "  </tr>\n"

    markdown += "</table>\n\n"

    last_items = eval(last_attempt)
    markdown += f"## " + Menu["Last_try"] + "\n"
    markdown += (
        f"**{last_items[0]}:** {last_items[1]} - "
        + Menu["Score"]
        + f": {last_items[2]}\n\n"
    )
    markdown += "---\n\n"

    markdown += "</div>"

    return markdown


with gr.Blocks() as demo:
    state = gr.State(-1)
    difficulty = gr.State(-1)
    hint = gr.State(False)
    logo_path = "config/logo.png"
    logo_win_path = "config/logo_win.gif"

    game = semantrix_sentence_transformers()

    header = gr.Markdown(
        """
    <p style="text-align:center"> """
        + Menu["Header"]
        + """ </p>
    """
    )

    def reset(difficulty, lang, model):
        global Config, game, Menu
        language = 1 if lang == "English" else 0

        if model == "Word2Vec":
            game = semantrix_word2vec(language)
        else:
            game = semantrix_sentence_transformers(language)

        if language == 1:
            Config = Config_full["ENG"]["Game"]
            Menu = Config_full["ENG"]["Menu"]
        else:
            Config = Config_full["SPA"]["Game"]
            Menu = Config_full["SPA"]["Menu"]

        game.prepare_game(difficulty)
        output = [
            -1,
            gr.Textbox(visible=False),
            gr.Textbox(visible=False),
            gr.Image(logo_path, visible=True, interactive=False),
            gr.Button(Menu["Start"], visible=True, variant="secondary"),
            gr.Radio(visible=False),
            gr.Textbox(visible=False),
            gr.Button(visible=False),
            gr.Markdown(
                """
                    <p style="text-align:center"> """
                + Menu["Header"]
                + """ </p>
                    """
            ),
        ]

        return output

    def change(state, inp):

        state = state + 1
        return [state, inp]

    def update(state, radio, inp, hint):
        global difficulty
        dif_state = 4
        output = [state]

        state_int = state

        if state_int == -1:
            output.extend(
                [
                    gr.Button(Menu["Start"], visible=True),
                    gr.Radio(label="", visible=False),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=False, label=""
                    ),
                    gr.Button(Menu["Give_up"], visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=True),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )

        elif state_int == 1:
            output.extend(
                [
                    gr.Button(visible=False),
                    gr.Radio([Menu["Yes"], Menu["No"]], label="", visible=True),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=True, label=""
                    ),
                    gr.Button(Menu["Give_up"], visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )
        elif state_int == 2:
            if radio == Menu["No"]:
                output = [
                    dif_state,
                    gr.Button("Introducir", visible=True),
                    gr.Radio(visible=False),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=True, label=""
                    ),
                    gr.Button(Menu["Give_up"], visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]

            else:
                output.extend(
                    [
                        gr.Button(Menu["Next"], visible=True),
                        gr.Radio(visible=False),
                        gr.Textbox(
                            Config[list(Config.keys())[state_int]],
                            visible=True,
                            label="",
                        ),
                        gr.Button(Menu["Give_up"], visible=False),
                        gr.Textbox(visible=False),
                        gr.Image(interactive=False, visible=False),
                        gr.Textbox(visible=False),
                        gr.Button(visible=False),
                        gr.Markdown(visible=False),
                    ]
                )
        elif state_int == dif_state:
            output.extend(
                [
                    gr.Button(Menu["Next"], visible=False),
                    gr.Radio(
                        [Menu["Easy"], Menu["Normal"], Menu["Hard"], Menu["Expert"]],
                        visible=True,
                    ),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=True, label=""
                    ),
                    gr.Button(Menu["Give_up"], visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )
        elif state_int == dif_state + 1:
            if radio == Menu["Easy"]:
                difficulty = 1
            elif radio == Menu["Normal"]:
                difficulty = 2
            elif radio == Menu["Hard"]:
                difficulty = 3
            else:
                difficulty = 4

            output.extend(
                [
                    gr.Button(Menu["Start"], visible=True, variant="primary"),
                    gr.Radio(visible=False),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=True, label=""
                    ),
                    gr.Button(Menu["Give_up"], visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )

        elif state_int == dif_state + 2:

            game.prepare_game(difficulty)

            output.extend(
                [
                    gr.Button(Menu["Send"], visible=True, variant="primary"),
                    gr.Radio(label="", visible=False),
                    gr.Textbox(visible=False, label=""),
                    gr.Button(visible=True, variant="stop"),
                    gr.Textbox(
                        value="",
                        visible=True,
                        autofocus=True,
                        placeholder=Menu["New_word"],
                    ),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )
        elif state_int > dif_state + 2:
            feed = game.play_game(inp)
            feedback_trim = feed.split("[rank]")
            if len(feedback_trim) > 1:
                ranking_vis = True
                ranking_md = convert_to_markdown_centered(feedback_trim[1])

            else:
                ranking_vis = False
                ranking_md = ""

            feedback = feedback_trim[0].split("[hint]")
            win = feedback_trim[0].split("[win]")
            lose = feedback_trim[0].split("[lose]")
            if len(feedback) > 1:
                hint = True
            else:
                hint = False

            if len(win) > 1 or len(lose) > 1:
                won = True if len(win) > 1 else False
                curiosity = game.curiosity()
                output.extend(
                    [
                        gr.Button(Menu["Send"], visible=False, variant="primary"),
                        gr.Radio(label="", visible=False),
                        gr.Textbox(win[1] if won else lose[1], visible=True, label=""),
                        gr.Button(visible=False, variant="stop"),
                        gr.Textbox(
                            value="", visible=False, placeholder=Menu["New_word"]
                        ),
                        gr.Image(
                            logo_win_path if won else logo_path,
                            interactive=False,
                            visible=True,
                        ),
                        gr.Textbox(curiosity, visible=True, label=Menu["Curiosity"]),
                        gr.Button(visible=True),
                        gr.Markdown(visible=False),
                    ]
                )
                return output

            output.extend(
                [
                    gr.Button(Menu["Send"], visible=True, variant="primary"),
                    gr.Radio(label="", visible=False),
                    gr.Textbox(feedback[0], visible=True, label=""),
                    gr.Button(visible=True, variant="stop"),
                    gr.Textbox(value="", visible=True, placeholder=Menu["New_word"]),
                    gr.Image(logo_path, interactive=False, visible=False),
                    gr.Textbox(
                        feedback[1] if hint else "", visible=hint, label="Pista"
                    ),
                    gr.Button(visible=False),
                    gr.Markdown(ranking_md, visible=ranking_vis),
                ]
            )

        else:
            output.extend(
                [
                    gr.Button(Menu["Next"], visible=True),
                    gr.Radio(label="", visible=False),
                    gr.Textbox(
                        Config[list(Config.keys())[state_int]], visible=True, label=""
                    ),
                    gr.Button("Pista", visible=False),
                    gr.Textbox(visible=False),
                    gr.Image(interactive=False, visible=False),
                    gr.Textbox(visible=False),
                    gr.Button(visible=False),
                    gr.Markdown(visible=False),
                ]
            )
        return output

    img = gr.Image(logo_path, height=430, interactive=False, visible=True)
    ranking = gr.Markdown(visible=False)

    with gr.Row():
        out = gr.Textbox(visible=False, placeholder=Config[list(Config.keys())[0]])
        hint_out = gr.Textbox(visible=False)
    radio = gr.Radio(visible=False)
    with gr.Row():
        inp = gr.Textbox(visible=False, interactive=True, label="")
        but = gr.Button(Menu["Start"])
        give_up = gr.Button("Pista", visible=False)
        reload = gr.Button(Menu["Play_again"], visible=False)

    with gr.Accordion("Configuración/Settings", open=False):
        with gr.Row():
            model = (
                gr.Dropdown(
                    ["Sentence Transformers", "Word2Vec"],
                    value="Sentence Transformers",
                    label="Embedding Model",
                ),
            )
            lang = (
                gr.Dropdown(
                    ["Español", "English"], value="Español", label="Idioma/Language"
                ),
            )

    inp.submit(change, inputs=[state, inp], outputs=[state, inp])
    but.click(change, inputs=[state, inp], outputs=[state, inp])
    give_up.click(
        change,
        inputs=[
            state,
            gr.Textbox("give_up", visible=False, interactive=True, label=""),
        ],
        outputs=[state, inp],
    )
    radio.input(change, inputs=[state, inp], outputs=[state, inp])

    reload.click(
        reset,
        inputs=[difficulty, lang[0], model[0]],
        outputs=[state, out, inp, img, but, radio, hint_out, reload, header],
    )
    demo.load(
        reset,
        inputs=[difficulty, lang[0], model[0]],
        outputs=[state, out, inp, img, but, radio, hint_out, reload, header],
    )
    lang[0].select(
        reset,
        inputs=[difficulty, lang[0], model[0]],
        outputs=[state, out, inp, img, but, radio, hint_out, reload, header],
    )
    model[0].select(
        reset,
        inputs=[difficulty, lang[0], model[0]],
        outputs=[state, out, inp, img, but, radio, hint_out, reload, header],
    )

    state.change(
        update,
        inputs=[state, radio, inp, hint],
        outputs=[state, but, radio, out, give_up, inp, img, hint_out, reload, ranking],
    )

if __name__ == "__main__":
    demo.launch()