Spaces:
Sleeping
Sleeping
File size: 3,263 Bytes
10e9b7d 5741c86 eccf8e4 5741c86 10e9b7d 31243f4 be2ae86 a999c9e be2ae86 a999c9e 9372910 4bdd0ed 9372910 4dca678 5741c86 60b70e3 e6f052c 5741c86 13d85d8 be2ae86 13d85d8 be2ae86 5741c86 13d85d8 5741c86 13d85d8 5741c86 be2ae86 13d85d8 5741c86 be2ae86 13d85d8 be2ae86 13d85d8 be2ae86 5741c86 be2ae86 5741c86 be2ae86 5741c86 be2ae86 5741c86 | 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import os
import gradio as gr
import requests
import pandas as pd
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class BasicAgent:
def __init__(self):
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("HF_TOKEN missing")
self.headers = {
"Authorization": f"Bearer {hf_token}"
}
self.api_url = "https://router.huggingface.co/v1/chat/completions"
self.model = "meta-llama/Llama-3.1-8B-Instruct"
print("Agent Ready")
def __call__(self, question: str):
prompt = f"""
You are a GAIA solving agent.
Rules:
- Think step by step internally
- Give ONLY final answer
- No explanation
- No extra words
Question:
{question}
Final Answer:
"""
payload = {
"model": self.model,
"messages": [
{"role": "user", "content": prompt}
],
"temperature": 0
}
try:
r = requests.post(
self.api_url,
headers=self.headers,
json=payload,
timeout=120
)
r.raise_for_status()
result = r.json()
return result["choices"][0]["message"]["content"].strip()
except Exception as e:
print("Error:", e)
return "ERROR"
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if not profile:
return "Login required", None
username = profile.username
questions_url = f"{DEFAULT_API_URL}/questions"
submit_url = f"{DEFAULT_API_URL}/submit"
agent = BasicAgent()
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
try:
r = requests.get(questions_url, timeout=30)
r.raise_for_status()
questions = r.json()
except Exception as e:
return f"Fetch error: {e}", None
answers = []
logs = []
for item in questions:
task_id = item.get("task_id")
question = item.get("question")
if not task_id or not question:
continue
ans = agent(question)
answers.append({
"task_id": task_id,
"submitted_answer": ans
})
logs.append({
"Task ID": task_id,
"Question": question,
"Answer": ans
})
payload = {
"username": username,
"agent_code": agent_code,
"answers": answers
}
try:
r = requests.post(submit_url, json=payload, timeout=120)
r.raise_for_status()
data = r.json()
return (
f"Score: {data.get('score')}%\n"
f"Correct: {data.get('correct_count')}/{data.get('total_attempted')}\n"
f"{data.get('message')}",
pd.DataFrame(logs)
)
except Exception as e:
return f"Submit error: {e}", pd.DataFrame(logs)
with gr.Blocks() as demo:
gr.Markdown("# GAIA Agent")
gr.LoginButton()
btn = gr.Button("Run & Submit")
out = gr.Textbox(label="Result", lines=6)
table = gr.DataFrame(label="Logs")
btn.click(run_and_submit_all, outputs=[out, table])
if __name__ == "__main__":
demo.launch() |