Spaces:
Sleeping
Sleeping
ping98k
commited on
Commit
Β·
2584782
1
Parent(s):
c94e158
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,158 +1,142 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import os
|
| 3 |
-
import random
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 5 |
from tqdm import tqdm
|
| 6 |
-
import
|
| 7 |
-
|
|
|
|
| 8 |
NUM_TOP_PICKS = int(os.getenv("NUM_TOP_PICKS", 5))
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Maximum number of worker threads for parallel execution
|
| 12 |
-
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 10))
|
| 13 |
|
| 14 |
-
# -----------------------------------------------------------------------------
|
| 15 |
-
from litellm import completion
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def prompt_score(player):
|
| 22 |
-
response = completion(
|
| 23 |
-
model="gpt-4o-mini",
|
| 24 |
-
messages=[{"role": "system", "content":
|
| 25 |
-
f"""Evaluate the output below based on the following criteria:
|
| 26 |
-
1) Factuality
|
| 27 |
-
2) Instruction Following
|
| 28 |
-
3) Precision
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
{instruction}
|
| 34 |
|
| 35 |
Output:
|
| 36 |
-
{player}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
a, b
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
ranking = playoff(list(candidates), executor, scores)
|
| 144 |
-
return ranking[:k]
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
-
|
| 148 |
-
all_players = [f"S{i}" for i in range(1, 101)]
|
| 149 |
-
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
|
| 150 |
-
# 1) Compute scores once
|
| 151 |
-
scores = precompute_scores(all_players, executor)
|
| 152 |
-
|
| 153 |
-
# 2) Select top N players by score
|
| 154 |
-
top_n_players = sorted(all_players, key=lambda p: scores[p], reverse=True)[:POOL_SIZE]
|
| 155 |
-
|
| 156 |
-
# 3) Run optimized tournament + playoff using cached scores
|
| 157 |
-
top5 = get_top(top_n_players, executor, scores)
|
| 158 |
-
print("π Top picks:", top5)
|
|
|
|
| 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 |
NUM_TOP_PICKS = int(os.getenv("NUM_TOP_PICKS", 5))
|
| 8 |
+
POOL_SIZE = int(os.getenv("POOL_SIZE", 20))
|
| 9 |
+
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 10))
|
|
|
|
|
|
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def run_tournament(instruction_input, criteria_input):
|
| 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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def prompt_score(player):
|
| 24 |
+
prompt = f"""Evaluate the output below on the following criteria:
|
| 25 |
+
{criteria_block()}
|
| 26 |
|
| 27 |
+
Return JSON exactly like: {{\"score\": [{', '.join(['1-10'] * len(criteria_list))}]}}.
|
| 28 |
+
|
| 29 |
+
Instruction:
|
| 30 |
{instruction}
|
| 31 |
|
| 32 |
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 |
+
|
| 40 |
+
def score(player):
|
| 41 |
+
try:
|
| 42 |
+
data = json.loads(prompt_score(player))
|
| 43 |
+
except json.JSONDecodeError:
|
| 44 |
+
data = eval(prompt_score(player))
|
| 45 |
+
lst = data.get("score", data.get("scores", []))
|
| 46 |
+
return sum(lst) / len(lst) if lst else 0.0
|
| 47 |
+
|
| 48 |
+
def prompt_play(a, b):
|
| 49 |
+
prompt = f"""Compare the two players below using:
|
| 50 |
+
{criteria_block()}
|
| 51 |
+
|
| 52 |
+
Return ONLY JSON {{\"winner\": \"A\"}} or {{\"winner\": \"B\"}}.
|
| 53 |
+
|
| 54 |
+
Instruction:
|
| 55 |
+
{instruction}
|
| 56 |
+
|
| 57 |
+
Players:
|
| 58 |
+
<A>{a}</A>
|
| 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 |
+
|
| 66 |
+
def play(a, b):
|
| 67 |
+
try:
|
| 68 |
+
winner_label = json.loads(prompt_play(a, b))["winner"]
|
| 69 |
+
except json.JSONDecodeError:
|
| 70 |
+
winner_label = eval(prompt_play(a, b)).get("winner", "A")
|
| 71 |
+
return a if winner_label == "A" else b
|
| 72 |
+
|
| 73 |
+
def precompute_scores(players, executor):
|
| 74 |
+
futures = {executor.submit(score, p): p for p in players}
|
| 75 |
+
scores = {}
|
| 76 |
+
for fut in tqdm(as_completed(futures), total=len(futures)):
|
| 77 |
+
scores[futures[fut]] = fut.result()
|
| 78 |
+
return scores
|
| 79 |
+
|
| 80 |
+
def tournament_round(pairs, executor):
|
| 81 |
+
futures = {executor.submit(play, a, b): (a, b) for a, b in pairs}
|
| 82 |
+
results = []
|
| 83 |
+
for fut in tqdm(as_completed(futures), total=len(futures)):
|
| 84 |
+
a, b = futures[fut]
|
| 85 |
+
winner = fut.result()
|
| 86 |
+
loser = b if winner == a else a
|
| 87 |
+
results.append((winner, loser))
|
| 88 |
+
return results
|
| 89 |
+
|
| 90 |
+
def tournament(players, executor):
|
| 91 |
+
lost_to = {}
|
| 92 |
+
current = players[:]
|
| 93 |
+
while len(current) > 1:
|
| 94 |
+
pairs = [(current[i], current[i + 1]) for i in range(0, len(current) - 1, 2)]
|
| 95 |
+
for w, l in tournament_round(pairs, executor):
|
| 96 |
+
lost_to[l] = w
|
| 97 |
+
current = [w for w, _ in tournament_round(pairs, executor)]
|
| 98 |
+
if len(players) % 2 == 1:
|
| 99 |
+
current.append(players[-1])
|
| 100 |
+
return current[0], lost_to
|
| 101 |
+
|
| 102 |
+
def get_candidates(champion, lost_to):
|
| 103 |
+
return [p for p, o in lost_to.items() if o == champion] + [champion]
|
| 104 |
+
|
| 105 |
+
def playoff(candidates, executor):
|
| 106 |
+
wins = {p: 0 for p in candidates}
|
| 107 |
+
pairs = [
|
| 108 |
+
(candidates[i], candidates[j])
|
| 109 |
+
for i in range(len(candidates))
|
| 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, k=NUM_TOP_PICKS):
|
| 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() if o in finalists and p not in finalists]
|
| 122 |
+
candidates = set(finalists + semifinalists + get_candidates(champion, lost_to))
|
| 123 |
+
return playoff(list(candidates), executor)[:k]
|
| 124 |
+
|
| 125 |
+
all_players = [f"S{i}" for i in range(1, 10)]
|
| 126 |
+
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as ex:
|
| 127 |
+
scores = precompute_scores(all_players, ex)
|
| 128 |
+
top_players = sorted(all_players, key=scores.get, reverse=True)[:POOL_SIZE]
|
| 129 |
+
top_k = get_top(top_players, ex)
|
| 130 |
+
return ", ".join(top_k)
|
| 131 |
+
|
| 132 |
+
demo = gr.Interface(
|
| 133 |
+
fn=run_tournament,
|
| 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__":
|
| 142 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|