Spaces:
Runtime error
Runtime error
Test async background task
Browse files- app.py +9 -4
- background_task.py +44 -15
app.py
CHANGED
|
@@ -6,8 +6,9 @@ from huggingface_hub import HfApi, hf_hub_download, Repository
|
|
| 6 |
from huggingface_hub.repocard import metadata_load
|
| 7 |
import pandas as pd
|
| 8 |
from matchmaking import *
|
| 9 |
-
from background_task import init_matchmaking
|
| 10 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
DATASET_REPO_URL = "https://huggingface.co/datasets/CarlCochet/BotFightData"
|
|
@@ -22,9 +23,13 @@ matchmaking = Matchmaking()
|
|
| 22 |
api = HfApi()
|
| 23 |
|
| 24 |
|
| 25 |
-
scheduler = BackgroundScheduler()
|
| 26 |
-
scheduler.add_job(func=init_matchmaking, trigger="interval", seconds=15000)
|
| 27 |
-
scheduler.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def get_elo_data() -> pd.DataFrame:
|
|
|
|
| 6 |
from huggingface_hub.repocard import metadata_load
|
| 7 |
import pandas as pd
|
| 8 |
from matchmaking import *
|
| 9 |
+
from background_task import init_matchmaking, run_background_loop
|
| 10 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 11 |
+
import asyncio
|
| 12 |
|
| 13 |
|
| 14 |
DATASET_REPO_URL = "https://huggingface.co/datasets/CarlCochet/BotFightData"
|
|
|
|
| 23 |
api = HfApi()
|
| 24 |
|
| 25 |
|
| 26 |
+
# scheduler = BackgroundScheduler()
|
| 27 |
+
# scheduler.add_job(func=init_matchmaking, trigger="interval", seconds=15000)
|
| 28 |
+
# scheduler.start()
|
| 29 |
+
|
| 30 |
+
loop = asyncio.get_event_loop()
|
| 31 |
+
loop.create_task(run_background_loop())
|
| 32 |
+
loop.run_forever()
|
| 33 |
|
| 34 |
|
| 35 |
def get_elo_data() -> pd.DataFrame:
|
background_task.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import random
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from datetime import datetime
|
| 5 |
from huggingface_hub import HfApi, Repository
|
|
@@ -64,12 +67,39 @@ class Matchmaking:
|
|
| 64 |
while len(self.queue) > 1:
|
| 65 |
model1 = self.queue.pop(0)
|
| 66 |
model2 = self.queue.pop(self.find_n_closest_indexes(model1, 10))
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
self.matches["model1"].append(model1.name)
|
| 70 |
self.matches["model2"].append(model2.name)
|
| 71 |
self.matches["result"].append(result)
|
| 72 |
-
self.matches["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
def compute_elo(self, model1, model2, result):
|
| 75 |
""" Compute the new elo for each model based on a match result. """
|
|
@@ -118,34 +148,27 @@ class Matchmaking:
|
|
| 118 |
repo.push_to_hub(commit_message="Update ELO")
|
| 119 |
|
| 120 |
|
| 121 |
-
def match(model1, model2)
|
| 122 |
"""
|
| 123 |
-
!!! Current code is placeholder !!!
|
| 124 |
-
TODO: Launch a Unity process with the 2 models and get the result of the match
|
| 125 |
-
|
| 126 |
:param model1: First Model object
|
| 127 |
:param model2: Second Model object
|
| 128 |
:return: match result (0: model1 lost, 0.5: draw, 1: model1 won)
|
| 129 |
"""
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
model1.games_played += 1
|
| 133 |
model2.games_played += 1
|
| 134 |
-
return result
|
| 135 |
|
| 136 |
|
| 137 |
def get_models_list() -> list:
|
| 138 |
"""
|
| 139 |
-
!!! Current code is placeholder !!!
|
| 140 |
-
TODO: Create a list of Model objects from the models found on the hub
|
| 141 |
-
|
| 142 |
:return: list of Model objects
|
| 143 |
"""
|
| 144 |
models = []
|
| 145 |
models_names = []
|
| 146 |
data = pd.read_csv(os.path.join(DATASET_REPO_URL, "resolve", "main", ELO_FILENAME))
|
| 147 |
-
|
| 148 |
-
models_on_hub = []
|
| 149 |
for i, row in data.iterrows():
|
| 150 |
models.append(Model(row["author"], row["model"], row["elo"], row["games_played"]))
|
| 151 |
models_names.append(row["model"])
|
|
@@ -163,6 +186,12 @@ def init_matchmaking():
|
|
| 163 |
print("Matchmaking done ---", datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
| 164 |
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
if __name__ == "__main__":
|
| 167 |
print("It's running!")
|
| 168 |
api = HfApi()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import random
|
| 4 |
+
import asyncio
|
| 5 |
+
import subprocess
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
| 8 |
from huggingface_hub import HfApi, Repository
|
|
|
|
| 67 |
while len(self.queue) > 1:
|
| 68 |
model1 = self.queue.pop(0)
|
| 69 |
model2 = self.queue.pop(self.find_n_closest_indexes(model1, 10))
|
| 70 |
+
match(model1, model2)
|
| 71 |
+
self.load_results()
|
| 72 |
+
|
| 73 |
+
def load_results(self):
|
| 74 |
+
""" Load the match history from the hub. """
|
| 75 |
+
repo.git_pull()
|
| 76 |
+
results = pd.read_csv(
|
| 77 |
+
"https://huggingface.co/datasets/huggingface-projects/temp-match-results/raw/main/results.csv"
|
| 78 |
+
)
|
| 79 |
+
# while len(results) < len(self.matches["model1"]):
|
| 80 |
+
# time.sleep(60)
|
| 81 |
+
# results = pd.read_csv(
|
| 82 |
+
# "https://huggingface.co/datasets/huggingface-projects/temp-match-results/raw/main/results.csv"
|
| 83 |
+
# )
|
| 84 |
+
|
| 85 |
+
for i, row in results.iterrows():
|
| 86 |
+
model1 = row["model1"].split("/")
|
| 87 |
+
model2 = row["model2"].split("/")
|
| 88 |
+
model1 = self.find_model(model1[0], model1[1])
|
| 89 |
+
model2 = self.find_model(model2[0], model2[1])
|
| 90 |
+
result = row["result"]
|
| 91 |
+
self.compute_elo(row["model1"], row["model2"], row["result"])
|
| 92 |
self.matches["model1"].append(model1.name)
|
| 93 |
self.matches["model2"].append(model2.name)
|
| 94 |
self.matches["result"].append(result)
|
| 95 |
+
self.matches["timestamp"].append(row["timestamp"])
|
| 96 |
+
|
| 97 |
+
def find_model(self, author, name):
|
| 98 |
+
""" Find a model in the models list. """
|
| 99 |
+
for model in self.models:
|
| 100 |
+
if model.author == author and model.name == name:
|
| 101 |
+
return model
|
| 102 |
+
return None
|
| 103 |
|
| 104 |
def compute_elo(self, model1, model2, result):
|
| 105 |
""" Compute the new elo for each model based on a match result. """
|
|
|
|
| 148 |
repo.push_to_hub(commit_message="Update ELO")
|
| 149 |
|
| 150 |
|
| 151 |
+
def match(model1, model2):
|
| 152 |
"""
|
|
|
|
|
|
|
|
|
|
| 153 |
:param model1: First Model object
|
| 154 |
:param model2: Second Model object
|
| 155 |
:return: match result (0: model1 lost, 0.5: draw, 1: model1 won)
|
| 156 |
"""
|
| 157 |
+
model1_id = model1.author + "/" + model1.name
|
| 158 |
+
model2_id = model2.author + "/" + model2.name
|
| 159 |
+
subprocess.run(["UnityEnvironment.exe", "-model1", model1_id, "-model2", model2_id])
|
| 160 |
model1.games_played += 1
|
| 161 |
model2.games_played += 1
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
def get_models_list() -> list:
|
| 165 |
"""
|
|
|
|
|
|
|
|
|
|
| 166 |
:return: list of Model objects
|
| 167 |
"""
|
| 168 |
models = []
|
| 169 |
models_names = []
|
| 170 |
data = pd.read_csv(os.path.join(DATASET_REPO_URL, "resolve", "main", ELO_FILENAME))
|
| 171 |
+
models_on_hub = api.list_models(filter=["reinforcement-learning", "ml-agents", "ML-Agents-SoccerTwos"])
|
|
|
|
| 172 |
for i, row in data.iterrows():
|
| 173 |
models.append(Model(row["author"], row["model"], row["elo"], row["games_played"]))
|
| 174 |
models_names.append(row["model"])
|
|
|
|
| 186 |
print("Matchmaking done ---", datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
| 187 |
|
| 188 |
|
| 189 |
+
async def run_background_loop():
|
| 190 |
+
while True:
|
| 191 |
+
print("It's running!", datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
|
| 192 |
+
await asyncio.sleep(60)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
if __name__ == "__main__":
|
| 196 |
print("It's running!")
|
| 197 |
api = HfApi()
|