|  | import gradio as gr | 
					
						
						|  | import json | 
					
						
						|  | from Research.semantrix.game_transformer import Semantrix | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | config_file_path = "config/lang.json" | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | with open(config_file_path, "r") as file: | 
					
						
						|  | Config_full = json.load(file) | 
					
						
						|  |  | 
					
						
						|  | lang = 0 | 
					
						
						|  |  | 
					
						
						|  | if lang == 0: | 
					
						
						|  | Config = Config_full["SPA"]["Game"] | 
					
						
						|  | elif lang == 1: | 
					
						
						|  | Config = Config_full["ENG"]["Game"] | 
					
						
						|  | else: | 
					
						
						|  | Config = Config_full["SPA"]["Game"] | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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 += "## Mejores intentos\n" | 
					
						
						|  | markdown += "<table>\n" | 
					
						
						|  | markdown += "  <tr>\n" | 
					
						
						|  | markdown += "    <th>Ranking</th>\n" | 
					
						
						|  | markdown += "    <th>Palabra</th>\n" | 
					
						
						|  | markdown += "    <th>Puntuación</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"## Último Intento\n" | 
					
						
						|  | markdown += f"**{last_items[0]}:** {last_items[1]} - Score: {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) | 
					
						
						|  | hint_path = "config/hint.png" | 
					
						
						|  | game = Semantrix() | 
					
						
						|  |  | 
					
						
						|  | gr.Markdown( | 
					
						
						|  | """ | 
					
						
						|  | <p style="text-align:center"> SEMANTRIX: EL JUEGO DE LAS PALABRAS </p> | 
					
						
						|  | """ | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | def reset(difficulty): | 
					
						
						|  | game.prepare_game(difficulty) | 
					
						
						|  | output = [ | 
					
						
						|  | -1, | 
					
						
						|  | gr.Textbox(visible=False), | 
					
						
						|  | gr.Textbox(visible=False), | 
					
						
						|  | gr.Image(hint_path, visible=True, interactive=False), | 
					
						
						|  | gr.Button("Empezar", visible=True, variant="secondary"), | 
					
						
						|  | gr.Radio(visible=False), | 
					
						
						|  | gr.Textbox(visible=False), | 
					
						
						|  | gr.Button(visible=False), | 
					
						
						|  | ] | 
					
						
						|  |  | 
					
						
						|  | 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("Empezar", visible=True), | 
					
						
						|  | gr.Radio(label="", visible=False), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | Config[list(Config.keys())[state_int]], visible=False, label="" | 
					
						
						|  | ), | 
					
						
						|  | gr.Button("Rendirse", visible=False), | 
					
						
						|  | gr.Textbox(visible=False), | 
					
						
						|  | gr.Image(interactive=False, visible=True), | 
					
						
						|  | gr.Textbox(visible=False), | 
					
						
						|  | gr.Button(visible=False), | 
					
						
						|  | gr.Markdown(visible=False), | 
					
						
						|  | ] | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | if state_int == 1: | 
					
						
						|  | output.extend( | 
					
						
						|  | [ | 
					
						
						|  | gr.Button("Si", visible=False), | 
					
						
						|  | gr.Radio(["SÍ", "NO"], label="", visible=True), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | Config[list(Config.keys())[state_int]], visible=True, label="" | 
					
						
						|  | ), | 
					
						
						|  | gr.Button("Rendirse", 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 == "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("Rendirse", 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("Siguiente", visible=True), | 
					
						
						|  | gr.Radio(visible=False), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | Config[list(Config.keys())[state_int]], | 
					
						
						|  | visible=True, | 
					
						
						|  | label="", | 
					
						
						|  | ), | 
					
						
						|  | gr.Button("Rendirse", 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("Siguiente", visible=False), | 
					
						
						|  | gr.Radio(["Fácil", "Normal", "Difícil", "Experto"], visible=True), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | Config[list(Config.keys())[state_int]], visible=True, label="" | 
					
						
						|  | ), | 
					
						
						|  | gr.Button("Rendirse", 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 == "Fácil": | 
					
						
						|  | difficulty = 1 | 
					
						
						|  | elif radio == "Normal": | 
					
						
						|  | difficulty = 2 | 
					
						
						|  | elif radio == "Dificil": | 
					
						
						|  | difficulty = 3 | 
					
						
						|  | else: | 
					
						
						|  | difficulty = 4 | 
					
						
						|  |  | 
					
						
						|  | output.extend( | 
					
						
						|  | [ | 
					
						
						|  | gr.Button("Empezar", visible=True, variant="primary"), | 
					
						
						|  | gr.Radio(visible=False), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | Config[list(Config.keys())[state_int]], visible=True, label="" | 
					
						
						|  | ), | 
					
						
						|  | gr.Button("Rendirse", 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("Enviar", 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, placeholder="Nueva palabra"), | 
					
						
						|  | 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_md = convert_to_markdown_centered(feedback_trim[1]) | 
					
						
						|  | feedback = feedback_trim[0].split("[hint]") | 
					
						
						|  | win = feedback_trim[0].split("[win]") | 
					
						
						|  | if len(feedback) > 1: | 
					
						
						|  | hint = True | 
					
						
						|  | else: | 
					
						
						|  | hint = False | 
					
						
						|  |  | 
					
						
						|  | if len(win) > 1: | 
					
						
						|  | won = True | 
					
						
						|  | curiosity = game.curiosity() | 
					
						
						|  | output.extend( | 
					
						
						|  | [ | 
					
						
						|  | gr.Button("Enviar", visible=False, variant="primary"), | 
					
						
						|  | gr.Radio(label="", visible=False), | 
					
						
						|  | gr.Textbox(win[1], visible=True, label=""), | 
					
						
						|  | gr.Button(visible=False, variant="stop"), | 
					
						
						|  | gr.Textbox( | 
					
						
						|  | value="", visible=False, placeholder="Nueva palabra" | 
					
						
						|  | ), | 
					
						
						|  | gr.Image(hint_path, interactive=False, visible=True), | 
					
						
						|  | gr.Textbox(curiosity, visible=True, label="Curiosidad"), | 
					
						
						|  | gr.Button(visible=True), | 
					
						
						|  | gr.Markdown(visible=False), | 
					
						
						|  | ] | 
					
						
						|  | ) | 
					
						
						|  | return output | 
					
						
						|  |  | 
					
						
						|  | output.extend( | 
					
						
						|  | [ | 
					
						
						|  | gr.Button("Enviar", 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="Nueva palabra"), | 
					
						
						|  | gr.Image(hint_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=True), | 
					
						
						|  | ] | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | else: | 
					
						
						|  | output.extend( | 
					
						
						|  | [ | 
					
						
						|  | gr.Button("Siguiente", 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(hint_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("Empezar") | 
					
						
						|  | give_up = gr.Button("Pista", visible=False) | 
					
						
						|  | reload = gr.Button("Volver a jugar", visible=False) | 
					
						
						|  |  | 
					
						
						|  | 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], | 
					
						
						|  | ) | 
					
						
						|  | reload.click( | 
					
						
						|  | reset, | 
					
						
						|  | inputs=difficulty, | 
					
						
						|  | outputs=[state, out, inp, img, but, radio, hint_out, reload], | 
					
						
						|  | ) | 
					
						
						|  | radio.input(change, inputs=[state, inp], outputs=[state, inp]) | 
					
						
						|  | demo.load( | 
					
						
						|  | reset, | 
					
						
						|  | inputs=difficulty, | 
					
						
						|  | outputs=[state, out, inp, img, but, radio, hint_out, reload], | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | 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() | 
					
						
						|  |  |