File size: 3,007 Bytes
cdf8921
 
 
37c9a6b
cdf8921
 
 
 
 
 
 
 
37c9a6b
 
cdf8921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37c9a6b
cdf8921
 
 
37c9a6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdf8921
37c9a6b
cdf8921
 
37c9a6b
cdf8921
 
 
37c9a6b
cdf8921
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
import os
import pandas as pd

from agent.open_ai_agent import OpenAiAgent
from rest_clients.hs_evaluator_client import HsEvaluatorClient


class Evaluator:

    def __init__(self, profile):
        self.profile = profile
        self.username = profile.username if profile else None
        self.space_id = os.getenv("SPACE_ID", "tommaso1288/Final_Assignment_Template")
        self.agent = OpenAiAgent()
        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 = []
        print(f"Running agent on {len(questions)} questions...")
        for item in questions:
            task_id = item.get("task_id")
            question_text = item.get("question")

            # ----------fetch any attached file ----------
            try:
                file_path = self.get_hs_evaluator_client().download_file_if_any(task_id)
            except Exception as e:
                file_path = None
                print(f"[file fetch error] {task_id}: {e}")

            # ---------- Build the prompt sent to the agent ----------
            if file_path:
                q_for_agent = (
                    f"{question_text}\n\n"
                    f"---\n"
                    f"A file was downloaded for this task and saved locally at:\n"
                    f"{file_path}\n"
                    f"---\n\n"
                )
            else:
                q_for_agent = question_text

            if not task_id or question_text is None:
                print(f"Skipping item with missing task_id or question: {item}")
                continue
            try:
                submitted_answer = self.agent(q_for_agent)
                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:
                print(f"Error running agent on task {task_id}: {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