Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Course agent: tools generate answers; no benchmark answer lookup tables."""
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from smolagents import ToolCallingAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool, tool
|
| 5 |
+
|
| 6 |
+
@tool
|
| 7 |
+
def guest_search(query: str) -> str:
|
| 8 |
+
"""Retrieve relevant public gala guest records for the Unit 3 use case.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
query: Guest name or topic to search for.
|
| 12 |
+
"""
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
records = load_dataset('agents-course/unit3-invitees', split='train')
|
| 15 |
+
terms = set(query.lower().split())
|
| 16 |
+
ranked = sorted(records, key=lambda r: len(terms & set(str(r).lower().split())), reverse=True)
|
| 17 |
+
return str(ranked[:3])
|
| 18 |
+
|
| 19 |
+
def build_agent(use_case=False):
|
| 20 |
+
token = os.environ.get('HF_TOKEN')
|
| 21 |
+
if not token:
|
| 22 |
+
raise RuntimeError('Set HF_TOKEN as a secret with inference permission first.')
|
| 23 |
+
model = InferenceClientModel(token=token, model_id=os.environ.get('MODEL_ID', 'Qwen/Qwen2.5-Coder-32B-Instruct'))
|
| 24 |
+
tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
|
| 25 |
+
if use_case:
|
| 26 |
+
tools.append(guest_search)
|
| 27 |
+
return ToolCallingAgent(tools=tools, model=model, max_steps=12)
|
| 28 |
+
|
| 29 |
+
def answer(agent, question):
|
| 30 |
+
return str(agent.run(question + '\nReturn only the exact answer requested, without a FINAL ANSWER prefix or explanation.', reset=True)).strip()
|
| 31 |
+
|
| 32 |
+
def evaluate(output_path='answers.json'):
|
| 33 |
+
import json
|
| 34 |
+
questions = requests.get('https://agents-course-unit4-scoring.hf.space/questions', timeout=60)
|
| 35 |
+
questions.raise_for_status()
|
| 36 |
+
agent = build_agent()
|
| 37 |
+
answers, failures = [], []
|
| 38 |
+
for item in questions.json():
|
| 39 |
+
if item.get('file_name'):
|
| 40 |
+
failures.append({'task_id': item['task_id'], 'reason': 'Attachment processing not implemented; do not claim this task is supported.'})
|
| 41 |
+
continue
|
| 42 |
+
try:
|
| 43 |
+
answers.append({'task_id': item['task_id'], 'submitted_answer': answer(agent, item['question'])})
|
| 44 |
+
except Exception as exc:
|
| 45 |
+
failures.append({'task_id': item['task_id'], 'reason': type(exc).__name__})
|
| 46 |
+
with open(output_path, 'w', encoding='utf-8') as handle:
|
| 47 |
+
json.dump({'answers': answers, 'failures': failures}, handle, indent=2)
|
| 48 |
+
return answers, failures
|
| 49 |
+
|
| 50 |
+
if __name__ == '__main__':
|
| 51 |
+
evaluate()
|