Javierss commited on
Commit
6b30beb
·
1 Parent(s): 032c75b

Add dataset data storage

Browse files
Files changed (4) hide show
  1. .gitignore +4 -1
  2. app.py +56 -30
  3. game.py +37 -20
  4. requirements.txt +2 -1
.gitignore CHANGED
@@ -1 +1,4 @@
1
- data/plays/*.txt
 
 
 
 
1
+ data/plays/*.txt
2
+ data/plays/*.json
3
+ data/plays/data/*.json
4
+ __pycache__/*
app.py CHANGED
@@ -60,14 +60,22 @@
60
  import gradio as gr
61
  import json
62
  from game import Semantrix
 
 
 
63
 
64
  # File paths for configuration and images
65
  config_file_path = "config/lang.json"
66
  logo_path = "config/images/logo.png"
67
  logo_win_path = "config/images/logo_win.gif"
68
  condition_config_path = "config/condition.json"
 
69
 
70
- condition_name = "condition_3"
 
 
 
 
71
 
72
  lang = "Español" # Default language
73
 
@@ -81,6 +89,14 @@ hints_enabled = condition_config[condition_name]["hints"]
81
  with open(config_file_path, "r") as file:
82
  Config_full = json.load(file)
83
 
 
 
 
 
 
 
 
 
84
  lang_conf = 0 # Language configuration flag (0 for Spanish, 1 for English)
85
 
86
  # Setting the configuration based on the language flag
@@ -155,30 +171,11 @@ with gr.Blocks() as demo:
155
  )
156
 
157
  # Function to reset the game
158
- def reset(difficulty):
159
  global Config, game, Menu, model, lang # Declare global variables to modify them within the function
160
 
161
- # Determine the language based on the input parameter
162
- # language = 2 if lang == "English" else 0
163
-
164
- # # # Initialize the game object based on the selected model
165
- # # if model == "Word2Vec":
166
- # # game = Semantrix(language, "word2vec") # Use Word2Vec model
167
- # # else:
168
- # # game = Semantrix(
169
- # # language, "SentenceTransformer"
170
- # # ) # Use Sentence Transformers model
171
-
172
- # # Set the configuration and menu based on the selected language
173
- # if language == 1:
174
- # Config = Config_full["ENG"]["Game"]
175
- # Menu = Config_full["ENG"]["Menu"]
176
- # else:
177
- # Config = Config_full["SPA"]["Game"]
178
- # Menu = Config_full["SPA"]["Menu"]
179
-
180
- # Prepare the game with the selected difficulty level
181
- # game.prepare_game(1 if hints_enabled else 4)
182
 
183
  # Define the initial output components for the UI
184
  output = [
@@ -331,7 +328,7 @@ with gr.Blocks() as demo:
331
  gr.Button(Menu["Send"], visible=True, variant="primary"),
332
  gr.Radio(label="", visible=False),
333
  gr.Textbox(visible=False, label=""),
334
- gr.Button(visible=True, variant="stop"),
335
  gr.Textbox(
336
  value="",
337
  visible=True,
@@ -447,6 +444,34 @@ with gr.Blocks() as demo:
447
  # Return the output components
448
  return output
449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
  # Define the UI layout for the gam
451
  img = gr.Image(logo_path, height=430, interactive=False, visible=True)
452
  ranking = gr.Markdown(visible=False)
@@ -463,9 +488,9 @@ with gr.Blocks() as demo:
463
  give_up = gr.Button("Pista", visible=False)
464
  reload = gr.Button(Menu["Play_again"], visible=False)
465
  finish = gr.Button(
466
- "Terminar y rellenar cuestionario",
467
  variant="stop",
468
- link="https://docs.google.com/forms/d/e/1FAIpQLSd0z8nI4hhOSR83yPIw_bR3KkSt25Lsq0ZXG1pZnkldeoceqA/viewform?usp=pp_url&entry.327829192=Condici%C3%B3n+3",
469
  visible=False,
470
  )
471
 
@@ -483,16 +508,17 @@ with gr.Blocks() as demo:
483
  outputs=[state, inp],
484
  )
485
  radio.input(change, inputs=[state, inp], outputs=[state, inp])
486
-
487
- # Define events that trigger the game reset
 
488
  reload.click(
489
  reset,
490
- inputs=[difficulty],
491
  outputs=[state, out, inp, img, but, radio, hint_out, reload, header, finish],
492
  )
493
  demo.load(
494
  reset,
495
- inputs=[difficulty],
496
  outputs=[state, out, inp, img, but, radio, hint_out, reload, header, finish],
497
  )
498
 
 
60
  import gradio as gr
61
  import json
62
  from game import Semantrix
63
+ from huggingface_hub import CommitScheduler
64
+ import os
65
+
66
 
67
  # File paths for configuration and images
68
  config_file_path = "config/lang.json"
69
  logo_path = "config/images/logo.png"
70
  logo_win_path = "config/images/logo_win.gif"
71
  condition_config_path = "config/condition.json"
72
+ data_path = "data/plays"
73
 
74
+ condition_name = "condition_2"
75
+ # Dynamically determine the condition name based on the folder name
76
+ # folder_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
77
+ # condition = folder_name.split("_")[-1][-1]
78
+ # condition_name = "condition_" + condition
79
 
80
  lang = "Español" # Default language
81
 
 
89
  with open(config_file_path, "r") as file:
90
  Config_full = json.load(file)
91
 
92
+ scheduler = CommitScheduler(
93
+ repo_id="Jsevisal/semantrix_data_" + condition_name[-1],
94
+ repo_type="dataset",
95
+ folder_path=data_path + "/data",
96
+ path_in_repo="data",
97
+ every=10,
98
+ )
99
+
100
  lang_conf = 0 # Language configuration flag (0 for Spanish, 1 for English)
101
 
102
  # Setting the configuration based on the language flag
 
171
  )
172
 
173
  # Function to reset the game
174
+ def reset(reload):
175
  global Config, game, Menu, model, lang # Declare global variables to modify them within the function
176
 
177
+ if reload:
178
+ game.reset_game() # Reset the game state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  # Define the initial output components for the UI
181
  output = [
 
328
  gr.Button(Menu["Send"], visible=True, variant="primary"),
329
  gr.Radio(label="", visible=False),
330
  gr.Textbox(visible=False, label=""),
331
+ gr.Button(visible=False, variant="stop"),
332
  gr.Textbox(
333
  value="",
334
  visible=True,
 
444
  # Return the output components
445
  return output
446
 
447
+ def commit_data():
448
+ session_id = game.get_session_id()
449
+ session_files = [f for f in os.listdir(data_path) if f.startswith(session_id)]
450
+ combined_data = []
451
+
452
+ for file_name in session_files:
453
+ with open(os.path.join(data_path, file_name), "r") as file:
454
+ combined_data.append(json.load(file))
455
+
456
+ combined_file_path = os.path.join(data_path + "/data", f"{session_id}.json")
457
+ with open(combined_file_path, "w") as combined_file:
458
+ json.dump(combined_data, combined_file, indent=4)
459
+
460
+ scheduler.push_to_hub()
461
+
462
+ return [
463
+ gr.Button(visible=False),
464
+ gr.Textbox(visible=False),
465
+ gr.Textbox(visible=False),
466
+ gr.Button(
467
+ "Rellenar cuestionario",
468
+ variant="primary",
469
+ link="https://docs.google.com/forms/d/e/1FAIpQLSd0z8nI4hhOSR83yPIw_bR3KkSt25Lsq0ZXG1pZnkldeoceqA/viewform?usp=pp_url&entry.327829192="
470
+ + session_id,
471
+ visible=True,
472
+ ),
473
+ ]
474
+
475
  # Define the UI layout for the gam
476
  img = gr.Image(logo_path, height=430, interactive=False, visible=True)
477
  ranking = gr.Markdown(visible=False)
 
488
  give_up = gr.Button("Pista", visible=False)
489
  reload = gr.Button(Menu["Play_again"], visible=False)
490
  finish = gr.Button(
491
+ "Terminar",
492
  variant="stop",
493
+ # link="https://docs.google.com/forms/d/e/1FAIpQLSd0z8nI4hhOSR83yPIw_bR3KkSt25Lsq0ZXG1pZnkldeoceqA/viewform?usp=pp_url&entry.327829192=Condici%C3%B3n+0",
494
  visible=False,
495
  )
496
 
 
508
  outputs=[state, inp],
509
  )
510
  radio.input(change, inputs=[state, inp], outputs=[state, inp])
511
+ finish.click(
512
+ commit_data, outputs=[reload, out, hint_out, finish]
513
+ ) # Define events that trigger the game reset
514
  reload.click(
515
  reset,
516
+ inputs=[gr.State(False)],
517
  outputs=[state, out, inp, img, but, radio, hint_out, reload, header, finish],
518
  )
519
  demo.load(
520
  reset,
521
+ inputs=[gr.State(True)],
522
  outputs=[state, out, inp, img, but, radio, hint_out, reload, header, finish],
523
  )
524
 
game.py CHANGED
@@ -37,8 +37,10 @@
37
  # """
38
 
39
  import json
 
40
  import random
41
  from datetime import datetime
 
42
  from tqdm import tqdm
43
  import numpy as np
44
  from gensim.models import KeyedVectors
@@ -80,6 +82,8 @@ class Semantrix:
80
  # Set the language of the game
81
  self.lang = lang
82
 
 
 
83
  # Set the model type
84
  self.model_type = model_type
85
 
@@ -94,6 +98,8 @@ class Semantrix:
94
  )
95
  self.Config = self.DictWrapper(self.Config_full["ENG"]["Game"])
96
  self.secret_dict = self.secret["ENG"]
 
 
97
  else:
98
  if self.model_type == "word2vec":
99
  self.model = KeyedVectors.load(
@@ -106,6 +112,7 @@ class Semantrix:
106
 
107
  self.Config = self.DictWrapper(self.Config_full["SPA"]["Game"])
108
  self.secret_dict = self.secret["SPA"]
 
109
 
110
  self.model_st = SentenceTransformer(
111
  "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
@@ -115,22 +122,23 @@ class Semantrix:
115
  with open(self.data_path + "ranking.txt", "w+") as file:
116
  file.write("---------------------------")
117
 
 
 
 
 
 
 
 
 
118
  # Define the function to prepare the game with the selected difficulty
119
  def prepare_game(self, difficulty):
120
 
121
  # Set the secret list depending on the difficulty
122
- # self.secret_list = (
123
- # self.secret_dict["basic"]
124
- # if difficulty <= 2
125
- # else self.secret_dict["advanced"]
126
- # )
127
- self.secret_list = self.secret_dict["basic"]
128
-
129
- # Select a random secret word from the secret list
130
- # self.secret = self.secret_list.pop(random.randint(0, len(self.secret_list) - 1))
131
  self.secret = self.secret_list.pop(0)
132
  self.secret = self.secret.lower()
133
 
 
 
134
  # Store the secret word in the words list
135
  self.words = [self.Config.secret_word]
136
 
@@ -298,17 +306,6 @@ class Semantrix:
298
  # Add the ranking file to the feedback message
299
  feedback += "[rank]" + open(self.data_path + "ranking.txt", "r").read()
300
 
301
- # Save the ranking file with the plays of the user if the user wins
302
- if self.win:
303
-
304
- with open(self.data_path + "ranking.txt", "r") as original_file:
305
- file_content = original_file.readlines()
306
-
307
- new_file_name = self.secret + "_" + str(datetime.now()) + ".txt"
308
-
309
- with open(self.data_path + "plays/" + new_file_name, "w+") as new_file:
310
- new_file.writelines(file_content[2:])
311
-
312
  # Return the feedback message
313
  return feedback
314
 
@@ -325,5 +322,25 @@ class Semantrix:
325
  ),
326
  )
327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328
  # Return the feedback message
329
  return feedback
 
 
 
 
37
  # """
38
 
39
  import json
40
+ import uuid
41
  import random
42
  from datetime import datetime
43
+ import time
44
  from tqdm import tqdm
45
  import numpy as np
46
  from gensim.models import KeyedVectors
 
82
  # Set the language of the game
83
  self.lang = lang
84
 
85
+ self.session_id = str(uuid.uuid4().hex)
86
+
87
  # Set the model type
88
  self.model_type = model_type
89
 
 
98
  )
99
  self.Config = self.DictWrapper(self.Config_full["ENG"]["Game"])
100
  self.secret_dict = self.secret["ENG"]
101
+ self.secret_dict = self.secret["ENG"]
102
+ self.secret_list = self.secret_dict["basic"]
103
  else:
104
  if self.model_type == "word2vec":
105
  self.model = KeyedVectors.load(
 
112
 
113
  self.Config = self.DictWrapper(self.Config_full["SPA"]["Game"])
114
  self.secret_dict = self.secret["SPA"]
115
+ self.secret_list = self.secret_dict["basic"]
116
 
117
  self.model_st = SentenceTransformer(
118
  "sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
 
122
  with open(self.data_path + "ranking.txt", "w+") as file:
123
  file.write("---------------------------")
124
 
125
+ def reset_game(self):
126
+ self.session_id = str(uuid.uuid4().hex)
127
+ # Load the secret file where the secret words are stored
128
+ with open(self.secret_file_path, "r") as file:
129
+ self.secret = json.load(file)
130
+ self.secret_dict = self.secret["SPA"]
131
+ self.secret_list = self.secret_dict["basic"]
132
+
133
  # Define the function to prepare the game with the selected difficulty
134
  def prepare_game(self, difficulty):
135
 
136
  # Set the secret list depending on the difficulty
 
 
 
 
 
 
 
 
 
137
  self.secret = self.secret_list.pop(0)
138
  self.secret = self.secret.lower()
139
 
140
+ self.init_time = time.time()
141
+
142
  # Store the secret word in the words list
143
  self.words = [self.Config.secret_word]
144
 
 
306
  # Add the ranking file to the feedback message
307
  feedback += "[rank]" + open(self.data_path + "ranking.txt", "r").read()
308
 
 
 
 
 
 
 
 
 
 
 
 
309
  # Return the feedback message
310
  return feedback
311
 
 
322
  ),
323
  )
324
 
325
+ # Save the ranking file with the plays of the user if the user wins
326
+ with open(self.data_path + "ranking.txt", "r") as original_file:
327
+ file_content = original_file.readlines()[2:]
328
+ new_file_name = f"{self.session_id}-{self.secret}.json"
329
+ play_data = {
330
+ "session_id": self.session_id,
331
+ "datetime": str(datetime.now()),
332
+ "time": time.time() - self.init_time,
333
+ "data": file_content,
334
+ "win": self.win,
335
+ "secret": self.secret,
336
+ "number_of_hints": self.n,
337
+ }
338
+
339
+ with open(self.data_path + "plays/" + new_file_name, "w") as new_file:
340
+ json.dump(play_data, new_file, indent=4)
341
+
342
  # Return the feedback message
343
  return feedback
344
+
345
+ def get_session_id(self):
346
+ return self.session_id
requirements.txt CHANGED
@@ -4,4 +4,5 @@ openai==1.2.3
4
  gradio==5.1.0
5
  transformers==4.33.2
6
  huggingface-hub==0.25.2
7
- python-multipart==0.0.12
 
 
4
  gradio==5.1.0
5
  transformers==4.33.2
6
  huggingface-hub==0.25.2
7
+ python-multipart==0.0.12
8
+ httpx==0.27.2