Spaces:
Runtime error
Runtime error
Sasha Rush
commited on
Commit
Β·
ed632fe
1
Parent(s):
2086271
leaderboard
Browse files- app.py +62 -7
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -14,8 +14,23 @@ from contextlib import redirect_stdout
|
|
| 14 |
import imageio
|
| 15 |
import tiktoken
|
| 16 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
openai.api_key = ""
|
| 18 |
tab = " "
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def start2(prompt, board, api_key):
|
| 21 |
out = ""
|
|
@@ -175,6 +190,10 @@ class Game:
|
|
| 175 |
def walls(self):
|
| 176 |
return self.board.wall_pos
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
def __repr__(self) -> str:
|
| 179 |
walls = ",".join(map(str, self.board.wall_pos))
|
| 180 |
return f"Game(init={self.board.player_pos}, flag={self.board.flag_pos}, walls={self.board.wall_pos}, boundary={self.boundary}, key={self.board.key_pos})"
|
|
@@ -379,7 +398,8 @@ GPTWorld is a prompting game. Your goal is to get an LLM to complete a maze. You
|
|
| 379 |
examples = gr.Radio(show_label=False,
|
| 380 |
choices=["Easy", "Medium", "Hard", "Evil"])
|
| 381 |
api_key = gr.Text(label="OpenAI Key", type="password",
|
| 382 |
-
value=os.environ.get("OPENAI_API_KEY")
|
|
|
|
| 383 |
with gr.Row():
|
| 384 |
start_btn = gr.Button("Prompt >")
|
| 385 |
cancel_btn = gr.Button("Cancel")
|
|
@@ -419,6 +439,7 @@ def move(board, action, old_pos):
|
|
| 419 |
with gr.Column():
|
| 420 |
im = gr.Gallery(label="Gallery of the Game")
|
| 421 |
im.style(preview=True, object_fit="scale-down", columns=1, container=True)
|
|
|
|
| 422 |
|
| 423 |
output = gr.Code(label="Generating Game Code (You can also edit and rerun)", language="python", value="""def my_example():
|
| 424 |
b = Game(init=(0, 0), flag=(2, 2), walls= [], boundary= (3, 3), key= (1, 1))
|
|
@@ -430,10 +451,10 @@ def move(board, action, old_pos):
|
|
| 430 |
p = move(b, "R", p)
|
| 431 |
return b
|
| 432 |
""", lines=50)
|
| 433 |
-
|
| 434 |
counter = gr.Slider(label="length", minimum=0, maximum=3000)
|
| 435 |
run_btn = gr.Button("Rerun ^")
|
| 436 |
-
|
| 437 |
|
| 438 |
|
| 439 |
examples.change(load, inputs=[examples], outputs=[im, game_desc])
|
|
@@ -447,7 +468,8 @@ def move(board, action, old_pos):
|
|
| 447 |
i = 0
|
| 448 |
count = 0
|
| 449 |
im_ = [f"tmp.svg"]
|
| 450 |
-
|
|
|
|
| 451 |
|
| 452 |
for prefix in start(inp, board, data[api_key]):
|
| 453 |
ps = prefix.split("\n")
|
|
@@ -467,8 +489,18 @@ def move(board, action, old_pos):
|
|
| 467 |
yield {im: im_, counter: count, output: prefix}
|
| 468 |
else:
|
| 469 |
yield {im: im_, counter: count, output: prefix}
|
| 470 |
-
|
| 471 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
cancel_btn.click(None, cancels=[start_prompt])
|
| 473 |
def run2(data):
|
| 474 |
c = data[output]
|
|
@@ -489,8 +521,31 @@ def move(board, action, old_pos):
|
|
| 489 |
return out
|
| 490 |
run_btn.click(run2, inputs={output}, outputs={im})
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
|
| 493 |
-
|
| 494 |
app.queue().launch()
|
| 495 |
|
| 496 |
|
|
|
|
| 14 |
import imageio
|
| 15 |
import tiktoken
|
| 16 |
import time
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import csv
|
| 19 |
+
from huggingface_hub import HfApi, Repository
|
| 20 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/srush/gptworld-leaderboard"
|
| 21 |
+
HF_TOKEN = os.environ.get("HF_API")
|
| 22 |
+
hf_api = HfApi(
|
| 23 |
+
endpoint="https://huggingface.co", # Can be a Private Hub endpoint.
|
| 24 |
+
token=os.environ.get("HF_API"), # Token is not persisted on the machine.
|
| 25 |
+
)
|
| 26 |
+
DATA_FILENAME = "data.csv"
|
| 27 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
| 28 |
+
|
| 29 |
openai.api_key = ""
|
| 30 |
tab = " "
|
| 31 |
+
repo = Repository(
|
| 32 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 33 |
+
)
|
| 34 |
|
| 35 |
def start2(prompt, board, api_key):
|
| 36 |
out = ""
|
|
|
|
| 190 |
def walls(self):
|
| 191 |
return self.board.wall_pos
|
| 192 |
|
| 193 |
+
def won(self):
|
| 194 |
+
final = self.board
|
| 195 |
+
return final.key_pos is None and final.player_pos == final.flag_pos
|
| 196 |
+
|
| 197 |
def __repr__(self) -> str:
|
| 198 |
walls = ",".join(map(str, self.board.wall_pos))
|
| 199 |
return f"Game(init={self.board.player_pos}, flag={self.board.flag_pos}, walls={self.board.wall_pos}, boundary={self.boundary}, key={self.board.key_pos})"
|
|
|
|
| 398 |
examples = gr.Radio(show_label=False,
|
| 399 |
choices=["Easy", "Medium", "Hard", "Evil"])
|
| 400 |
api_key = gr.Text(label="OpenAI Key", type="password",
|
| 401 |
+
value=os.environ.get("OPENAI_API_KEY"),
|
| 402 |
+
visible=not os.environ.get("OPENAI_API_KEY"))
|
| 403 |
with gr.Row():
|
| 404 |
start_btn = gr.Button("Prompt >")
|
| 405 |
cancel_btn = gr.Button("Cancel")
|
|
|
|
| 439 |
with gr.Column():
|
| 440 |
im = gr.Gallery(label="Gallery of the Game")
|
| 441 |
im.style(preview=True, object_fit="scale-down", columns=1, container=True)
|
| 442 |
+
msg_box = gr.Text(label="", show_label=False)
|
| 443 |
|
| 444 |
output = gr.Code(label="Generating Game Code (You can also edit and rerun)", language="python", value="""def my_example():
|
| 445 |
b = Game(init=(0, 0), flag=(2, 2), walls= [], boundary= (3, 3), key= (1, 1))
|
|
|
|
| 451 |
p = move(b, "R", p)
|
| 452 |
return b
|
| 453 |
""", lines=50)
|
| 454 |
+
|
| 455 |
counter = gr.Slider(label="length", minimum=0, maximum=3000)
|
| 456 |
run_btn = gr.Button("Rerun ^")
|
| 457 |
+
state = gr.State()
|
| 458 |
|
| 459 |
|
| 460 |
examples.change(load, inputs=[examples], outputs=[im, game_desc])
|
|
|
|
| 468 |
i = 0
|
| 469 |
count = 0
|
| 470 |
im_ = [f"tmp.svg"]
|
| 471 |
+
state_val = None
|
| 472 |
+
yield {im: im_, counter: 0, output: "", msg_box: "", state: state_val}
|
| 473 |
|
| 474 |
for prefix in start(inp, board, data[api_key]):
|
| 475 |
ps = prefix.split("\n")
|
|
|
|
| 489 |
yield {im: im_, counter: count, output: prefix}
|
| 490 |
else:
|
| 491 |
yield {im: im_, counter: count, output: prefix}
|
| 492 |
+
if q["board"].won():
|
| 493 |
+
final_msg = "ππππππVictoryππππππ"
|
| 494 |
+
state_val = (data[prompt], prefix, count, data[examples])
|
| 495 |
+
else:
|
| 496 |
+
final_msg = "Didn't make it"
|
| 497 |
+
yield {im: [f"pic{j}.svg" for j in range(i)], counter: count, output: prefix,
|
| 498 |
+
msg_box: final_msg, state: state_val}
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
start_prompt = start_btn.click(run,
|
| 502 |
+
inputs={prompt, game_desc, api_key, examples},
|
| 503 |
+
outputs={im, output, counter, msg_box, state})
|
| 504 |
cancel_btn.click(None, cancels=[start_prompt])
|
| 505 |
def run2(data):
|
| 506 |
c = data[output]
|
|
|
|
| 521 |
return out
|
| 522 |
run_btn.click(run2, inputs={output}, outputs={im})
|
| 523 |
|
| 524 |
+
gr.HTML("""<center><h2>Leaderboard</h2></center>""")
|
| 525 |
+
with gr.Row() as row:
|
| 526 |
+
team_name = gr.Text(label="Team Name")
|
| 527 |
+
leaderboard = gr.Button(value="Submit")
|
| 528 |
+
msg = gr.Text(label="Status")
|
| 529 |
+
leader = gr.Dataframe(pd.read_csv(DATA_FILE)[["team", "board", "count"]])
|
| 530 |
+
def leaderfn(data):
|
| 531 |
+
if data[state] is None:
|
| 532 |
+
return {msg: "Nothing to submit"}
|
| 533 |
+
if not data[team_name]:
|
| 534 |
+
return {msg: "No team name"}
|
| 535 |
+
prompt, code, count, board = data[state]
|
| 536 |
+
repo.git_pull()
|
| 537 |
+
with open(DATA_FILE, "a") as csvfile:
|
| 538 |
+
writer = csv.DictWriter(csvfile, fieldnames=["team", "prompt", "code", "count", "board"])
|
| 539 |
+
writer.writerow(
|
| 540 |
+
{"team": data[team_name], "prompt": prompt, "code": code, "count": count, "board": board}
|
| 541 |
+
)
|
| 542 |
+
commit_url = repo.push_to_hub()
|
| 543 |
+
leader_df = pd.read_csv(DATA_FILE)[["team", "board", "count"]]
|
| 544 |
+
leader_df = leader_df.sort_values(["board", "count"])
|
| 545 |
+
return {msg: f"Sucess: Final score: {count} {board}", leader: leader_df}
|
| 546 |
+
|
| 547 |
+
leaderboard.click(fn=leaderfn, inputs={state, team_name}, outputs={msg, leader})
|
| 548 |
|
|
|
|
| 549 |
app.queue().launch()
|
| 550 |
|
| 551 |
|
requirements.txt
CHANGED
|
@@ -4,3 +4,4 @@ openai
|
|
| 4 |
pycairo
|
| 5 |
tiktoken
|
| 6 |
imageio
|
|
|
|
|
|
| 4 |
pycairo
|
| 5 |
tiktoken
|
| 6 |
imageio
|
| 7 |
+
pandas
|