Spaces:
Sleeping
Sleeping
ping98k
commited on
Commit
·
753833c
1
Parent(s):
2584782
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,21 +1,31 @@
|
|
| 1 |
-
import os, json
|
| 2 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 3 |
from tqdm import tqdm
|
| 4 |
from litellm import completion
|
| 5 |
-
import gradio as gr
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
instruction = instruction_input.strip()
|
| 14 |
criteria_list = [c.strip() for c in criteria_input.split(",") if c.strip()] or [
|
| 15 |
-
"Factuality",
|
| 16 |
-
"Instruction Following",
|
| 17 |
-
"Precision",
|
| 18 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def criteria_block():
|
| 21 |
return "\n".join(f"{i + 1}) {c}" for i, c in enumerate(criteria_list))
|
|
@@ -24,7 +34,7 @@ def run_tournament(instruction_input, criteria_input):
|
|
| 24 |
prompt = f"""Evaluate the output below on the following criteria:
|
| 25 |
{criteria_block()}
|
| 26 |
|
| 27 |
-
Return JSON exactly like: {{
|
| 28 |
|
| 29 |
Instruction:
|
| 30 |
{instruction}
|
|
@@ -33,7 +43,7 @@ Output:
|
|
| 33 |
{player}"""
|
| 34 |
response = completion(
|
| 35 |
model="gpt-4o-mini",
|
| 36 |
-
messages=[{"role": "system", "content": prompt}]
|
| 37 |
)
|
| 38 |
return response.choices[0].message.content.strip()
|
| 39 |
|
|
@@ -49,7 +59,7 @@ Output:
|
|
| 49 |
prompt = f"""Compare the two players below using:
|
| 50 |
{criteria_block()}
|
| 51 |
|
| 52 |
-
Return ONLY JSON {{
|
| 53 |
|
| 54 |
Instruction:
|
| 55 |
{instruction}
|
|
@@ -59,7 +69,7 @@ Players:
|
|
| 59 |
<B>{b}</B>"""
|
| 60 |
response = completion(
|
| 61 |
model="gpt-4o-mini",
|
| 62 |
-
messages=[{"role": "system", "content": prompt}]
|
| 63 |
)
|
| 64 |
return response.choices[0].message.content.strip()
|
| 65 |
|
|
@@ -104,28 +114,28 @@ Players:
|
|
| 104 |
|
| 105 |
def playoff(candidates, executor):
|
| 106 |
wins = {p: 0 for p in candidates}
|
| 107 |
-
pairs = [
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
for j in range(i + 1, len(candidates))
|
| 111 |
-
]
|
| 112 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 113 |
for fut in tqdm(as_completed(futures), total=len(futures)):
|
| 114 |
wins[fut.result()] += 1
|
| 115 |
return sorted(candidates, key=lambda p: wins[p], reverse=True)
|
| 116 |
|
| 117 |
-
def get_top(players, executor
|
| 118 |
champion, lost_to = tournament(players, executor)
|
| 119 |
runner_up = lost_to.get(champion)
|
| 120 |
finalists = [champion] + ([runner_up] if runner_up else [])
|
| 121 |
-
semifinalists = [p for p, o in lost_to.items()
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
scores = precompute_scores(all_players, ex)
|
| 128 |
-
top_players = sorted(all_players, key=scores.get, reverse=True)[:
|
| 129 |
top_k = get_top(top_players, ex)
|
| 130 |
return ", ".join(top_k)
|
| 131 |
|
|
@@ -134,8 +144,12 @@ demo = gr.Interface(
|
|
| 134 |
inputs=[
|
| 135 |
gr.Textbox(lines=2, label="Instruction"),
|
| 136 |
gr.Textbox(lines=1, label="Criteria (comma separated)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
],
|
| 138 |
-
outputs=gr.Textbox(label="Top picks")
|
| 139 |
)
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import os, json, gradio as gr
|
| 2 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 3 |
from tqdm import tqdm
|
| 4 |
from litellm import completion
|
|
|
|
| 5 |
|
| 6 |
+
NUM_TOP_PICKS_DEFAULT = int(os.getenv("NUM_TOP_PICKS", 5))
|
| 7 |
+
POOL_SIZE_DEFAULT = int(os.getenv("POOL_SIZE", 20))
|
| 8 |
+
MAX_WORKERS_DEFAULT = int(os.getenv("MAX_WORKERS", 10))
|
| 9 |
+
NUM_GENERATIONS_DEFAULT = int(os.getenv("NUM_GENERATIONS", 100))
|
| 10 |
+
|
| 11 |
+
def generate_players(instruction, n):
|
| 12 |
+
response = completion(
|
| 13 |
+
model="gpt-4o-mini",
|
| 14 |
+
messages=[{"role": "user", "content": instruction}],
|
| 15 |
+
n=n
|
| 16 |
+
)
|
| 17 |
+
return [c.message.content.strip() for c in response.choices]
|
| 18 |
+
|
| 19 |
+
def run_tournament(instruction_input, criteria_input, n_gen,
|
| 20 |
+
num_top_picks, pool_size, max_workers):
|
| 21 |
instruction = instruction_input.strip()
|
| 22 |
criteria_list = [c.strip() for c in criteria_input.split(",") if c.strip()] or [
|
| 23 |
+
"Factuality", "Instruction Following", "Precision"
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
+
n_gen = int(n_gen)
|
| 26 |
+
num_top_picks = int(num_top_picks)
|
| 27 |
+
pool_size = int(pool_size)
|
| 28 |
+
max_workers = int(max_workers)
|
| 29 |
|
| 30 |
def criteria_block():
|
| 31 |
return "\n".join(f"{i + 1}) {c}" for i, c in enumerate(criteria_list))
|
|
|
|
| 34 |
prompt = f"""Evaluate the output below on the following criteria:
|
| 35 |
{criteria_block()}
|
| 36 |
|
| 37 |
+
Return JSON exactly like: {{"score": [{', '.join(['1-10'] * len(criteria_list))}]}}.
|
| 38 |
|
| 39 |
Instruction:
|
| 40 |
{instruction}
|
|
|
|
| 43 |
{player}"""
|
| 44 |
response = completion(
|
| 45 |
model="gpt-4o-mini",
|
| 46 |
+
messages=[{"role": "system", "content": prompt}]
|
| 47 |
)
|
| 48 |
return response.choices[0].message.content.strip()
|
| 49 |
|
|
|
|
| 59 |
prompt = f"""Compare the two players below using:
|
| 60 |
{criteria_block()}
|
| 61 |
|
| 62 |
+
Return ONLY JSON {{"winner": "A"}} or {{"winner": "B"}}.
|
| 63 |
|
| 64 |
Instruction:
|
| 65 |
{instruction}
|
|
|
|
| 69 |
<B>{b}</B>"""
|
| 70 |
response = completion(
|
| 71 |
model="gpt-4o-mini",
|
| 72 |
+
messages=[{"role": "system", "content": prompt}]
|
| 73 |
)
|
| 74 |
return response.choices[0].message.content.strip()
|
| 75 |
|
|
|
|
| 114 |
|
| 115 |
def playoff(candidates, executor):
|
| 116 |
wins = {p: 0 for p in candidates}
|
| 117 |
+
pairs = [(candidates[i], candidates[j])
|
| 118 |
+
for i in range(len(candidates))
|
| 119 |
+
for j in range(i + 1, len(candidates))]
|
|
|
|
|
|
|
| 120 |
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 121 |
for fut in tqdm(as_completed(futures), total=len(futures)):
|
| 122 |
wins[fut.result()] += 1
|
| 123 |
return sorted(candidates, key=lambda p: wins[p], reverse=True)
|
| 124 |
|
| 125 |
+
def get_top(players, executor):
|
| 126 |
champion, lost_to = tournament(players, executor)
|
| 127 |
runner_up = lost_to.get(champion)
|
| 128 |
finalists = [champion] + ([runner_up] if runner_up else [])
|
| 129 |
+
semifinalists = [p for p, o in lost_to.items()
|
| 130 |
+
if o in finalists and p not in finalists]
|
| 131 |
+
candidates = set(finalists + semifinalists +
|
| 132 |
+
get_candidates(champion, lost_to))
|
| 133 |
+
return playoff(list(candidates), executor)[:num_top_picks]
|
| 134 |
+
|
| 135 |
+
all_players = generate_players(instruction, n_gen)
|
| 136 |
+
with ThreadPoolExecutor(max_workers=max_workers) as ex:
|
| 137 |
scores = precompute_scores(all_players, ex)
|
| 138 |
+
top_players = sorted(all_players, key=scores.get, reverse=True)[:pool_size]
|
| 139 |
top_k = get_top(top_players, ex)
|
| 140 |
return ", ".join(top_k)
|
| 141 |
|
|
|
|
| 144 |
inputs=[
|
| 145 |
gr.Textbox(lines=2, label="Instruction"),
|
| 146 |
gr.Textbox(lines=1, label="Criteria (comma separated)"),
|
| 147 |
+
gr.Number(value=NUM_GENERATIONS_DEFAULT, label="Number of Generations"),
|
| 148 |
+
gr.Number(value=NUM_TOP_PICKS_DEFAULT, label="Top Picks (k)"),
|
| 149 |
+
gr.Number(value=POOL_SIZE_DEFAULT, label="Pool Size"),
|
| 150 |
+
gr.Number(value=MAX_WORKERS_DEFAULT, label="Max Workers")
|
| 151 |
],
|
| 152 |
+
outputs=gr.Textbox(label="Top picks")
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|