tommaso1288's picture
First commit - code refactoring
cdf8921
raw
history blame
2.02 kB
import os
import pandas as pd
from rest_clients.hs_evaluator_client import HsEvaluatorClient
from src.agent.base_agent import BasicAgent
class Evaluator:
def __init__(self, profile):
self.profile = profile
self.username = profile.username if profile else None
self.space_id = os.getenv("SPACE_ID")
self.agent = BasicAgent()
self.hs_evaluator_client: HsEvaluatorClient | None = None
def run_and_submit(self):
if not self.username:
return "Please Login to Hugging Face with the button.", None
questions = self.get_hs_evaluator_client().fetch_questions()
if not questions:
return "Fetched questions list is empty or invalid format.", None
results_log, answers_payload = self._run_agent(questions)
if not answers_payload:
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
return self.get_hs_evaluator_client().submit_answers(answers_payload, results_log)
def _run_agent(self, questions):
results_log = []
answers_payload = []
for item in questions:
task_id = item.get("task_id")
question_text = item.get("question")
if not task_id or question_text is None:
continue
try:
submitted_answer = self.agent(question_text)
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
except Exception as e:
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
return results_log, answers_payload
def get_hs_evaluator_client(self):
if not self.hs_evaluator_client:
self.hs_evaluator_client = HsEvaluatorClient(self.username, self.space_id)
return self.hs_evaluator_client