Create CustomAgent that inherits from BasicAgent
Browse files- CustomAgent.py +5 -0
- app.py +12 -2
- utils.py +25 -18
CustomAgent.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from BasicAgent import BasicAgent
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class CustomAgent(BasicAgent):
|
| 5 |
+
...
|
app.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Demo app
|
| 7 |
with gr.Blocks() as demo:
|
|
@@ -21,6 +25,7 @@ wip
|
|
| 21 |
)
|
| 22 |
gr.LoginButton()
|
| 23 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 24 |
status_output = gr.Textbox(
|
| 25 |
label="Run Status / Submission Result",
|
| 26 |
lines=5,
|
|
@@ -30,8 +35,13 @@ wip
|
|
| 30 |
label="Questions and Agent Answers",
|
| 31 |
wrap=True
|
| 32 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
run_button.click(
|
| 34 |
-
fn=
|
|
|
|
| 35 |
outputs=[status_output, results_table]
|
| 36 |
)
|
| 37 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
from BasicAgent import BasicAgent
|
| 4 |
+
from CustomAgent import CustomAgent
|
| 5 |
+
from utils import get_agents, run_and_submit_all
|
| 6 |
|
| 7 |
+
# Get all available agents
|
| 8 |
+
AGENTS = get_agents()
|
| 9 |
|
| 10 |
# Demo app
|
| 11 |
with gr.Blocks() as demo:
|
|
|
|
| 25 |
)
|
| 26 |
gr.LoginButton()
|
| 27 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 28 |
+
agent_dropdown = gr.Dropdown(choices=list(AGENTS.keys()), label="Select Agent")
|
| 29 |
status_output = gr.Textbox(
|
| 30 |
label="Run Status / Submission Result",
|
| 31 |
lines=5,
|
|
|
|
| 35 |
label="Questions and Agent Answers",
|
| 36 |
wrap=True
|
| 37 |
)
|
| 38 |
+
|
| 39 |
+
def wrapper(a: str) -> tuple[str, list]:
|
| 40 |
+
return run_and_submit_all(AGENTS.get(a, BasicAgent))
|
| 41 |
+
|
| 42 |
run_button.click(
|
| 43 |
+
fn=wrapper,
|
| 44 |
+
inputs=[agent_dropdown],
|
| 45 |
outputs=[status_output, results_table]
|
| 46 |
)
|
| 47 |
|
utils.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
|
@@ -11,9 +13,20 @@ from BasicAgent import BasicAgent
|
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
|
| 14 |
-
def
|
| 15 |
"""
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
and displays the results.
|
| 18 |
"""
|
| 19 |
if profile:
|
|
@@ -27,17 +40,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, agent: BasicAgent) -> Tu
|
|
| 27 |
questions_url = f"{api_url}/questions"
|
| 28 |
submit_url = f"{api_url}/submit"
|
| 29 |
|
| 30 |
-
# 1.
|
| 31 |
-
try:
|
| 32 |
-
agent = BasicAgent()
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print(f"Error instantiating agent: {e}")
|
| 35 |
-
return f"Error initializing agent: {e}", None
|
| 36 |
-
# In the case of an app running as a HF Space, this link points toward your codebase
|
| 37 |
-
agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main"
|
| 38 |
-
print(agent_code)
|
| 39 |
-
|
| 40 |
-
# 2. Fetch Questions
|
| 41 |
print(f"Fetching questions from: {questions_url}")
|
| 42 |
try:
|
| 43 |
response = requests.get(questions_url, timeout=15)
|
|
@@ -58,7 +61,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, agent: BasicAgent) -> Tu
|
|
| 58 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 59 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 60 |
|
| 61 |
-
#
|
| 62 |
results_log = []
|
| 63 |
answers_payload = []
|
| 64 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
@@ -80,12 +83,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, agent: BasicAgent) -> Tu
|
|
| 80 |
print("Agent did not produce any answers to submit.")
|
| 81 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 86 |
print(status_update)
|
| 87 |
|
| 88 |
-
#
|
| 89 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 90 |
try:
|
| 91 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 1 |
+
import inspect
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
+
from typing import Any
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import requests
|
|
|
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
|
| 16 |
+
def get_agents() -> dict[str, type]:
|
| 17 |
"""
|
| 18 |
+
Get all agents that inherit from BasicAgent, including imported ones.
|
| 19 |
+
"""
|
| 20 |
+
return {
|
| 21 |
+
name: obj
|
| 22 |
+
for name, obj in globals().items()
|
| 23 |
+
if inspect.isclass(obj) and issubclass(obj, BasicAgent)
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None, agent: BasicAgent) -> tuple[str, Any]:
|
| 28 |
+
"""
|
| 29 |
+
Fetches all questions, runs the agent on them, submits all answers,
|
| 30 |
and displays the results.
|
| 31 |
"""
|
| 32 |
if profile:
|
|
|
|
| 40 |
questions_url = f"{api_url}/questions"
|
| 41 |
submit_url = f"{api_url}/submit"
|
| 42 |
|
| 43 |
+
# 1. Fetch Questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
print(f"Fetching questions from: {questions_url}")
|
| 45 |
try:
|
| 46 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 61 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 62 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 63 |
|
| 64 |
+
# 2. Run your Agent
|
| 65 |
results_log = []
|
| 66 |
answers_payload = []
|
| 67 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 83 |
print("Agent did not produce any answers to submit.")
|
| 84 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 85 |
|
| 86 |
+
# 3. Prepare Submission
|
| 87 |
+
submission_data = {
|
| 88 |
+
"username": username.strip(),
|
| 89 |
+
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
|
| 90 |
+
"answers": answers_payload
|
| 91 |
+
}
|
| 92 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 93 |
print(status_update)
|
| 94 |
|
| 95 |
+
# 4. Submit
|
| 96 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 97 |
try:
|
| 98 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|