File size: 3,529 Bytes
1bbca12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import aiohttp
import asyncio
from graph import graph
from state import JARVISState
from pydantic import BaseModel
from typing import List
import json
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
# Debug: Verify environment variables
print(f"OPENAI_API_KEY loaded: {'set' if os.getenv('OPENAI_API_KEY') else 'not set'}")
print(f"LANGFUSE_PUBLIC_KEY loaded: {'set' if os.getenv('LANGFUSE_PUBLIC_KEY') else 'not set'}")

# Verify critical environment variables
required_env_vars = ["OPENAI_API_KEY", "LANGFUSE_PUBLIC_KEY", "LANGFUSE_SECRET_KEY"]
for var in required_env_vars:
    if not os.getenv(var):
        raise ValueError(f"Environment variable {var} is not set")

# Pydantic Models for Submission
class Answer(BaseModel):
    task_id: str
    submitted_answer: str

class Submission(BaseModel):
    username: str
    agent_code: str
    answers: List[Answer]

async def fetch_questions() -> List[dict]:
    async with aiohttp.ClientSession() as session:
        async with session.get("https://api.gaia-benchmark.com/questions") as resp:
            return await resp.json()

async def download_file(task_id: str, file_path: str) -> bool:
    async with aiohttp.ClientSession() as session:
        async with session.get(f"https://api.gaia-benchmark.com/files/{task_id}") as resp:
            if resp.status == 200:
                with open(file_path, "wb") as f:
                    f.write(await resp.read())
                return True
            return False

async def process_question(question: dict) -> Answer:
    # Determine file type based on question context
    file_type = "jpg" if "image" in question["question"].lower() else "txt"
    if "menu" in question["question"].lower() or "report" in question["question"].lower() or "document" in question["question"].lower():
        file_type = "pdf"  # Prioritize PDF for reports/documents
    elif "data" in question["question"].lower():
        file_type = "csv"
    
    file_path = f"temp_{question['task_id']}.{file_type}"
    await download_file(question["task_id"], file_path)
    
    state = JARVISState(
        task_id=question["task_id"],
        question=question["question"],
        tools_needed=[],
        web_results=[],
        file_results="",
        image_results="",
        calculation_results="",
        document_results="",
        messages=[],
        answer=""
    )
    # Use unique thread_id for memory
    result = await graph.ainvoke(state, config={"thread_id": question["task_id"]})
    return Answer(task_id=question["task_id"], submitted_answer=result["answer"])

async def submit_answers(answers: List[Answer], username: str, agent_code: str):
    submission = Submission(
        username=username,
        agent_code=agent_code,
        answers=answers
    )
    async with aiohttp.ClientSession() as session:
        async with session.post("https://api.gaia-benchmark.com/submit", json=submission.dict()) as resp:
            return await resp.json()

async def main():
    username = "onisj"  # Your Hugging Face username
    agent_code = "https://huggingface.co/spaces/onisj/jarvis_gaia_agent/tree/main"
    questions = await fetch_questions()
    answers = []
    for question in questions[:20]:  # Process 20 questions
        answer = await process_question(question)
        answers.append(answer)
    result = await submit_answers(answers, username, agent_code)
    print("Submission result:", json.dumps(result, indent=2))

if __name__ == "__main__":
    asyncio.run(main())