File size: 2,201 Bytes
615d1b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import pandas as pd
from tqdm import tqdm

class EvaluationRunner:
    API_URL = "https://agents-course-unit4-scoring.hf.space"

    def run_evaluation(self, agent, username: str, agent_code: str):
        """Полный цикл оценки"""
        questions = self._fetch_questions()
        if isinstance(questions, str):
            return questions, 0, 0, None

        results = []
        answers = []
        
        for q in tqdm(questions, desc="Processing"):
            try:
                response = agent(q["question"], q["task_id"])
                answer = response.get("final_answer", "")
                answers.append({
                    "task_id": q["task_id"],
                    "submitted_answer": str(answer)[:500]  # Лимит длины
                })
                results.append({
                    "Question": q["question"][:100],
                    "Your Answer": str(answer)[:100],
                    "Status": "Processed"
                })
            except Exception as e:
                results.append({
                    "Question": q["question"][:100],
                    "Your Answer": f"Error: {str(e)}",
                    "Status": "Failed"
                })

        submission_result = self._submit_answers(username, agent_code, answers)
        return submission_result, 0, len(questions), pd.DataFrame(results)

    def _fetch_questions(self):
        try:
            response = requests.get(f"{self.API_URL}/questions", timeout=30)
            return response.json()
        except Exception as e:
            return f"Failed to fetch questions: {str(e)}"

    def _submit_answers(self, username: str, agent_code: str, answers: list):
        try:
            response = requests.post(
                f"{self.API_URL}/submit",
                json={
                    "username": username.strip(),
                    "agent_code": agent_code.strip(),
                    "answers": answers
                },
                timeout=60
            )
            return response.json().get("message", "Submitted successfully")
        except Exception as e:
            return f"Submission failed: {str(e)}"