Javierss commited on
Commit ·
f60623c
1
Parent(s): 158bf9c
Add word check and change ln to log10
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/game.cpython-311.pyc +0 -0
- app.py +59 -59
- data/ranking.txt +1 -10
- game.py +80 -85
- hints.py +30 -30
__pycache__/app.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
|
__pycache__/game.cpython-311.pyc
CHANGED
|
Binary files a/__pycache__/game.cpython-311.pyc and b/__pycache__/game.cpython-311.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,61 +1,61 @@
|
|
| 1 |
-
"""
|
| 2 |
-
This module defines a Gradio-based web application for the Semantrix game. The application allows users to play the game in either Spanish or English, using different embedding models for word similarity.
|
| 3 |
-
|
| 4 |
-
Modules:
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
File Paths:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Functions:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
Gradio Components:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
Events:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
Main:
|
| 57 |
-
|
| 58 |
-
"""
|
| 59 |
|
| 60 |
import gradio as gr
|
| 61 |
import json
|
|
@@ -411,7 +411,7 @@ with gr.Blocks() as demo:
|
|
| 411 |
visible=True,
|
| 412 |
),
|
| 413 |
gr.Textbox(curiosity, visible=True, label=Menu["Curiosity"]),
|
| 414 |
-
gr.Button(visible=True),
|
| 415 |
gr.Markdown(visible=False),
|
| 416 |
]
|
| 417 |
)
|
|
|
|
| 1 |
+
# """
|
| 2 |
+
# This module defines a Gradio-based web application for the Semantrix game. The application allows users to play the game in either Spanish or English, using different embedding models for word similarity.
|
| 3 |
+
|
| 4 |
+
# Modules:
|
| 5 |
+
# gradio: Used for creating the web interface.
|
| 6 |
+
# json: Used for loading configuration files.
|
| 7 |
+
# game: Contains the Semantrix class for game logic.
|
| 8 |
+
|
| 9 |
+
# File Paths:
|
| 10 |
+
# config_file_path: Path to the configuration file.
|
| 11 |
+
# logo_path: Path to the logo image.
|
| 12 |
+
# logo_win_path: Path to the winning logo image.
|
| 13 |
+
|
| 14 |
+
# Functions:
|
| 15 |
+
# convert_to_markdown_centered(text):
|
| 16 |
+
# Converts text to a centered markdown format for displaying game history and last attempt.
|
| 17 |
+
|
| 18 |
+
# reset(difficulty, lang, model):
|
| 19 |
+
# Resets the game state based on the selected difficulty, language, and model.
|
| 20 |
+
|
| 21 |
+
# change(state, inp):
|
| 22 |
+
# Changes the game state by incrementing the state variable.
|
| 23 |
+
|
| 24 |
+
# update(state, radio, inp, hint):
|
| 25 |
+
# Updates the game state and UI components based on the current state and user inputs.
|
| 26 |
+
|
| 27 |
+
# Gradio Components:
|
| 28 |
+
# demo: The main Gradio Blocks component that contains the entire UI layout.
|
| 29 |
+
# header: A Markdown component for displaying the game header.
|
| 30 |
+
# state: A State component for tracking the current game state.
|
| 31 |
+
# difficulty: A State component for tracking the difficulty level.
|
| 32 |
+
# hint: A State component for tracking if a hint is provided.
|
| 33 |
+
# img: An Image component for displaying the game logo.
|
| 34 |
+
# ranking: A Markdown component for displaying the ranking.
|
| 35 |
+
# out: A Textbox component for displaying game messages.
|
| 36 |
+
# hint_out: A Textbox component for displaying hints.
|
| 37 |
+
# radio: A Radio component for user selections.
|
| 38 |
+
# inp: A Textbox component for user input.
|
| 39 |
+
# but: A Button component for several actions.
|
| 40 |
+
# give_up: A Button component for giving up.
|
| 41 |
+
# reload: A Button component for reloading the game.
|
| 42 |
+
# model: A Dropdown component for selecting the embedding model.
|
| 43 |
+
# lang: A Dropdown component for selecting the language.
|
| 44 |
+
|
| 45 |
+
# Events:
|
| 46 |
+
# inp.submit: Triggers the change function on input submission.
|
| 47 |
+
# but.click: Triggers the change function on button click.
|
| 48 |
+
# give_up.click: Triggers the change function on give up button click.
|
| 49 |
+
# radio.input: Triggers the change function on radio input.
|
| 50 |
+
# reload.click: Triggers the reset function on reload button click.
|
| 51 |
+
# demo.load: Triggers the reset function on demo load.
|
| 52 |
+
# lang[0].select: Triggers the reset function on language selection.
|
| 53 |
+
# model[0].select: Triggers the reset function on model selection.
|
| 54 |
+
# state.change: Triggers the update function on state change.
|
| 55 |
+
|
| 56 |
+
# Main:
|
| 57 |
+
# Launches the Gradio application if the script is run as the main module.
|
| 58 |
+
# """
|
| 59 |
|
| 60 |
import gradio as gr
|
| 61 |
import json
|
|
|
|
| 411 |
visible=True,
|
| 412 |
),
|
| 413 |
gr.Textbox(curiosity, visible=True, label=Menu["Curiosity"]),
|
| 414 |
+
gr.Button(Menu["Play_again"], visible=True),
|
| 415 |
gr.Markdown(visible=False),
|
| 416 |
]
|
| 417 |
)
|
data/ranking.txt
CHANGED
|
@@ -1,10 +1 @@
|
|
| 1 |
-
|
| 2 |
-
---------------------------
|
| 3 |
-
['#1', 'amigo', 8.3]
|
| 4 |
-
['#2', 'loco', 7.64]
|
| 5 |
-
['#7', 'fantasma', 7.41]
|
| 6 |
-
['#8', 'medium', 6.94]
|
| 7 |
-
['#4', 'hermano', 6.81]
|
| 8 |
-
['#5', 'duelo', 5.76]
|
| 9 |
-
['#3', 'familia', 4.88]
|
| 10 |
-
['#6', 'divorcio', 1.67]
|
|
|
|
| 1 |
+
---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game.py
CHANGED
|
@@ -1,40 +1,40 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Semantrix Game Module
|
| 3 |
-
|
| 4 |
-
This module defines the Semantrix class, which implements a word guessing game using word embeddings. The game can be configured to use either a Word2Vec model or a SentenceTransformer model for word embeddings. The game supports multiple languages and difficulty levels.
|
| 5 |
-
|
| 6 |
-
Classes:
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
Functions:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
Attributes:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"""
|
| 38 |
|
| 39 |
import json
|
| 40 |
import random
|
|
@@ -55,9 +55,6 @@ warnings.filterwarnings(action="ignore", category=UserWarning, module="gensim")
|
|
| 55 |
# Define the class Semantrix
|
| 56 |
class Semantrix:
|
| 57 |
|
| 58 |
-
# Create empty KeyedVectors model with predefined size where the embeddings will be stored
|
| 59 |
-
model = KeyedVectors(768)
|
| 60 |
-
|
| 61 |
# Define the paths for the configuration files and the data
|
| 62 |
config_file_path = "config/lang.json"
|
| 63 |
secret_file_path = "config/secret.json"
|
|
@@ -85,33 +82,23 @@ class Semantrix:
|
|
| 85 |
# Set the model type
|
| 86 |
self.model_type = model_type
|
| 87 |
|
| 88 |
-
# Load word2vec model if needed
|
| 89 |
-
if self.model_type == "word2vec":
|
| 90 |
-
if self.lang == 1:
|
| 91 |
-
self.model = KeyedVectors.load(
|
| 92 |
-
"config/w2v_models/eng_w2v_model", mmap="r"
|
| 93 |
-
)
|
| 94 |
-
self.Config = self.DictWrapper(self.Config_full["ENG"]["Game"])
|
| 95 |
-
self.secret_dict = self.secret["ENG"]
|
| 96 |
-
else:
|
| 97 |
-
self.model = KeyedVectors.load(
|
| 98 |
-
"config/w2v_models/esp_w2v_model", mmap="r"
|
| 99 |
-
)
|
| 100 |
-
self.Config = self.DictWrapper(self.Config_full["SPA"]["Game"])
|
| 101 |
-
self.secret_dict = self.secret["SPA"]
|
| 102 |
-
else:
|
| 103 |
-
self.model_st = SentenceTransformer(
|
| 104 |
-
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
| 105 |
-
)
|
| 106 |
-
|
| 107 |
-
# Set the configuration variables depending on the language
|
| 108 |
if self.lang == 1:
|
|
|
|
| 109 |
self.Config = self.DictWrapper(self.Config_full["ENG"]["Game"])
|
| 110 |
self.secret_dict = self.secret["ENG"]
|
| 111 |
else:
|
|
|
|
| 112 |
self.Config = self.DictWrapper(self.Config_full["SPA"]["Game"])
|
| 113 |
self.secret_dict = self.secret["SPA"]
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Create the ranking file
|
| 116 |
with open(self.data_path + "ranking.txt", "w+") as file:
|
| 117 |
file.write("---------------------------")
|
|
@@ -137,12 +124,11 @@ class Semantrix:
|
|
| 137 |
self.scores = [10]
|
| 138 |
|
| 139 |
# Store the embedding of the secret word in the embeddings dictionary
|
| 140 |
-
if self.
|
| 141 |
-
|
| 142 |
# Add the secret word to the KeyedVectors model if the model type is SentenceTransformer
|
| 143 |
# If the model type is word2vec, the secret word is already in the model
|
| 144 |
-
if self.
|
| 145 |
-
self.
|
| 146 |
self.secret,
|
| 147 |
self.model_st.encode(self.secret, convert_to_tensor=True).tolist(),
|
| 148 |
)
|
|
@@ -206,31 +192,40 @@ class Semantrix:
|
|
| 206 |
|
| 207 |
# Check if the word is in the model already
|
| 208 |
if word not in self.model.key_to_index.keys():
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
# Calculate the score of the word, apply logarithmic scaling, interpolate the score to a range from 0 to 10, and round it to two decimal places
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
score = round(
|
| 231 |
np.interp(
|
| 232 |
-
|
| 233 |
-
[0, np.
|
| 234 |
[0, 10],
|
| 235 |
),
|
| 236 |
2,
|
|
|
|
| 1 |
+
# """
|
| 2 |
+
# Semantrix Game Module
|
| 3 |
+
|
| 4 |
+
# This module defines the Semantrix class, which implements a word guessing game using word embeddings. The game can be configured to use either a Word2Vec model or a SentenceTransformer model for word embeddings. The game supports multiple languages and difficulty levels.
|
| 5 |
+
|
| 6 |
+
# Classes:
|
| 7 |
+
# Semantrix: A class that implements the Semantrix word guessing game.
|
| 8 |
+
# Semantrix.DictWrapper: A helper class to wrap configuration dictionaries.
|
| 9 |
+
|
| 10 |
+
# Functions:
|
| 11 |
+
# __init__(self, lang=0, model_type="SentenceTransformer"): Initializes the Semantrix game with the specified language and model type.
|
| 12 |
+
# prepare_game(self, difficulty): Prepares the game with the selected difficulty level.
|
| 13 |
+
# gen_rank(self, repeated): Generates the ranking file based on the scores.
|
| 14 |
+
# play_game(self, word): Plays the game with the selected word and returns feedback.
|
| 15 |
+
# curiosity(self): Generates a curiosity hint about the secret word once the game is over.
|
| 16 |
+
|
| 17 |
+
# Attributes:
|
| 18 |
+
# model (KeyedVectors): The word embeddings model.
|
| 19 |
+
# config_file_path (str): Path to the configuration file.
|
| 20 |
+
# secret_file_path (str): Path to the secret words file.
|
| 21 |
+
# data_path (str): Path to the data directory.
|
| 22 |
+
# Config_full (dict): Full configuration data.
|
| 23 |
+
# secret (dict): Secret words data.
|
| 24 |
+
# lang (int): Language of the game (0 for Spanish, 1 for English).
|
| 25 |
+
# model_type (str): Type of the model ("word2vec" or "SentenceTransformer").
|
| 26 |
+
# Config (DictWrapper): Configuration data for the selected language.
|
| 27 |
+
# secret_dict (dict): Secret words for the selected language.
|
| 28 |
+
# secret_list (list): List of secret words for the selected difficulty.
|
| 29 |
+
# words (list): List of words guessed by the player.
|
| 30 |
+
# scores (list): List of scores for the guessed words.
|
| 31 |
+
# win (bool): Indicates if the player has won the game.
|
| 32 |
+
# n (int): Number of hints given.
|
| 33 |
+
# recent_hint (int): Counter for recent hints.
|
| 34 |
+
# f_dev_avg (float): Moving average of the tendency slope.
|
| 35 |
+
# last_hint (int): Index of the last hint given.
|
| 36 |
+
# difficulty (int): Difficulty level of the game.
|
| 37 |
+
# """
|
| 38 |
|
| 39 |
import json
|
| 40 |
import random
|
|
|
|
| 55 |
# Define the class Semantrix
|
| 56 |
class Semantrix:
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
# Define the paths for the configuration files and the data
|
| 59 |
config_file_path = "config/lang.json"
|
| 60 |
secret_file_path = "config/secret.json"
|
|
|
|
| 82 |
# Set the model type
|
| 83 |
self.model_type = model_type
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if self.lang == 1:
|
| 86 |
+
self.model = KeyedVectors.load("config/w2v_models/eng_w2v_model", mmap="r")
|
| 87 |
self.Config = self.DictWrapper(self.Config_full["ENG"]["Game"])
|
| 88 |
self.secret_dict = self.secret["ENG"]
|
| 89 |
else:
|
| 90 |
+
self.model = KeyedVectors.load("config/w2v_models/esp_w2v_model", mmap="r")
|
| 91 |
self.Config = self.DictWrapper(self.Config_full["SPA"]["Game"])
|
| 92 |
self.secret_dict = self.secret["SPA"]
|
| 93 |
|
| 94 |
+
# Create empty KeyedVectors model with predefined size where the embeddings will be stored if Sentence Transformer used
|
| 95 |
+
if self.model_type == "SentenceTransformer":
|
| 96 |
+
self.model_trans = KeyedVectors(768)
|
| 97 |
+
|
| 98 |
+
self.model_st = SentenceTransformer(
|
| 99 |
+
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
# Create the ranking file
|
| 103 |
with open(self.data_path + "ranking.txt", "w+") as file:
|
| 104 |
file.write("---------------------------")
|
|
|
|
| 124 |
self.scores = [10]
|
| 125 |
|
| 126 |
# Store the embedding of the secret word in the embeddings dictionary
|
| 127 |
+
if self.model_type == "SentenceTransformer":
|
|
|
|
| 128 |
# Add the secret word to the KeyedVectors model if the model type is SentenceTransformer
|
| 129 |
# If the model type is word2vec, the secret word is already in the model
|
| 130 |
+
if self.secret not in self.model_trans.key_to_index.keys():
|
| 131 |
+
self.model_trans.add_vector(
|
| 132 |
self.secret,
|
| 133 |
self.model_st.encode(self.secret, convert_to_tensor=True).tolist(),
|
| 134 |
)
|
|
|
|
| 192 |
|
| 193 |
# Check if the word is in the model already
|
| 194 |
if word not in self.model.key_to_index.keys():
|
| 195 |
+
# If the word is not in the model, remove it from the words list and provide feedback
|
| 196 |
+
self.words.pop(len(self.words) - 1)
|
| 197 |
+
feedback = (
|
| 198 |
+
"I don't know that word. Try again."
|
| 199 |
+
if self.lang == 1
|
| 200 |
+
else "No conozco esa palabra. Inténtalo de nuevo."
|
| 201 |
+
)
|
| 202 |
+
feedback += (
|
| 203 |
+
"[rank]" + open(self.data_path + "ranking.txt", "r").read()
|
| 204 |
+
if len(self.words) > 1
|
| 205 |
+
else "\n\n"
|
| 206 |
+
)
|
| 207 |
+
return feedback
|
| 208 |
+
|
| 209 |
+
# Add the word to the KeyedVectors model if the model type is SentenceTransformer and the word is not already in the model
|
| 210 |
+
if (
|
| 211 |
+
self.model_type == "SentenceTransformer"
|
| 212 |
+
and word not in self.model_trans.key_to_index.keys()
|
| 213 |
+
):
|
| 214 |
+
self.model_trans.add_vector(
|
| 215 |
+
word, self.model_st.encode(word, convert_to_tensor=True).tolist()
|
| 216 |
+
)
|
| 217 |
|
| 218 |
# Calculate the score of the word, apply logarithmic scaling, interpolate the score to a range from 0 to 10, and round it to two decimal places
|
| 219 |
+
if self.model_type == "word2vec":
|
| 220 |
+
similarity = self.model.similarity(self.secret, word)
|
| 221 |
+
else:
|
| 222 |
+
similarity = self.model_trans.similarity(self.secret, word)
|
| 223 |
+
|
| 224 |
+
log_similarity = np.log10((similarity if similarity > 0 else 0) * 10)
|
| 225 |
score = round(
|
| 226 |
np.interp(
|
| 227 |
+
log_similarity,
|
| 228 |
+
[0, np.log10(10)],
|
| 229 |
[0, 10],
|
| 230 |
),
|
| 231 |
2,
|
hints.py
CHANGED
|
@@ -1,36 +1,36 @@
|
|
| 1 |
-
"""
|
| 2 |
-
This module provides functions to generate dynamic hints and curiosities about a secret word using llms.
|
| 3 |
|
| 4 |
-
Functions:
|
| 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 |
import random
|
| 36 |
import openai
|
|
|
|
| 1 |
+
# """
|
| 2 |
+
# This module provides functions to generate dynamic hints and curiosities about a secret word using llms.
|
| 3 |
|
| 4 |
+
# Functions:
|
| 5 |
+
# hint(secret, n, model, last_hint, lang, Config):
|
| 6 |
+
# Generates a dynamic hint based on the secret word and the number of hints given.
|
| 7 |
+
# Parameters:
|
| 8 |
+
# secret (str): The secret word.
|
| 9 |
+
# n (int): The number of hints already given.
|
| 10 |
+
# model: The sentence transformer model used for encoding.
|
| 11 |
+
# last_hint (int): The index of the last hint given.
|
| 12 |
+
# lang (int): The language code (0 for Spanish, 1 for English).
|
| 13 |
+
# Config: Configuration object containing hint templates.
|
| 14 |
+
# Returns:
|
| 15 |
+
# tuple: A tuple containing the generated hint (str), the updated number of hints (int), and the index of the last hint given (int).
|
| 16 |
|
| 17 |
+
# curiosity(secret, Config):
|
| 18 |
+
# Generates a curiosity about the secret word.
|
| 19 |
+
# Parameters:
|
| 20 |
+
# secret (str): The secret word.
|
| 21 |
+
# Config: Configuration object containing the curiosity template.
|
| 22 |
+
# Returns:
|
| 23 |
+
# str: The generated curiosity.
|
| 24 |
|
| 25 |
+
# ireplace(old, new, text):
|
| 26 |
+
# Replaces all occurrences of a substring in a string, case-insensitively.
|
| 27 |
+
# Parameters:
|
| 28 |
+
# old (str): The substring to be replaced.
|
| 29 |
+
# new (str): The substring to replace with.
|
| 30 |
+
# text (str): The original string.
|
| 31 |
+
# Returns:
|
| 32 |
+
# str: The modified string with all occurrences of the old substring replaced by the new substring.
|
| 33 |
+
# """
|
| 34 |
|
| 35 |
import random
|
| 36 |
import openai
|