Added a user-specific answer cache
Browse files- evaluator.py +4 -6
- runner.py +8 -7
evaluator.py
CHANGED
@@ -57,14 +57,11 @@ class Evaluator():
|
|
57 |
questions = self.get_questions()
|
58 |
return questions[random.randint(0, len(questions)-1)]
|
59 |
|
60 |
-
def _read_answer_file(self,
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
with open("answers.json", "r") as f:
|
65 |
pairs = [QuestionAnswerPair(**pair) for pair in json.load(f)]
|
66 |
formatted_data = [pair.get_answer() for pair in pairs]
|
67 |
-
# Return count and the formatted data
|
68 |
return formatted_data
|
69 |
|
70 |
def submit_answers(self, username: str) -> str:
|
@@ -73,6 +70,7 @@ class Evaluator():
|
|
73 |
answers_payload = self._read_answer_file({username})
|
74 |
except FileNotFoundError:
|
75 |
return "Click 'Get One Answer' or 'Get All Answers' to run before trying to submit."
|
|
|
76 |
agent_code = f"https://huggingface.co/spaces/{self.settings.space_id}/tree/main"
|
77 |
submission_data = {
|
78 |
"username": self.settings.username,
|
|
|
57 |
questions = self.get_questions()
|
58 |
return questions[random.randint(0, len(questions)-1)]
|
59 |
|
60 |
+
def _read_answer_file(self, username) -> List[str]:
|
61 |
+
"""Read the question answer pairs from a user-specific answer file."""
|
62 |
+
with open(f"answers_{username}.json", "r") as f:
|
|
|
|
|
63 |
pairs = [QuestionAnswerPair(**pair) for pair in json.load(f)]
|
64 |
formatted_data = [pair.get_answer() for pair in pairs]
|
|
|
65 |
return formatted_data
|
66 |
|
67 |
def submit_answers(self, username: str) -> str:
|
|
|
70 |
answers_payload = self._read_answer_file({username})
|
71 |
except FileNotFoundError:
|
72 |
return "Click 'Get One Answer' or 'Get All Answers' to run before trying to submit."
|
73 |
+
|
74 |
agent_code = f"https://huggingface.co/spaces/{self.settings.space_id}/tree/main"
|
75 |
submission_data = {
|
76 |
"username": self.settings.username,
|
runner.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from settings import Settings
|
2 |
-
from models import QuestionAnswerPair
|
3 |
from agent import BasicAgent
|
4 |
import pandas as pd
|
5 |
import logging
|
@@ -14,9 +14,10 @@ class Runner():
|
|
14 |
def __init__(self, settings: Settings):
|
15 |
self.settings = settings
|
16 |
|
17 |
-
def _save_pairs(self, pairs: list[QuestionAnswerPair]):
|
|
|
18 |
answers = [pair.model_dump() for pair in pairs if pair is not None]
|
19 |
-
with open("
|
20 |
json.dump(answers, f, indent=4)
|
21 |
|
22 |
def _enrich_question_text(self, item):
|
@@ -33,7 +34,7 @@ class Runner():
|
|
33 |
question_text = f"{question_text} file_name: {file_name} (use tools to fetch the file)"
|
34 |
return question_text
|
35 |
|
36 |
-
async def _run_agent_async(self, item):
|
37 |
"""Runs the agent asynchronously."""
|
38 |
task_id = item.task_id
|
39 |
question_text = self._enrich_question_text(item)
|
@@ -45,12 +46,12 @@ class Runner():
|
|
45 |
return QuestionAnswerPair(task_id=task_id,
|
46 |
question=item.question, answer=str(answer))
|
47 |
|
48 |
-
def _assign_questions(self, questions):
|
49 |
"""Runs the asynchronous loop and returns task outputs."""
|
50 |
tasks = [self._run_agent_async(item) for item in questions]
|
51 |
return asyncio.gather(*tasks)
|
52 |
|
53 |
-
def run_agent(self, questions) -> pd.DataFrame:
|
54 |
"""Run the agent(s) async, save answers and return a dataframe"""
|
55 |
# Assign questions to agents and wait
|
56 |
try:
|
@@ -67,7 +68,7 @@ class Runner():
|
|
67 |
pairs = run_tasks_in_thread()
|
68 |
|
69 |
# save json to disk and return a dataframe
|
70 |
-
self._save_pairs(pairs)
|
71 |
results_log = [pair.model_dump() for pair in pairs if pair is not None]
|
72 |
if not results_log:
|
73 |
logger.warning("Agent did not produce any answers to submit.")
|
|
|
1 |
from settings import Settings
|
2 |
+
from models import Question, QuestionAnswerPair
|
3 |
from agent import BasicAgent
|
4 |
import pandas as pd
|
5 |
import logging
|
|
|
14 |
def __init__(self, settings: Settings):
|
15 |
self.settings = settings
|
16 |
|
17 |
+
def _save_pairs(self, pairs: list[QuestionAnswerPair], username: str):
|
18 |
+
"""Write the question answer pairs to a user-specific file."""
|
19 |
answers = [pair.model_dump() for pair in pairs if pair is not None]
|
20 |
+
with open(f"answers_{username}.json", "w") as f:
|
21 |
json.dump(answers, f, indent=4)
|
22 |
|
23 |
def _enrich_question_text(self, item):
|
|
|
34 |
question_text = f"{question_text} file_name: {file_name} (use tools to fetch the file)"
|
35 |
return question_text
|
36 |
|
37 |
+
async def _run_agent_async(self, item: Question):
|
38 |
"""Runs the agent asynchronously."""
|
39 |
task_id = item.task_id
|
40 |
question_text = self._enrich_question_text(item)
|
|
|
46 |
return QuestionAnswerPair(task_id=task_id,
|
47 |
question=item.question, answer=str(answer))
|
48 |
|
49 |
+
def _assign_questions(self, questions: list[Question]):
|
50 |
"""Runs the asynchronous loop and returns task outputs."""
|
51 |
tasks = [self._run_agent_async(item) for item in questions]
|
52 |
return asyncio.gather(*tasks)
|
53 |
|
54 |
+
def run_agent(self, questions: list[Question], username: str) -> pd.DataFrame:
|
55 |
"""Run the agent(s) async, save answers and return a dataframe"""
|
56 |
# Assign questions to agents and wait
|
57 |
try:
|
|
|
68 |
pairs = run_tasks_in_thread()
|
69 |
|
70 |
# save json to disk and return a dataframe
|
71 |
+
self._save_pairs(pairs, username)
|
72 |
results_log = [pair.model_dump() for pair in pairs if pair is not None]
|
73 |
if not results_log:
|
74 |
logger.warning("Agent did not produce any answers to submit.")
|