Spaces:
Running
Running
ffreemt
commited on
Commit
·
6c88d14
1
Parent(s):
2a02f64
Try out
Browse files- app.py +8 -3
- basic_agent.py +94 -0
- requirements.txt +3 -1
app.py
CHANGED
@@ -5,18 +5,21 @@ import inspect
|
|
5 |
import pandas as pd
|
6 |
from ycecream import y
|
7 |
|
|
|
|
|
8 |
y.configure(sln=1)
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
-
y(os.environ)
|
15 |
y(DEFAULT_API_URL)
|
16 |
y(os.getenv("SPACE_ID"))
|
17 |
|
18 |
# --- Basic Agent Definition ---
|
19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
20 |
class BasicAgent:
|
21 |
def __init__(self):
|
22 |
print("BasicAgent initialized.")
|
@@ -25,6 +28,7 @@ class BasicAgent:
|
|
25 |
fixed_answer = "This is a default answer."
|
26 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
27 |
return fixed_answer
|
|
|
28 |
|
29 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
30 |
"""
|
@@ -42,7 +46,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
42 |
return "Please Login to Hugging Face with the button.", None
|
43 |
|
44 |
api_url = DEFAULT_API_URL
|
45 |
-
questions_url = f"{api_url}/questions"
|
46 |
submit_url = f"{api_url}/submit"
|
47 |
|
48 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
@@ -65,6 +69,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
65 |
print("Fetched questions list is empty.")
|
66 |
return "Fetched questions list is empty or invalid format.", None
|
67 |
print(f"Fetched {len(questions_data)} questions.")
|
|
|
68 |
except requests.exceptions.RequestException as e:
|
69 |
print(f"Error fetching questions: {e}")
|
70 |
return f"Error fetching questions: {e}", None
|
@@ -98,7 +103,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
98 |
print("Agent did not produce any answers to submit.")
|
99 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
100 |
|
101 |
-
# 4. Prepare Submission
|
102 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
103 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
104 |
print(status_update)
|
|
|
5 |
import pandas as pd
|
6 |
from ycecream import y
|
7 |
|
8 |
+
from basic_agent import BasicAgent
|
9 |
+
|
10 |
y.configure(sln=1)
|
11 |
|
12 |
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
15 |
|
16 |
+
# y(os.environ)
|
17 |
y(DEFAULT_API_URL)
|
18 |
y(os.getenv("SPACE_ID"))
|
19 |
|
20 |
# --- Basic Agent Definition ---
|
21 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
22 |
+
_ = """ basic_agent.py
|
23 |
class BasicAgent:
|
24 |
def __init__(self):
|
25 |
print("BasicAgent initialized.")
|
|
|
28 |
fixed_answer = "This is a default answer."
|
29 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
30 |
return fixed_answer
|
31 |
+
# """
|
32 |
|
33 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
34 |
"""
|
|
|
46 |
return "Please Login to Hugging Face with the button.", None
|
47 |
|
48 |
api_url = DEFAULT_API_URL
|
49 |
+
questions_url = f"{api_url}/questions" # https://agents-course-unit4-scoring.hf.space/questions
|
50 |
submit_url = f"{api_url}/submit"
|
51 |
|
52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
|
|
69 |
print("Fetched questions list is empty.")
|
70 |
return "Fetched questions list is empty or invalid format.", None
|
71 |
print(f"Fetched {len(questions_data)} questions.")
|
72 |
+
|
73 |
except requests.exceptions.RequestException as e:
|
74 |
print(f"Error fetching questions: {e}")
|
75 |
return f"Error fetching questions: {e}", None
|
|
|
103 |
print("Agent did not produce any answers to submit.")
|
104 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
105 |
|
106 |
+
# 4. Prepare Submission
|
107 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
108 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
109 |
print(status_update)
|
basic_agent.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import rich
|
2 |
+
from loguru import logger
|
3 |
+
import requests
|
4 |
+
|
5 |
+
print = rich.get_console().print
|
6 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
7 |
+
|
8 |
+
class BasicAgent:
|
9 |
+
def __init__(self):
|
10 |
+
logger.debug("BasicAgent initialized.")
|
11 |
+
def __call__(self, question: str) -> str:
|
12 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
13 |
+
print(f"Agent received question: {question}...")
|
14 |
+
fixed_answer = "This is a default answer."
|
15 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
16 |
+
return fixed_answer
|
17 |
+
|
18 |
+
def main():
|
19 |
+
try:
|
20 |
+
agent = BasicAgent()
|
21 |
+
except Exception as e:
|
22 |
+
logger.debug(f"Error instantiating agent: {e}")
|
23 |
+
return f"Error initializing agent: {e}", None
|
24 |
+
|
25 |
+
api_url = DEFAULT_API_URL
|
26 |
+
questions_url = f"{api_url}/questions"
|
27 |
+
submit_url = f"{api_url}/submit"
|
28 |
+
username = "mikeee"
|
29 |
+
repo_name = "final-assignment"
|
30 |
+
space_id = f"{username}/{repo_name}"
|
31 |
+
|
32 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
33 |
+
try:
|
34 |
+
agent = BasicAgent()
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error instantiating agent: {e}")
|
37 |
+
return f"Error initializing agent: {e}", None
|
38 |
+
# 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)
|
39 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
40 |
+
print(agent_code)
|
41 |
+
|
42 |
+
# 2. Fetch Questions
|
43 |
+
print(f"Fetching questions from: {questions_url}")
|
44 |
+
try:
|
45 |
+
response = requests.get(questions_url, timeout=15)
|
46 |
+
response.raise_for_status()
|
47 |
+
questions_data = response.json()
|
48 |
+
if not questions_data:
|
49 |
+
print("Fetched questions list is empty.")
|
50 |
+
return "Fetched questions list is empty or invalid format.", None
|
51 |
+
print(f"Fetched {len(questions_data)} questions.")
|
52 |
+
except requests.exceptions.RequestException as e:
|
53 |
+
print(f"Error fetching questions: {e}")
|
54 |
+
return f"Error fetching questions: {e}", None
|
55 |
+
except requests.exceptions.JSONDecodeError as e:
|
56 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
57 |
+
print(f"Response text: {response.text[:500]}")
|
58 |
+
return f"Error decoding server response for questions: {e}", None
|
59 |
+
except Exception as e:
|
60 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
61 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
62 |
+
|
63 |
+
# 3. Run your Agent
|
64 |
+
results_log = []
|
65 |
+
answers_payload = []
|
66 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
67 |
+
for item in questions_data:
|
68 |
+
task_id = item.get("task_id")
|
69 |
+
question_text = item.get("question")
|
70 |
+
if not task_id or question_text is None:
|
71 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
72 |
+
continue
|
73 |
+
try:
|
74 |
+
submitted_answer = agent(question_text)
|
75 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
76 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Error running agent on task {task_id}: {e}")
|
79 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
80 |
+
|
81 |
+
if not answers_payload:
|
82 |
+
print("Agent did not produce any answers to submit.")
|
83 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
84 |
+
|
85 |
+
# 4. Prepare Submission
|
86 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
87 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
88 |
+
print(status_update)
|
89 |
+
print(answers_payload)
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
main()
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
gradio
|
2 |
requests
|
3 |
|
4 |
-
ycecream
|
|
|
|
|
|
1 |
gradio
|
2 |
requests
|
3 |
|
4 |
+
# ycecream
|
5 |
+
mcpadapt
|
6 |
+
smolagents@git+https://github.com/huggingface/smolagents.git
|