Update app.py
Browse files
app.py
CHANGED
|
@@ -12,40 +12,64 @@ from tools.final_answer import FinalAnswerTool
|
|
| 12 |
from tools.web_search import DuckDuckGoSearchTool
|
| 13 |
|
| 14 |
# -----------------------------------------------------------------------------
|
| 15 |
-
# 1) YOUR CUSTOM TOOLS
|
| 16 |
# -----------------------------------------------------------------------------
|
| 17 |
@tool
|
| 18 |
def get_weather(city: str) -> str:
|
| 19 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
resp = requests.get(f"https://wttr.in/{city}?format=3")
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
return f"Error: {e}"
|
| 26 |
|
| 27 |
@tool
|
| 28 |
def generate_pun(topic: str) -> str:
|
| 29 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
puns = {
|
| 31 |
-
"banana":
|
| 32 |
-
"fish":
|
| 33 |
"computer": "My computer wanted to dance, so I gave it the disk-o.",
|
| 34 |
"coffee": "I’ve bean thinking about you!",
|
| 35 |
"time": "I wanted to be a watchmaker, but I just didn’t have the time.",
|
| 36 |
"weather": "I tried to catch some fog... but I mist.",
|
| 37 |
}
|
| 38 |
-
return puns.get(
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@tool
|
| 42 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 43 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
return "What magic will you build?"
|
| 45 |
|
| 46 |
@tool
|
| 47 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 48 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
tz = pytz.timezone(timezone)
|
| 51 |
now = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -54,7 +78,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 54 |
return f"Error fetching time for timezone '{timezone}': {e}"
|
| 55 |
|
| 56 |
# -----------------------------------------------------------------------------
|
| 57 |
-
# 2) LOAD PROMPTS & INSTANTIATE YOUR AGENT
|
| 58 |
# -----------------------------------------------------------------------------
|
| 59 |
with open("prompts.yaml", "r") as f:
|
| 60 |
prompt_templates = yaml.safe_load(f)
|
|
@@ -92,7 +116,7 @@ class BasicAgent:
|
|
| 92 |
return self.agent.run(question)
|
| 93 |
|
| 94 |
# -----------------------------------------------------------------------------
|
| 95 |
-
# 3) EVALUATION LOGIC
|
| 96 |
# -----------------------------------------------------------------------------
|
| 97 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 98 |
|
|
@@ -104,7 +128,6 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
|
| 104 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 105 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 106 |
|
| 107 |
-
# fetch questions
|
| 108 |
try:
|
| 109 |
r = requests.get(questions_url, timeout=15)
|
| 110 |
r.raise_for_status()
|
|
@@ -115,12 +138,12 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
|
| 115 |
if not questions:
|
| 116 |
return "No questions returned.", None
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
logs = []
|
| 121 |
answers = []
|
| 122 |
for item in questions:
|
| 123 |
-
tid = item.get("task_id")
|
|
|
|
| 124 |
if not tid or not q:
|
| 125 |
continue
|
| 126 |
ans = agent(q)
|
|
@@ -130,16 +153,15 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
|
| 130 |
if not answers:
|
| 131 |
return "Agent produced no answers.", pd.DataFrame(logs)
|
| 132 |
|
| 133 |
-
# submit
|
| 134 |
try:
|
| 135 |
payload = {
|
| 136 |
-
"username":
|
| 137 |
-
"agent_code":
|
| 138 |
-
"answers":
|
| 139 |
}
|
| 140 |
r2 = requests.post(submit_url, json=payload, timeout=60)
|
| 141 |
r2.raise_for_status()
|
| 142 |
-
res
|
| 143 |
status = (
|
| 144 |
f"✔ Submission Successful!\n"
|
| 145 |
f"User: {res.get('username')}\n"
|
|
@@ -153,7 +175,7 @@ def run_and_submit_all(profile: Optional[gr.OAuthProfile]):
|
|
| 153 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
| 154 |
|
| 155 |
# -----------------------------------------------------------------------------
|
| 156 |
-
# 4) GRADIO EVALUATION UI
|
| 157 |
# -----------------------------------------------------------------------------
|
| 158 |
with gr.Blocks() as demo:
|
| 159 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
|
| 12 |
from tools.web_search import DuckDuckGoSearchTool
|
| 13 |
|
| 14 |
# -----------------------------------------------------------------------------
|
| 15 |
+
# 1) YOUR CUSTOM TOOLS (with proper docstrings for Args)
|
| 16 |
# -----------------------------------------------------------------------------
|
| 17 |
@tool
|
| 18 |
def get_weather(city: str) -> str:
|
| 19 |
+
"""
|
| 20 |
+
Fetches current weather info for a given city using wttr.in.
|
| 21 |
+
|
| 22 |
+
Args:
|
| 23 |
+
city: Name of the city to get the weather for.
|
| 24 |
+
"""
|
| 25 |
try:
|
| 26 |
resp = requests.get(f"https://wttr.in/{city}?format=3")
|
| 27 |
+
if resp.ok:
|
| 28 |
+
return f"The current weather in {city} is: {resp.text}"
|
| 29 |
+
return f"Failed fetching weather for {city}."
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error: {e}"
|
| 32 |
|
| 33 |
@tool
|
| 34 |
def generate_pun(topic: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Generates a spontaneous pun based on a given topic.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
topic: The subject to base the pun on.
|
| 40 |
+
"""
|
| 41 |
puns = {
|
| 42 |
+
"banana": "I find bananas very *a-peeling*.",
|
| 43 |
+
"fish": "You cod not be more fin-tastic!",
|
| 44 |
"computer": "My computer wanted to dance, so I gave it the disk-o.",
|
| 45 |
"coffee": "I’ve bean thinking about you!",
|
| 46 |
"time": "I wanted to be a watchmaker, but I just didn’t have the time.",
|
| 47 |
"weather": "I tried to catch some fog... but I mist.",
|
| 48 |
}
|
| 49 |
+
return puns.get(
|
| 50 |
+
topic.lower(),
|
| 51 |
+
f"I don't have a pun for '{topic}' yet, but I’ll *try* not to make a joke of it!"
|
| 52 |
+
)
|
| 53 |
|
| 54 |
@tool
|
| 55 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 56 |
+
"""
|
| 57 |
+
A placeholder tool you can flesh out later.
|
| 58 |
+
|
| 59 |
+
Args:
|
| 60 |
+
arg1: The first argument.
|
| 61 |
+
arg2: The second argument.
|
| 62 |
+
"""
|
| 63 |
return "What magic will you build?"
|
| 64 |
|
| 65 |
@tool
|
| 66 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 67 |
+
"""
|
| 68 |
+
Fetches the current local time in a specified timezone.
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 72 |
+
"""
|
| 73 |
try:
|
| 74 |
tz = pytz.timezone(timezone)
|
| 75 |
now = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 78 |
return f"Error fetching time for timezone '{timezone}': {e}"
|
| 79 |
|
| 80 |
# -----------------------------------------------------------------------------
|
| 81 |
+
# 2) LOAD PROMPTS & INSTANTIATE YOUR AGENT (unchanged)
|
| 82 |
# -----------------------------------------------------------------------------
|
| 83 |
with open("prompts.yaml", "r") as f:
|
| 84 |
prompt_templates = yaml.safe_load(f)
|
|
|
|
| 116 |
return self.agent.run(question)
|
| 117 |
|
| 118 |
# -----------------------------------------------------------------------------
|
| 119 |
+
# 3) EVALUATION LOGIC (unchanged)
|
| 120 |
# -----------------------------------------------------------------------------
|
| 121 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 122 |
|
|
|
|
| 128 |
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 129 |
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 130 |
|
|
|
|
| 131 |
try:
|
| 132 |
r = requests.get(questions_url, timeout=15)
|
| 133 |
r.raise_for_status()
|
|
|
|
| 138 |
if not questions:
|
| 139 |
return "No questions returned.", None
|
| 140 |
|
| 141 |
+
agent = BasicAgent()
|
| 142 |
+
logs = []
|
|
|
|
| 143 |
answers = []
|
| 144 |
for item in questions:
|
| 145 |
+
tid = item.get("task_id")
|
| 146 |
+
q = item.get("question")
|
| 147 |
if not tid or not q:
|
| 148 |
continue
|
| 149 |
ans = agent(q)
|
|
|
|
| 153 |
if not answers:
|
| 154 |
return "Agent produced no answers.", pd.DataFrame(logs)
|
| 155 |
|
|
|
|
| 156 |
try:
|
| 157 |
payload = {
|
| 158 |
+
"username": profile.username,
|
| 159 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 160 |
+
"answers": answers,
|
| 161 |
}
|
| 162 |
r2 = requests.post(submit_url, json=payload, timeout=60)
|
| 163 |
r2.raise_for_status()
|
| 164 |
+
res = r2.json()
|
| 165 |
status = (
|
| 166 |
f"✔ Submission Successful!\n"
|
| 167 |
f"User: {res.get('username')}\n"
|
|
|
|
| 175 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
| 176 |
|
| 177 |
# -----------------------------------------------------------------------------
|
| 178 |
+
# 4) GRADIO EVALUATION UI (unchanged)
|
| 179 |
# -----------------------------------------------------------------------------
|
| 180 |
with gr.Blocks() as demo:
|
| 181 |
gr.Markdown("# Basic Agent Evaluation Runner")
|