Spaces:
Sleeping
Sleeping
ping98k
commited on
Commit
Β·
c94e158
1
Parent(s):
f92590a
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 4 |
from tqdm import tqdm
|
| 5 |
import time
|
| 6 |
-
|
| 7 |
# Number of top picks to return (default 5)
|
| 8 |
NUM_TOP_PICKS = int(os.getenv("NUM_TOP_PICKS", 5))
|
| 9 |
# Initial pool size after scoring (default 20)
|
|
@@ -12,14 +12,49 @@ POOL_SIZE = int(os.getenv("POOL_SIZE", 20))
|
|
| 12 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 10))
|
| 13 |
|
| 14 |
# -----------------------------------------------------------------------------
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def score(player):
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# play: Determine match winner using precomputed scores.
|
| 22 |
-
# Compare two players' scores in O(1) time without additional delays.
|
| 23 |
def play(a, b, scores):
|
| 24 |
# Return 'a' if its score >= b's score, else 'b'
|
| 25 |
return a if scores[a] >= scores[b] else b
|
|
|
|
| 1 |
+
import json
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 5 |
from tqdm import tqdm
|
| 6 |
import time
|
|
|
|
| 7 |
# Number of top picks to return (default 5)
|
| 8 |
NUM_TOP_PICKS = int(os.getenv("NUM_TOP_PICKS", 5))
|
| 9 |
# Initial pool size after scoring (default 20)
|
|
|
|
| 12 |
MAX_WORKERS = int(os.getenv("MAX_WORKERS", 10))
|
| 13 |
|
| 14 |
# -----------------------------------------------------------------------------
|
| 15 |
+
from litellm import completion
|
| 16 |
+
|
| 17 |
+
instruction = "tell me story"
|
| 18 |
+
|
| 19 |
+
# prompt_score: send player to 4o-mini and return raw response content
|
| 20 |
+
# expects model returns something like '{"score": [4,5,6]}'
|
| 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 |
+
Return a JSON object in this format: {{"score": [1β10, 1β10, 1β10]}} β one score for each criterion.
|
| 31 |
+
|
| 32 |
+
Here is the instruction:
|
| 33 |
+
{instruction}
|
| 34 |
+
|
| 35 |
+
Output:
|
| 36 |
+
{player}
|
| 37 |
+
""".split()
|
| 38 |
+
}]
|
| 39 |
+
)
|
| 40 |
+
# extract message text content
|
| 41 |
+
return response.choices[0].message.content
|
| 42 |
+
|
| 43 |
+
# score: Call Litellm (OpenAI 4o-mini) to get a list of scores and return their average.
|
| 44 |
+
# Parses JSON with key "score" or "scores" and computes average.
|
| 45 |
def score(player):
|
| 46 |
+
response = prompt_score(player)
|
| 47 |
+
try:
|
| 48 |
+
data = json.loads(response)
|
| 49 |
+
scores_list = data.get("score", data.get("scores", []))
|
| 50 |
+
except (json.JSONDecodeError, NameError):
|
| 51 |
+
# Fallback: eval in safe context
|
| 52 |
+
data = eval(response)
|
| 53 |
+
scores_list = data.get("score", data.get("scores", []))
|
| 54 |
+
if not scores_list:
|
| 55 |
+
return 0.0
|
| 56 |
+
return sum(scores_list) / len(scores_list)
|
| 57 |
|
|
|
|
|
|
|
| 58 |
def play(a, b, scores):
|
| 59 |
# Return 'a' if its score >= b's score, else 'b'
|
| 60 |
return a if scores[a] >= scores[b] else b
|