Commit ·
980a2ce
1
Parent(s): 81917a3
files
Browse files- .gitignore +1 -0
- README.md +2 -2
- agent.py +29 -0
- app.py +4 -14
- requirements.txt +1 -1
- tools.py +75 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 🕵🏻♂️
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: indigo
|
|
@@ -12,4 +12,4 @@ hf_oauth: true
|
|
| 12 |
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
|
| 15 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Final Assignment
|
| 3 |
emoji: 🕵🏻♂️
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: indigo
|
|
|
|
| 12 |
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
|
| 15 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
agent.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, WikipediaSearchTool
|
| 3 |
+
from tools import VisitWebpageTool, DownloadTaskAttachmentTool
|
| 4 |
+
|
| 5 |
+
class TheAgent:
|
| 6 |
+
"""Agent that processes questions using LLM and various tools."""
|
| 7 |
+
|
| 8 |
+
def __init__(self):
|
| 9 |
+
# Initialize the agent with model and tools
|
| 10 |
+
self.agent = CodeAgent(
|
| 11 |
+
model=LiteLLMModel(
|
| 12 |
+
model_id="openrouter/meta-llama/llama-4-maverick:free",
|
| 13 |
+
api_key=os.getenv("OPENROUTER_KEY")
|
| 14 |
+
),
|
| 15 |
+
tools=[
|
| 16 |
+
DuckDuckGoSearchTool(),
|
| 17 |
+
WikipediaSearchTool(),
|
| 18 |
+
VisitWebpageTool(),
|
| 19 |
+
DownloadTaskAttachmentTool()
|
| 20 |
+
],
|
| 21 |
+
add_base_tools=True,
|
| 22 |
+
additional_authorized_imports=['pandas', 'numpy', 'csv', 'subprocess', 'exec']
|
| 23 |
+
)
|
| 24 |
+
print("Agent initialized successfully.")
|
| 25 |
+
|
| 26 |
+
def __call__(self, question: str) -> str:
|
| 27 |
+
"""Process a question and return the answer."""
|
| 28 |
+
print(f"Processing question: {question[:50]}..." if len(question) > 50 else f"Processing question: {question}")
|
| 29 |
+
return self.agent.run(question)
|
app.py
CHANGED
|
@@ -1,24 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
| 21 |
-
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -40,7 +30,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -193,4 +183,4 @@ if __name__ == "__main__":
|
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from agent import TheAgent
|
| 7 |
+
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 13 |
"""
|
| 14 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 30 |
|
| 31 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 32 |
try:
|
| 33 |
+
agent = TheAgent()
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error instantiating agent: {e}")
|
| 36 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 183 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 184 |
|
| 185 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 186 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
tools.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
from smolagents import Tool
|
| 5 |
+
from smolagents.utils import truncate_content
|
| 6 |
+
|
| 7 |
+
# Define constant for API URL
|
| 8 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
+
|
| 10 |
+
class DownloadTaskAttachmentTool(Tool):
|
| 11 |
+
"""Tool for downloading files attached to tasks."""
|
| 12 |
+
|
| 13 |
+
name = "download_file"
|
| 14 |
+
description = "Downloads the file attached to the task ID"
|
| 15 |
+
inputs = {'task_id': {'type': 'string', 'description': 'The task id to download attachment from.'}}
|
| 16 |
+
output_type = "string"
|
| 17 |
+
|
| 18 |
+
def __init__(self, *args, **kwargs):
|
| 19 |
+
self.is_initialized = True
|
| 20 |
+
|
| 21 |
+
def forward(self, task_id: str) -> str:
|
| 22 |
+
"""Download a file associated with the given task ID."""
|
| 23 |
+
file_url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 24 |
+
local_file_path = f"downloads/{task_id}.file"
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Create downloads directory if it doesn't exist
|
| 28 |
+
os.makedirs("downloads", exist_ok=True)
|
| 29 |
+
|
| 30 |
+
# Download the file
|
| 31 |
+
with requests.get(file_url, stream=True, timeout=15) as response:
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
with open(local_file_path, "wb") as file:
|
| 34 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 35 |
+
file.write(chunk)
|
| 36 |
+
|
| 37 |
+
return local_file_path
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Error downloading file for task {task_id}: {e}")
|
| 41 |
+
raise
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class VisitWebpageTool(Tool):
|
| 45 |
+
"""Tool for visiting webpages and converting content to markdown."""
|
| 46 |
+
|
| 47 |
+
name = "visit_webpage"
|
| 48 |
+
description = "Visits a webpage and returns its content as markdown"
|
| 49 |
+
inputs = {'url': {'type': 'string', 'description': 'The URL to visit'}}
|
| 50 |
+
output_type = "string"
|
| 51 |
+
|
| 52 |
+
def __init__(self, *args, **kwargs):
|
| 53 |
+
self.is_initialized = True
|
| 54 |
+
|
| 55 |
+
def forward(self, url: str) -> str:
|
| 56 |
+
"""Visit a webpage and return its content as markdown."""
|
| 57 |
+
try:
|
| 58 |
+
# Import here to avoid dependency issues
|
| 59 |
+
from markdownify import markdownify
|
| 60 |
+
|
| 61 |
+
# Fetch webpage content
|
| 62 |
+
response = requests.get(url, timeout=20)
|
| 63 |
+
response.raise_for_status()
|
| 64 |
+
|
| 65 |
+
# Convert HTML to markdown and clean up
|
| 66 |
+
content = markdownify(response.text).strip()
|
| 67 |
+
content = re.sub(r"\n{3,}", "\n\n", content)
|
| 68 |
+
|
| 69 |
+
# Truncate to avoid excessively long responses
|
| 70 |
+
return truncate_content(content, 10000)
|
| 71 |
+
|
| 72 |
+
except requests.exceptions.Timeout:
|
| 73 |
+
return "Request timed out. Please check the URL or try again later."
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error: {str(e)}"
|