Spaces:
Runtime error
Runtime error
Commit
·
b422574
1
Parent(s):
622785e
agents course submit
Browse files- .gitignore +2 -1
- __pycache__/tools.cpython-312.pyc +0 -0
- app.py +509 -12
- app_smolagents.py +273 -0
- test.py +0 -18
- tool.py +0 -0
- tools.py +65 -0
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
.venv/
|
| 2 |
-
.env
|
|
|
|
|
|
| 1 |
.venv/
|
| 2 |
+
.env
|
| 3 |
+
temp/
|
__pycache__/tools.cpython-312.pyc
ADDED
|
Binary file (1.93 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,8 +1,39 @@
|
|
|
|
|
| 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 ---
|
|
@@ -10,15 +41,467 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 24 |
"""
|
|
@@ -41,7 +524,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 41 |
|
| 42 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 43 |
try:
|
| 44 |
-
agent =
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error instantiating agent: {e}")
|
| 47 |
return f"Error initializing agent: {e}", None
|
|
@@ -55,6 +538,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 55 |
response = requests.get(questions_url, timeout=15)
|
| 56 |
response.raise_for_status()
|
| 57 |
questions_data = response.json()
|
|
|
|
| 58 |
if not questions_data:
|
| 59 |
print("Fetched questions list is empty.")
|
| 60 |
return "Fetched questions list is empty or invalid format.", None
|
|
@@ -74,14 +558,26 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 74 |
results_log = []
|
| 75 |
answers_payload = []
|
| 76 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
|
|
|
| 77 |
for item in questions_data:
|
| 78 |
task_id = item.get("task_id")
|
| 79 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if not task_id or question_text is None:
|
| 81 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 82 |
continue
|
| 83 |
try:
|
| 84 |
-
submitted_answer = agent(question_text)
|
| 85 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 86 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 87 |
except Exception as e:
|
|
@@ -173,6 +669,7 @@ with gr.Blocks() as demo:
|
|
| 173 |
)
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
|
|
|
| 176 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 177 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 178 |
space_host_startup = os.getenv("SPACE_HOST")
|
|
|
|
| 1 |
+
# Standard library
|
| 2 |
import os
|
| 3 |
+
import io
|
| 4 |
+
import base64
|
| 5 |
+
import shutil
|
| 6 |
+
import subprocess
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Any, Dict, List, TypedDict
|
| 9 |
+
|
| 10 |
+
# Third-party libraries
|
| 11 |
import gradio as gr
|
|
|
|
|
|
|
| 12 |
import pandas as pd
|
| 13 |
+
import requests
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
import PIL.Image as Image
|
| 16 |
+
from openai import AzureOpenAI
|
| 17 |
+
|
| 18 |
+
# LangChain and LangGraph
|
| 19 |
+
from langchain_openai import AzureChatOpenAI
|
| 20 |
+
from langchain_core.tools import tool
|
| 21 |
+
from langchain.tools import Tool
|
| 22 |
+
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 23 |
+
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 24 |
+
from langchain.agents import AgentExecutor
|
| 25 |
+
from langchain.agents.openai_functions_agent.base import create_openai_functions_agent
|
| 26 |
+
from langchain.tools import tool
|
| 27 |
+
from langgraph.graph import START, StateGraph, END
|
| 28 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 29 |
+
|
| 30 |
+
# LangChain Community Tools
|
| 31 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
| 32 |
+
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
| 33 |
+
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
| 34 |
+
|
| 35 |
+
# Custom Tools
|
| 36 |
+
from tools import add, subtract, divide, multiply, modulus, string_reverse
|
| 37 |
|
| 38 |
# (Keep Constants as is)
|
| 39 |
# --- Constants ---
|
|
|
|
| 41 |
|
| 42 |
# --- Basic Agent Definition ---
|
| 43 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 44 |
+
|
| 45 |
+
load_dotenv()
|
| 46 |
+
|
| 47 |
+
# Azure OpenAI model
|
| 48 |
+
llm = AzureChatOpenAI(
|
| 49 |
+
deployment_name=os.environ["AZURE_OPENAI_GPT41MINI_ID"],
|
| 50 |
+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
|
| 51 |
+
api_version=os.environ["AZURE_OPENAI_GPT41MINI_VERSION"],
|
| 52 |
+
azure_endpoint=os.environ["AZURE_OPENAI_GPT41MINI_ENDPOINT"],
|
| 53 |
+
temperature=0
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
o4_mini = AzureChatOpenAI(
|
| 57 |
+
deployment_name = os.environ.get("AZURE_OPENAI_O4MINI_ID"),
|
| 58 |
+
api_key=os.environ.get("AZURE_OPENAI_API_KEY"),
|
| 59 |
+
api_version=os.environ.get("AZURE_OPENAI_O4MINI_VERSION"),
|
| 60 |
+
azure_endpoint=os.environ.get("AZURE_OPENAI_O4MINI_ENDPOINT")
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
class AgentState(TypedDict, total=False):
|
| 64 |
+
file_path: str | None # Contains file path
|
| 65 |
+
question: str # Contains tabular file path (CSV)
|
| 66 |
+
answer: str | None
|
| 67 |
+
agent_type: str | None
|
| 68 |
+
messages: list[AIMessage | HumanMessage | SystemMessage]
|
| 69 |
+
|
| 70 |
+
## Tools
|
| 71 |
+
duckduckgo_search = Tool(
|
| 72 |
+
name="duckduckgo_search",
|
| 73 |
+
func=DuckDuckGoSearchRun().run,
|
| 74 |
+
description="""A wrapper around DuckDuckGo Search.
|
| 75 |
+
Useful for when you need to answer questions about current events.
|
| 76 |
+
Input should be a search query."""
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
@tool
|
| 80 |
+
def web_search(query: str):
|
| 81 |
+
"""
|
| 82 |
+
Description:
|
| 83 |
+
A web search tool. Scrapes the top results and returns each on its own line.
|
| 84 |
+
Arguments:
|
| 85 |
+
• query (str) : question you want to web search.
|
| 86 |
+
Return:
|
| 87 |
+
str – A newline-separated text summary: '<title> — <url> : <snippet>' or 'No results found'
|
| 88 |
+
"""
|
| 89 |
+
search = TavilySearchResults()
|
| 90 |
+
results = search.run(query)
|
| 91 |
+
return "\n".join([f"- {r['content']} ({r['url']})" for r in results])
|
| 92 |
+
|
| 93 |
+
@tool
|
| 94 |
+
def wikipedia_query(query: str):
|
| 95 |
+
"""
|
| 96 |
+
Description:
|
| 97 |
+
Query the English-language Wikipedia via the MediaWiki API and
|
| 98 |
+
return a short plain-text extract.
|
| 99 |
+
Arguments:
|
| 100 |
+
• query (str) : Page title or free-text search string.
|
| 101 |
+
Return:
|
| 102 |
+
str – Extracted summary paragraph.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
wiki = WikipediaAPIWrapper()
|
| 106 |
+
return wiki.run(query)
|
| 107 |
+
|
| 108 |
+
@tool
|
| 109 |
+
def python_handler(filepath: str) -> str:
|
| 110 |
+
"""
|
| 111 |
+
Description:
|
| 112 |
+
Execute a stand-alone Python script in a sandboxed subprocess and
|
| 113 |
+
capture anything the script prints to stdout. Stderr is returned
|
| 114 |
+
instead if the script exits with a non-zero status.
|
| 115 |
+
Arguments:
|
| 116 |
+
• filepath (str): Path to the .py file to run.
|
| 117 |
+
Return:
|
| 118 |
+
str – The final output of the .py file.
|
| 119 |
+
"""
|
| 120 |
+
try:
|
| 121 |
+
result = subprocess.run(
|
| 122 |
+
["python", filepath],
|
| 123 |
+
capture_output=True,
|
| 124 |
+
text=True,
|
| 125 |
+
timeout=30 # Safety
|
| 126 |
+
)
|
| 127 |
+
return result.stdout.strip() if result.returncode == 0 else result.stderr
|
| 128 |
+
except Exception as e:
|
| 129 |
+
return f"Execution failed: {str(e)}"
|
| 130 |
+
|
| 131 |
+
@tool
|
| 132 |
+
def addition_tool(list: List[float]) -> float:
|
| 133 |
+
"""
|
| 134 |
+
Description:
|
| 135 |
+
A simple addition tool that takes a list of numbers and returns their sum.
|
| 136 |
+
Arguments:
|
| 137 |
+
• list (List[float]): List of numbers to add.
|
| 138 |
+
Return:
|
| 139 |
+
float – The sum of the numbers in the list.
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
return sum(list)
|
| 143 |
+
|
| 144 |
+
@tool
|
| 145 |
+
def xlsx_handler(filepath: str) -> List[Dict[str, Any]]:
|
| 146 |
+
"""
|
| 147 |
+
Description:
|
| 148 |
+
Load the first sheet of an Excel workbook and convert it into
|
| 149 |
+
a JSON-serialisable list of row dictionaries (records).
|
| 150 |
+
Arguments:
|
| 151 |
+
• filepath (str): Absolute or relative path to the .xlsx file.
|
| 152 |
+
Return:
|
| 153 |
+
str – A list of dictionaries representing the column names and their values.
|
| 154 |
+
"""
|
| 155 |
+
# Load the Excel file
|
| 156 |
+
df = pd.read_excel(filepath)
|
| 157 |
+
|
| 158 |
+
columns = df.columns.tolist()
|
| 159 |
+
|
| 160 |
+
result = []
|
| 161 |
+
for col in columns:
|
| 162 |
+
result.append({"column": col, "values": df[col].tolist()})
|
| 163 |
+
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
## Functions
|
| 167 |
+
def img_to_data(img: Image.Image) -> str:
|
| 168 |
+
buf = io.BytesIO(); img.save(buf, format="PNG", optimize=True)
|
| 169 |
+
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 170 |
+
return f"data:image/png;base64,{b64}"
|
| 171 |
+
|
| 172 |
+
def task_examiner(state: AgentState):
|
| 173 |
+
file_path = state["file_path"]
|
| 174 |
+
|
| 175 |
+
if file_path != None:
|
| 176 |
+
p = Path(file_path)
|
| 177 |
+
suffix = p.suffix
|
| 178 |
+
if suffix == ".png":
|
| 179 |
+
state["agent_type"] = "image"
|
| 180 |
+
elif suffix == ".mp3":
|
| 181 |
+
state["agent_type"] = "audio"
|
| 182 |
+
elif suffix == ".py" or suffix == ".xlsx":
|
| 183 |
+
state["agent_type"] = "code"
|
| 184 |
+
else:
|
| 185 |
+
state["agent_type"] = "general"
|
| 186 |
+
return state
|
| 187 |
+
|
| 188 |
+
def task_router(state: AgentState) -> str:
|
| 189 |
+
|
| 190 |
+
return state["agent_type"]
|
| 191 |
+
|
| 192 |
+
## Agents
|
| 193 |
+
|
| 194 |
+
def general_agent(state: AgentState):
|
| 195 |
+
|
| 196 |
+
question = state["question"]
|
| 197 |
+
|
| 198 |
+
tools = [web_search, wikipedia_query, string_reverse]
|
| 199 |
+
|
| 200 |
+
system_prompt = ChatPromptTemplate.from_messages([
|
| 201 |
+
("system",
|
| 202 |
+
"""
|
| 203 |
+
SYSTEM GUIDELINES:
|
| 204 |
+
You are a general-purpose AI assistant tasked with accurately answering the user's questions.
|
| 205 |
+
|
| 206 |
+
BEHAVIOR RULES:
|
| 207 |
+
- You have access to a set of specialized tools to help with certain tasks.
|
| 208 |
+
- You MUST reason step-by-step internally before calling any tool.
|
| 209 |
+
- Only call a tool when you are confident it is required to answer the question.
|
| 210 |
+
- Tool calls should be minimal and purposeful.
|
| 211 |
+
|
| 212 |
+
TOOL REUSE RULE:
|
| 213 |
+
- Maintain an internal list of tools already used in the current answer.
|
| 214 |
+
- You MUST NOT call the same tool more than once per answer. (However, you may still use a different tool.)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
AVAILABLE TOOLS:
|
| 218 |
+
- `web_search`: Searches the web for up-to-date information not present in your training data.
|
| 219 |
+
- `wikipedia_query`: Searches Wikipedia for factual information not present in your training data.
|
| 220 |
+
- `string_reverse`: Reverses a sentence. Use this if the input appears garbled, backward, or unreadable.
|
| 221 |
+
|
| 222 |
+
INPUT FORMAT:
|
| 223 |
+
- A single user question (free-form text).
|
| 224 |
+
|
| 225 |
+
OUTPUT FORMAT:
|
| 226 |
+
- Output ONLY the final answer to the user's question.
|
| 227 |
+
- NEVER include explanations, reasoning steps, or tool usage metadata.
|
| 228 |
+
- Your output must strictly follow the required format described below.
|
| 229 |
+
|
| 230 |
+
SPECIAL CASE FORMATTING RULES:
|
| 231 |
+
- If the question includes a YouTube link (e.g. `https://www.youtube.com/watch?...`), respond ONLY with:
|
| 232 |
+
`Don't know`
|
| 233 |
+
|
| 234 |
+
- For questions beginning with:
|
| 235 |
+
- **"How many..."** → Respond with a **single numeral** (e.g., `5`). Do **not** include punctuation, words, or units.
|
| 236 |
+
- **"What number..."** → Respond with a **single numeral** (e.g., `42`). No extra text.
|
| 237 |
+
- **"Who did..."** → Respond with the **full name of the person only**, without any punctuation or additional commentary.
|
| 238 |
+
|
| 239 |
+
- If asked for a **comma-separated list**, respond in the format:
|
| 240 |
+
`[item1,item2,item3]`
|
| 241 |
+
NEVER use `a,b,c,d` formatting outside brackets.
|
| 242 |
+
|
| 243 |
+
- If asked to **output a list**, respond with:
|
| 244 |
+
`[item1,item2,item3]`
|
| 245 |
+
|
| 246 |
+
- If the question says: **"What does person A say when..."** → Respond with **only the quoted phrase**, with no extra punctuation, commentary, or formatting.
|
| 247 |
+
|
| 248 |
+
FAILURE TO FOLLOW THESE FORMATTING RULES WILL RESULT IN AN INVALID RESPONSE.
|
| 249 |
+
"""),
|
| 250 |
+
("user", "{input}"),
|
| 251 |
+
MessagesPlaceholder("agent_scratchpad"),
|
| 252 |
+
])
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
agent = create_openai_functions_agent(
|
| 256 |
+
llm=llm,
|
| 257 |
+
tools=tools,
|
| 258 |
+
prompt=system_prompt
|
| 259 |
+
)
|
| 260 |
+
|
| 261 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
| 262 |
+
|
| 263 |
+
response = agent_executor.invoke({"input": question})
|
| 264 |
+
|
| 265 |
+
state["answer"] = response["output"]
|
| 266 |
+
|
| 267 |
+
return state
|
| 268 |
+
|
| 269 |
+
def audio_agent(state: AgentState):
|
| 270 |
+
|
| 271 |
+
with open(state["file_path"], "rb") as f:
|
| 272 |
+
client = AzureOpenAI(
|
| 273 |
+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
|
| 274 |
+
api_version='2024-06-01',
|
| 275 |
+
azure_endpoint=os.environ["AZURE_OPENAI_WHISPER_ENDPOINT"],
|
| 276 |
+
)
|
| 277 |
+
transcript = client.audio.transcriptions.create(model='whisper', file=f, response_format="text")
|
| 278 |
+
|
| 279 |
+
question = state["question"]
|
| 280 |
+
|
| 281 |
+
system_msg = SystemMessage(
|
| 282 |
+
content=("You are an AI assistant that answers the user's question based solely on the provided transcript."
|
| 283 |
+
"When the user asks for a “comma-delimited / comma-separated list”, you must:"
|
| 284 |
+
" - Filter the items exactly as requested."
|
| 285 |
+
" - Output one single line that contains the items separated by commas and a space enclosed in square brackets."
|
| 286 |
+
" - Output nothing else- no extra words or explanations"
|
| 287 |
+
"OUTPUT FORMAT EXAMPLES:"
|
| 288 |
+
"If asked to output a list -> Output: [item1,item2,item3]"
|
| 289 |
+
"If asked something else -> Output: text answering exactly that question and nothing more"
|
| 290 |
+
)
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
messages = [
|
| 294 |
+
system_msg,
|
| 295 |
+
HumanMessage(
|
| 296 |
+
content=[
|
| 297 |
+
{
|
| 298 |
+
"type": "text",
|
| 299 |
+
"text": f"Transcript:\n{transcript}\n\nQuestion:\n{question}"
|
| 300 |
+
}
|
| 301 |
+
]
|
| 302 |
+
)
|
| 303 |
+
]
|
| 304 |
+
|
| 305 |
+
response = llm.invoke(messages)
|
| 306 |
+
|
| 307 |
+
state["answer"] = response.content.strip()
|
| 308 |
+
|
| 309 |
+
return state
|
| 310 |
+
|
| 311 |
+
def image_agent(state: AgentState):
|
| 312 |
+
|
| 313 |
+
file_path = state["file_path"]
|
| 314 |
+
question = state["question"]
|
| 315 |
+
|
| 316 |
+
with open(file_path, "rb") as image_file:
|
| 317 |
+
|
| 318 |
+
image_bytes = image_file.read()
|
| 319 |
+
|
| 320 |
+
image_base64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 321 |
+
|
| 322 |
+
system_msg = SystemMessage(
|
| 323 |
+
content=("""
|
| 324 |
+
You are a Image AI assistant that can process images and answer correctly the user's questions"
|
| 325 |
+
**OUTPUT** only the final answer and absolutely nothing else (no punctuation, no sentence, no units).
|
| 326 |
+
""")
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
messages = [
|
| 330 |
+
system_msg,
|
| 331 |
+
HumanMessage(
|
| 332 |
+
content=[
|
| 333 |
+
{
|
| 334 |
+
"type": "text",
|
| 335 |
+
"text": (f"{question}")
|
| 336 |
+
},
|
| 337 |
+
{
|
| 338 |
+
"type": "image_url",
|
| 339 |
+
"image_url": {
|
| 340 |
+
"url": f"data:image/png;base64,{image_base64}"
|
| 341 |
+
},
|
| 342 |
+
}
|
| 343 |
+
]
|
| 344 |
+
)
|
| 345 |
+
]
|
| 346 |
+
|
| 347 |
+
response = llm.invoke(messages)
|
| 348 |
+
|
| 349 |
+
state["answer"] = response.content.strip()
|
| 350 |
+
|
| 351 |
+
return state
|
| 352 |
+
|
| 353 |
+
def code_agent(state: AgentState):
|
| 354 |
+
|
| 355 |
+
file_path = state["file_path"]
|
| 356 |
+
question = state["question"]
|
| 357 |
+
|
| 358 |
+
tools = [xlsx_handler, python_handler, addition_tool]
|
| 359 |
+
|
| 360 |
+
system_prompt = ChatPromptTemplate.from_messages([
|
| 361 |
+
("system",
|
| 362 |
+
""" SYSTEM GUIDELINES:
|
| 363 |
+
- You are a data AI assistant and your job is to answer questions that depend on .xlsx or .py files.
|
| 364 |
+
- You have in your disposal 2 tools that are mandatory for solving the tasks.
|
| 365 |
+
- You **MUST** use the tools as instructed below and you **MUST** output only the final numeric result of the task.
|
| 366 |
+
INPUT FORMAT:
|
| 367 |
+
- A question (text) based on a file which will be either .py or .xlsx.
|
| 368 |
+
- The path of the file related to the question.
|
| 369 |
+
TOOLS:
|
| 370 |
+
- Tool name: xlsx_handler, Purpose: This is the tool you should use if the file contained in the file_path is an .xlsx file and it's purpose is to return the contents of the file in a list of dictionaries for you to process, reason **INTERNALLY** and output only the final numeric result.
|
| 371 |
+
- Tool name: python_handler, Purpose: This is the tool you should use if the file contained in the file_path is a .py file and it's purpose is to execute the python file and return the final numeric result of it.
|
| 372 |
+
- Tool name: addition_tool, Purpose: This is the tool you should use if the question asks you to sum a list of numbers and return the final numeric result.
|
| 373 |
+
EXAMPLE OUTPUTS:
|
| 374 |
+
- Input: "What is the result of the code in the file?" Output: "5"
|
| 375 |
+
- Input: "What is the total sales mentioned in the file. Your answer must have 2 decimal places?" Output: "305.00"
|
| 376 |
+
- YOU MUST OUTPUT ONLY THE FINAL NUMBER.
|
| 377 |
+
|
| 378 |
+
The file relevant to the task is at: {file_path}."""),
|
| 379 |
+
("user", "{input}"),
|
| 380 |
+
MessagesPlaceholder("agent_scratchpad"),
|
| 381 |
+
])
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
agent = create_openai_functions_agent(
|
| 385 |
+
llm=llm,
|
| 386 |
+
tools=tools,
|
| 387 |
+
prompt=system_prompt # Optional – remove if you want default prompt behavior
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
| 391 |
+
|
| 392 |
+
response = agent_executor.invoke({"input": question, "file_path": file_path})
|
| 393 |
+
|
| 394 |
+
state["answer"] = response["output"]
|
| 395 |
+
|
| 396 |
+
return state
|
| 397 |
+
|
| 398 |
+
# def math_agent(state: AgentState):
|
| 399 |
+
|
| 400 |
+
# file_path = state["file_path"]
|
| 401 |
+
# question = state["question"]
|
| 402 |
+
|
| 403 |
+
# tools = [add, subtract, divide, multiply, modulus]
|
| 404 |
+
|
| 405 |
+
# system_prompt = ChatPromptTemplate.from_messages([
|
| 406 |
+
# ("system",
|
| 407 |
+
# """ SYSTEM GUIDELINES:
|
| 408 |
+
# - You are a data AI assistant and your job is to answer questions that are math-related.
|
| 409 |
+
# - You have in your disposal 5 tools that are mandatory for solving the tasks.
|
| 410 |
+
# - You **MUST** use the tools as instructed below and you **MUST** output only the final numeric result of the task.
|
| 411 |
+
# INPUT FORMAT:
|
| 412 |
+
# - A question (text) based that needs a math function to be solved.
|
| 413 |
+
# TOOLS:
|
| 414 |
+
# - Tool name: add, Purpose: A simple addition tool that takes two numbers and returns their sum.
|
| 415 |
+
# - Tool name: subtract, Purpose: A simple subtraction tool that takes two numbers and returns the result of the first number minus the second number.
|
| 416 |
+
# - Tool name: divide, Purpose: A simple division tool that takes two numbers and returns the result of the first number divided by the second number.
|
| 417 |
+
# - Tool name: multiply, Purpose: A simple multiplication tool that takes two numbers and returns the result of the first number multiplied by the second number.
|
| 418 |
+
# - Tool name: modulus, Purpose: A simple modulus tool that takes two numbers and returns the result of the first number modulo the second number.
|
| 419 |
+
# EXAMPLE OUTPUTS:
|
| 420 |
+
# - Input: "What is 10 divided by 2" Output: "5"
|
| 421 |
+
# - YOU MUST OUTPUT ONLY THE FINAL NUMBER.
|
| 422 |
+
|
| 423 |
+
# """),
|
| 424 |
+
# ("user", "{input}"),
|
| 425 |
+
# MessagesPlaceholder("agent_scratchpad"),
|
| 426 |
+
# ])
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
# agent = OpenAIFunctionsAgent(
|
| 430 |
+
# llm=llm,
|
| 431 |
+
# tools=tools,
|
| 432 |
+
# prompt=system_prompt
|
| 433 |
+
# )
|
| 434 |
+
|
| 435 |
+
# agent_executor = AgentExecutor.from_agent_and_tools(
|
| 436 |
+
# agent=agent,
|
| 437 |
+
# tools=tools,
|
| 438 |
+
# verbose=True,
|
| 439 |
+
# )
|
| 440 |
+
|
| 441 |
+
# response = agent_executor.invoke({"input": question, "file_path": file_path})
|
| 442 |
+
|
| 443 |
+
# state["answer"] = response["output"]
|
| 444 |
+
|
| 445 |
+
# return state
|
| 446 |
+
|
| 447 |
+
## Agent Workflow
|
| 448 |
+
|
| 449 |
+
class Agent_Workflow:
|
| 450 |
def __init__(self):
|
| 451 |
+
print("Agent Workflow initialized.")
|
| 452 |
+
def __call__(self, question: str, filepath: str) -> str:
|
| 453 |
+
|
| 454 |
+
builder = StateGraph(AgentState)
|
| 455 |
+
|
| 456 |
+
# Agent Nodes
|
| 457 |
+
builder.add_node("task_examiner", task_examiner)
|
| 458 |
+
builder.add_node("general_agent", general_agent)
|
| 459 |
+
builder.add_node("audio_agent", audio_agent)
|
| 460 |
+
builder.add_node("image_agent", image_agent)
|
| 461 |
+
builder.add_node("code_agent", code_agent)
|
| 462 |
+
|
| 463 |
+
# Edges that connect agent nodes
|
| 464 |
+
builder.add_edge(START, "task_examiner")
|
| 465 |
+
builder.add_conditional_edges("task_examiner", task_router,
|
| 466 |
+
{
|
| 467 |
+
"general": "general_agent",
|
| 468 |
+
"audio": "audio_agent",
|
| 469 |
+
"image": "image_agent",
|
| 470 |
+
"code": "code_agent",
|
| 471 |
+
}
|
| 472 |
+
)
|
| 473 |
+
builder.add_edge("general_agent", END)
|
| 474 |
+
builder.add_edge("audio_agent", END)
|
| 475 |
+
builder.add_edge("image_agent", END)
|
| 476 |
+
builder.add_edge("code_agent", END)
|
| 477 |
+
|
| 478 |
+
workflow_graph = builder.compile()
|
| 479 |
+
|
| 480 |
+
state = workflow_graph.invoke({"file_path": filepath, "question": question, "answer": "",})
|
| 481 |
+
|
| 482 |
+
return state["answer"]
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
def fetch_task_file_static(task_id: str, file_name: str | None = None, session: requests.Session | None = None) -> Path:
|
| 486 |
+
"""
|
| 487 |
+
Download the attachment for `task_id` to temp_files/<task_id>.<suffix>
|
| 488 |
+
"""
|
| 489 |
+
if file_name == None:
|
| 490 |
+
return None
|
| 491 |
+
|
| 492 |
+
# Decide the suffix
|
| 493 |
+
suffix = Path(file_name).suffix if file_name else ""
|
| 494 |
+
dest = "temp/"+task_id+suffix
|
| 495 |
+
|
| 496 |
+
url = f"{DEFAULT_API_URL}/files/{task_id}"
|
| 497 |
+
s = session or requests
|
| 498 |
+
|
| 499 |
+
with s.get(url, stream=True, timeout=30) as r:
|
| 500 |
+
r.raise_for_status()
|
| 501 |
+
with open(dest, "wb") as f:
|
| 502 |
+
shutil.copyfileobj(r.raw, f)
|
| 503 |
+
|
| 504 |
+
return dest
|
| 505 |
|
| 506 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 507 |
"""
|
|
|
|
| 524 |
|
| 525 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 526 |
try:
|
| 527 |
+
agent = Agent_Workflow()
|
| 528 |
except Exception as e:
|
| 529 |
print(f"Error instantiating agent: {e}")
|
| 530 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 538 |
response = requests.get(questions_url, timeout=15)
|
| 539 |
response.raise_for_status()
|
| 540 |
questions_data = response.json()
|
| 541 |
+
questions_data = questions_data
|
| 542 |
if not questions_data:
|
| 543 |
print("Fetched questions list is empty.")
|
| 544 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
| 558 |
results_log = []
|
| 559 |
answers_payload = []
|
| 560 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 561 |
+
session = requests.Session() # Reuse session for fetching files
|
| 562 |
+
|
| 563 |
for item in questions_data:
|
| 564 |
task_id = item.get("task_id")
|
| 565 |
question_text = item.get("question")
|
| 566 |
+
file_name = item.get("file_name")
|
| 567 |
+
|
| 568 |
+
file_path = None
|
| 569 |
+
|
| 570 |
+
if file_name:
|
| 571 |
+
try:
|
| 572 |
+
file_path = fetch_task_file_static(task_id, file_name, session=session)
|
| 573 |
+
except requests.HTTPError as e:
|
| 574 |
+
print(f"⚠️ Couldn’t fetch file for {task_id}: {e}")
|
| 575 |
+
|
| 576 |
if not task_id or question_text is None:
|
| 577 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 578 |
continue
|
| 579 |
try:
|
| 580 |
+
submitted_answer = agent(question_text, filepath=file_path)
|
| 581 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 582 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 583 |
except Exception as e:
|
|
|
|
| 669 |
)
|
| 670 |
|
| 671 |
if __name__ == "__main__":
|
| 672 |
+
|
| 673 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 674 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 675 |
space_host_startup = os.getenv("SPACE_HOST")
|
app_smolagents.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from typing import Optional, Dict
|
| 7 |
+
|
| 8 |
+
from smolagents import CodeAgent, FinalAnswerTool, ToolCallingAgent
|
| 9 |
+
from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool, VisitWebpageTool
|
| 10 |
+
from smolagents import PythonInterpreterTool
|
| 11 |
+
from smolagents import LiteLLMModel
|
| 12 |
+
from smolagents import AzureOpenAIServerModel
|
| 13 |
+
from tools import add, subtract, divide, multiply, modulus
|
| 14 |
+
from tools import string_reverse
|
| 15 |
+
|
| 16 |
+
# (Keep Constants as is)
|
| 17 |
+
# --- Constants ---
|
| 18 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
+
|
| 20 |
+
# --- Basic Agent Definition ---
|
| 21 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 22 |
+
|
| 23 |
+
load_dotenv()
|
| 24 |
+
|
| 25 |
+
o4mini = AzureOpenAIServerModel(
|
| 26 |
+
model_id = os.environ.get("AZURE_OPENAI_O4MINI_ID"),
|
| 27 |
+
api_key=os.environ.get("AZURE_OPENAI_API_KEY"),
|
| 28 |
+
api_version=os.environ.get("AZURE_OPENAI_O4MINI_VERSION"),
|
| 29 |
+
azure_endpoint=os.environ.get("AZURE_OPENAI_O4MINI_ENDPOINT")
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
gpt41mini = AzureOpenAIServerModel(
|
| 33 |
+
model_id = os.environ.get("AZURE_OPENAI_GPT41MINI_ID"),
|
| 34 |
+
api_key=os.environ.get("AZURE_OPENAI_API_KEY"),
|
| 35 |
+
api_version=os.environ.get("AZURE_OPENAI_GPT41MINI_VERSION"),
|
| 36 |
+
azure_endpoint=os.environ.get("AZURE_OPENAI_GPT41MINI_ENDPOINT")
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
web_agent = ToolCallingAgent(
|
| 41 |
+
model=gpt41mini,
|
| 42 |
+
tools=[
|
| 43 |
+
DuckDuckGoSearchTool(),
|
| 44 |
+
VisitWebpageTool(),
|
| 45 |
+
WikipediaSearchTool()
|
| 46 |
+
],
|
| 47 |
+
name="web_agent",
|
| 48 |
+
description="An agent for browsing the web and wikipedia to find information",
|
| 49 |
+
verbosity_level=0,
|
| 50 |
+
max_steps=10,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
coder_agent = ToolCallingAgent(
|
| 54 |
+
model=gpt41mini,
|
| 55 |
+
tools=[PythonInterpreterTool()],
|
| 56 |
+
name="coder_agent",
|
| 57 |
+
description="An agent for interpret code to solve programming tasks",
|
| 58 |
+
verbosity_level=0,
|
| 59 |
+
max_steps=10,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
math_agent = ToolCallingAgent(
|
| 64 |
+
model=gpt41mini,
|
| 65 |
+
tools=[
|
| 66 |
+
add, subtract, divide, multiply, modulus
|
| 67 |
+
],
|
| 68 |
+
name="math_agent",
|
| 69 |
+
description="An agent for solving math problems like add, subtract, divide, multiply, modulus",
|
| 70 |
+
verbosity_level=0,
|
| 71 |
+
max_steps=10,
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
text_agent = ToolCallingAgent(
|
| 75 |
+
model=gpt41mini,
|
| 76 |
+
tools=[string_reverse],
|
| 77 |
+
name="text_agent",
|
| 78 |
+
description="An agent that can work with text, like reversing a string",
|
| 79 |
+
verbosity_level=0,
|
| 80 |
+
max_steps=10,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 84 |
+
system_prompt = f.read()
|
| 85 |
+
|
| 86 |
+
manager_agent = ToolCallingAgent(
|
| 87 |
+
model=gpt41mini,
|
| 88 |
+
tools=[FinalAnswerTool()],
|
| 89 |
+
managed_agents=[web_agent, coder_agent, math_agent, text_agent],
|
| 90 |
+
name="manager_agent",
|
| 91 |
+
instructions=system_prompt,
|
| 92 |
+
planning_interval=5,
|
| 93 |
+
verbosity_level=2,
|
| 94 |
+
max_steps=15,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 98 |
+
"""
|
| 99 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 100 |
+
and displays the results.
|
| 101 |
+
"""
|
| 102 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 103 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 104 |
+
|
| 105 |
+
if profile:
|
| 106 |
+
username= f"{profile.username}"
|
| 107 |
+
print(f"User logged in: {username}")
|
| 108 |
+
else:
|
| 109 |
+
print("User not logged in.")
|
| 110 |
+
return "Please Login to Hugging Face with the button.", None
|
| 111 |
+
|
| 112 |
+
api_url = DEFAULT_API_URL
|
| 113 |
+
questions_url = f"{api_url}/questions"
|
| 114 |
+
submit_url = f"{api_url}/submit"
|
| 115 |
+
|
| 116 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 117 |
+
try:
|
| 118 |
+
agent = manager_agent
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Error instantiating agent: {e}")
|
| 121 |
+
return f"Error initializing agent: {e}", None
|
| 122 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
| 123 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 124 |
+
print(agent_code)
|
| 125 |
+
|
| 126 |
+
# 2. Fetch Questions
|
| 127 |
+
print(f"Fetching questions from: {questions_url}")
|
| 128 |
+
try:
|
| 129 |
+
response = requests.get(questions_url, timeout=15)
|
| 130 |
+
response.raise_for_status()
|
| 131 |
+
questions_data = response.json()
|
| 132 |
+
questions_data = questions_data[:1]
|
| 133 |
+
if not questions_data:
|
| 134 |
+
print("Fetched questions list is empty.")
|
| 135 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 136 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 137 |
+
except requests.exceptions.RequestException as e:
|
| 138 |
+
print(f"Error fetching questions: {e}")
|
| 139 |
+
return f"Error fetching questions: {e}", None
|
| 140 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 141 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 142 |
+
print(f"Response text: {response.text[:500]}")
|
| 143 |
+
return f"Error decoding server response for questions: {e}", None
|
| 144 |
+
except Exception as e:
|
| 145 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 146 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 147 |
+
|
| 148 |
+
# 3. Run your Agent
|
| 149 |
+
results_log = []
|
| 150 |
+
answers_payload = []
|
| 151 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 152 |
+
for item in questions_data:
|
| 153 |
+
task_id = item.get("task_id")
|
| 154 |
+
question_text = item.get("question")
|
| 155 |
+
if not task_id or question_text is None:
|
| 156 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 157 |
+
continue
|
| 158 |
+
try:
|
| 159 |
+
submitted_answer = agent(question_text)
|
| 160 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 161 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 162 |
+
except Exception as e:
|
| 163 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 164 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 165 |
+
|
| 166 |
+
if not answers_payload:
|
| 167 |
+
print("Agent did not produce any answers to submit.")
|
| 168 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 169 |
+
|
| 170 |
+
# 4. Prepare Submission
|
| 171 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 172 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 173 |
+
print(status_update)
|
| 174 |
+
|
| 175 |
+
# 5. Submit
|
| 176 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 177 |
+
try:
|
| 178 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 179 |
+
response.raise_for_status()
|
| 180 |
+
result_data = response.json()
|
| 181 |
+
final_status = (
|
| 182 |
+
f"Submission Successful!\n"
|
| 183 |
+
f"User: {result_data.get('username')}\n"
|
| 184 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 185 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 186 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 187 |
+
)
|
| 188 |
+
print("Submission successful.")
|
| 189 |
+
results_df = pd.DataFrame(results_log)
|
| 190 |
+
return final_status, results_df
|
| 191 |
+
except requests.exceptions.HTTPError as e:
|
| 192 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 193 |
+
try:
|
| 194 |
+
error_json = e.response.json()
|
| 195 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 196 |
+
except requests.exceptions.JSONDecodeError:
|
| 197 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 198 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 199 |
+
print(status_message)
|
| 200 |
+
results_df = pd.DataFrame(results_log)
|
| 201 |
+
return status_message, results_df
|
| 202 |
+
except requests.exceptions.Timeout:
|
| 203 |
+
status_message = "Submission Failed: The request timed out."
|
| 204 |
+
print(status_message)
|
| 205 |
+
results_df = pd.DataFrame(results_log)
|
| 206 |
+
return status_message, results_df
|
| 207 |
+
except requests.exceptions.RequestException as e:
|
| 208 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 209 |
+
print(status_message)
|
| 210 |
+
results_df = pd.DataFrame(results_log)
|
| 211 |
+
return status_message, results_df
|
| 212 |
+
except Exception as e:
|
| 213 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 214 |
+
print(status_message)
|
| 215 |
+
results_df = pd.DataFrame(results_log)
|
| 216 |
+
return status_message, results_df
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
# --- Build Gradio Interface using Blocks ---
|
| 220 |
+
with gr.Blocks() as demo:
|
| 221 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 222 |
+
gr.Markdown(
|
| 223 |
+
"""
|
| 224 |
+
**Instructions:**
|
| 225 |
+
|
| 226 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 227 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 228 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 229 |
+
|
| 230 |
+
---
|
| 231 |
+
**Disclaimers:**
|
| 232 |
+
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 233 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 234 |
+
"""
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
gr.LoginButton()
|
| 238 |
+
|
| 239 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 240 |
+
|
| 241 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 242 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 243 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 244 |
+
|
| 245 |
+
run_button.click(
|
| 246 |
+
fn=run_and_submit_all,
|
| 247 |
+
outputs=[status_output, results_table]
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
if __name__ == "__main__":
|
| 251 |
+
|
| 252 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 253 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 254 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 255 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 256 |
+
|
| 257 |
+
if space_host_startup:
|
| 258 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 259 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 260 |
+
else:
|
| 261 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 262 |
+
|
| 263 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 264 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 265 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 266 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 267 |
+
else:
|
| 268 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 269 |
+
|
| 270 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 271 |
+
|
| 272 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 273 |
+
demo.launch(debug=True, share=False)
|
test.py
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
#%%
|
| 2 |
-
import os
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import requests
|
| 5 |
-
import inspect
|
| 6 |
-
import pandas as pd
|
| 7 |
-
|
| 8 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
-
|
| 10 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 11 |
-
|
| 12 |
-
response = requests.get(questions_url, timeout=15)
|
| 13 |
-
response.raise_for_status()
|
| 14 |
-
questions_data = response.json()
|
| 15 |
-
|
| 16 |
-
for question in questions_data:
|
| 17 |
-
print(question)
|
| 18 |
-
# %%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tool.py
DELETED
|
File without changes
|
tools.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
# math tools
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def multiply(a: int, b: int) -> int:
|
| 7 |
+
"""Multiply two numbers.
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
return a * b
|
| 13 |
+
|
| 14 |
+
@tool
|
| 15 |
+
def add(a: int, b: int) -> int:
|
| 16 |
+
"""Add two numbers.
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
a: first int
|
| 20 |
+
b: second int
|
| 21 |
+
"""
|
| 22 |
+
return a + b
|
| 23 |
+
|
| 24 |
+
@tool
|
| 25 |
+
def subtract(a: int, b: int) -> int:
|
| 26 |
+
"""Subtract two numbers.
|
| 27 |
+
|
| 28 |
+
Args:
|
| 29 |
+
a: first int
|
| 30 |
+
b: second int
|
| 31 |
+
"""
|
| 32 |
+
return a - b
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def divide(a: int, b: int) -> float:
|
| 36 |
+
"""Divide two numbers.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
a: first int
|
| 40 |
+
b: second int
|
| 41 |
+
"""
|
| 42 |
+
if b == 0:
|
| 43 |
+
raise ValueError("Cannot divide by zero.")
|
| 44 |
+
return a / b
|
| 45 |
+
|
| 46 |
+
@tool
|
| 47 |
+
def modulus(a: int, b: int) -> int:
|
| 48 |
+
"""Get the modulus of two numbers.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
a: first int
|
| 52 |
+
b: second int
|
| 53 |
+
"""
|
| 54 |
+
return a % b
|
| 55 |
+
|
| 56 |
+
# text tools
|
| 57 |
+
|
| 58 |
+
@tool
|
| 59 |
+
def string_reverse(text: str) -> str:
|
| 60 |
+
"""Reverse a string.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
text: The string to reverse.
|
| 64 |
+
"""
|
| 65 |
+
return text[::-1]
|