Spaces:
Sleeping
Sleeping
Complete GAIA agent implementation with local LLM
Browse files- agent/__init__.py +0 -0
- agent/local_llm.py +32 -0
- agent/tools.py +17 -0
- app.py +55 -0
- packages.txt +2 -0
- requirements.txt +9 -0
- utils/gaia_api.py +31 -0
agent/__init__.py
ADDED
File without changes
|
agent/local_llm.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
+
import torch
|
3 |
+
|
4 |
+
class LocalLLM:
|
5 |
+
def __init__(self):
|
6 |
+
self.model_name = "HuggingFaceH4/zephyr-7b-beta"
|
7 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
self.pipeline = self._load_model()
|
9 |
+
|
10 |
+
def _load_model(self):
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(self.model_name)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
self.model_name,
|
14 |
+
torch_dtype=torch.float16,
|
15 |
+
device_map="auto",
|
16 |
+
load_in_4bit=True # Quantization to save memory
|
17 |
+
)
|
18 |
+
return pipeline(
|
19 |
+
"text-generation",
|
20 |
+
model=model,
|
21 |
+
tokenizer=tokenizer,
|
22 |
+
device=self.device
|
23 |
+
)
|
24 |
+
|
25 |
+
def generate(self, prompt: str) -> str:
|
26 |
+
outputs = self.pipeline(
|
27 |
+
prompt,
|
28 |
+
max_new_tokens=256,
|
29 |
+
do_sample=True,
|
30 |
+
temperature=0.7
|
31 |
+
)
|
32 |
+
return outputs[0]['generated_text']
|
agent/tools.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from llama_index.core.tools import FunctionTool
|
2 |
+
from utils.gaia_api import GaiaAPI
|
3 |
+
|
4 |
+
def get_gaia_questions() -> str:
|
5 |
+
"""Fetch all GAIA benchmark questions"""
|
6 |
+
questions = GaiaAPI.get_questions()
|
7 |
+
return "\n".join([f"{q['task_id']}: {q['question']}" for q in questions])
|
8 |
+
|
9 |
+
def get_random_question() -> str:
|
10 |
+
"""Get a single random GAIA question"""
|
11 |
+
question = GaiaAPI.get_random_question()
|
12 |
+
return f"{question['task_id']}: {question['question']}"
|
13 |
+
|
14 |
+
gaia_tools = [
|
15 |
+
FunctionTool.from_defaults(fn=get_gaia_questions),
|
16 |
+
FunctionTool.from_defaults(fn=get_random_question)
|
17 |
+
]
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from agent.local_llm import LocalLLM
|
3 |
+
from agent.tools import gaia_tools
|
4 |
+
from llama_index.core.agent import ReActAgent
|
5 |
+
from utils.gaia_api import GaiaAPI
|
6 |
+
|
7 |
+
# Initialize components
|
8 |
+
llm = LocalLLM()
|
9 |
+
agent = ReActAgent.from_tools(gaia_tools, llm=llm.pipeline)
|
10 |
+
|
11 |
+
def process_question(question_text: str) -> str:
|
12 |
+
"""Process GAIA question through agent"""
|
13 |
+
try:
|
14 |
+
response = agent.query(question_text)
|
15 |
+
return str(response)
|
16 |
+
except Exception as e:
|
17 |
+
return f"Error: {str(e)}"
|
18 |
+
|
19 |
+
def submit_to_gaia(username: str, code_url: str) -> str:
|
20 |
+
"""Submit all answers to GAIA"""
|
21 |
+
try:
|
22 |
+
questions = GaiaAPI.get_questions()
|
23 |
+
answers = []
|
24 |
+
for q in questions:
|
25 |
+
answer = process_question(q['question'])
|
26 |
+
answers.append({
|
27 |
+
"task_id": q['task_id'],
|
28 |
+
"submitted_answer": answer
|
29 |
+
})
|
30 |
+
result = GaiaAPI.submit_answers(username, code_url, answers)
|
31 |
+
return f"Submitted! Score: {result.get('score', 'N/A')}"
|
32 |
+
except Exception as e:
|
33 |
+
return f"Submission failed: {str(e)}"
|
34 |
+
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# GAIA Benchmark Agent")
|
37 |
+
|
38 |
+
with gr.Tab("Question Processing"):
|
39 |
+
question_input = gr.Textbox(label="Enter GAIA Question")
|
40 |
+
answer_output = gr.Textbox(label="Agent Answer")
|
41 |
+
process_btn = gr.Button("Process Question")
|
42 |
+
process_btn.click(process_question, inputs=question_input, outputs=answer_output)
|
43 |
+
|
44 |
+
with gr.Tab("GAIA Submission"):
|
45 |
+
username_input = gr.Textbox(label="HF Username")
|
46 |
+
code_url_input = gr.Textbox(label="Space Code URL")
|
47 |
+
submit_btn = gr.Button("Submit to GAIA")
|
48 |
+
submission_output = gr.Textbox(label="Submission Result")
|
49 |
+
submit_btn.click(
|
50 |
+
submit_to_gaia,
|
51 |
+
inputs=[username_input, code_url_input],
|
52 |
+
outputs=submission_output
|
53 |
+
)
|
54 |
+
|
55 |
+
demo.launch()
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
libgl1
|
2 |
+
libglib2.0-0
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
llama-index==0.10.0
|
2 |
+
transformers==4.34.0
|
3 |
+
torch==2.0.1
|
4 |
+
accelerate==0.23.0
|
5 |
+
sentence-transformers==2.2.2
|
6 |
+
python-dotenv==1.0.0
|
7 |
+
gradio==3.41.0
|
8 |
+
requests==2.31.0
|
9 |
+
bitsandbytes==0.41.1
|
utils/gaia_api.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from typing import List, Dict, Optional
|
3 |
+
|
4 |
+
class GaiaAPI:
|
5 |
+
BASE_URL = "https://https://agents-course-unit4-scoring.hf.space/docs" # Actual GAIA API URL
|
6 |
+
|
7 |
+
@classmethod
|
8 |
+
def get_questions(cls) -> List[Dict]:
|
9 |
+
"""Fetch all questions from GAIA"""
|
10 |
+
response = requests.get(f"{cls.BASE_URL}/questions")
|
11 |
+
response.raise_for_status()
|
12 |
+
return response.json()
|
13 |
+
|
14 |
+
@classmethod
|
15 |
+
def get_random_question(cls) -> Dict:
|
16 |
+
"""Get single random question"""
|
17 |
+
response = requests.get(f"{cls.BASE_URL}/random-question")
|
18 |
+
response.raise_for_status()
|
19 |
+
return response.json()
|
20 |
+
|
21 |
+
@classmethod
|
22 |
+
def submit_answers(cls, username: str, code_url: str, answers: List[Dict]) -> Dict:
|
23 |
+
"""Submit answers to GAIA"""
|
24 |
+
payload = {
|
25 |
+
"username": username,
|
26 |
+
"agent_code": code_url,
|
27 |
+
"answers": answers
|
28 |
+
}
|
29 |
+
response = requests.post(f"{cls.BASE_URL}/submit", json=payload)
|
30 |
+
response.raise_for_status()
|
31 |
+
return response.json()
|