DeDeckerThomas commited on
Commit
bc3a370
·
1 Parent(s): 36f4302

Add first agent version: testing

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +199 -0
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ sandbox/
2
+ .env
app.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, OpenAIServerModel
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
+
14
+ class GAIAAgent:
15
+ def __init__(self) -> None:
16
+ self.model = OpenAIServerModel(
17
+ model_id="gpt-4.1-mini",
18
+ api_key=os.getenv("OPENAI_API_KEY")
19
+ )
20
+
21
+ def __call__(self, question: str) -> str:
22
+ agent = CodeAgent(tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()], model=self.model)
23
+ return agent.run(question)
24
+
25
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
26
+ """
27
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
28
+ and displays the results.
29
+ """
30
+ # --- Determine HF Space Runtime URL and Repo URL ---
31
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
32
+
33
+ if profile:
34
+ username= f"{profile.username}"
35
+ print(f"User logged in: {username}")
36
+ else:
37
+ print("User not logged in.")
38
+ return "Please Login to Hugging Face with the button.", None
39
+
40
+ api_url = DEFAULT_API_URL
41
+ questions_url = f"{api_url}/questions"
42
+ submit_url = f"{api_url}/submit"
43
+
44
+ # 1. Instantiate Agent ( modify this part to create your agent)
45
+ try:
46
+ agent = GAIAAgent()
47
+ except Exception as e:
48
+ print(f"Error instantiating agent: {e}")
49
+ return f"Error initializing agent: {e}", None
50
+ # 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)
51
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
52
+ print(agent_code)
53
+
54
+ # 2. Fetch Questions
55
+ print(f"Fetching questions from: {questions_url}")
56
+ try:
57
+ response = requests.get(questions_url, timeout=15)
58
+ response.raise_for_status()
59
+ questions_data = response.json()
60
+ if not questions_data:
61
+ print("Fetched questions list is empty.")
62
+ return "Fetched questions list is empty or invalid format.", None
63
+ print(f"Fetched {len(questions_data)} questions.")
64
+ except requests.exceptions.RequestException as e:
65
+ print(f"Error fetching questions: {e}")
66
+ return f"Error fetching questions: {e}", None
67
+ except requests.exceptions.JSONDecodeError as e:
68
+ print(f"Error decoding JSON response from questions endpoint: {e}")
69
+ print(f"Response text: {response.text[:500]}")
70
+ return f"Error decoding server response for questions: {e}", None
71
+ except Exception as e:
72
+ print(f"An unexpected error occurred fetching questions: {e}")
73
+ return f"An unexpected error occurred fetching questions: {e}", None
74
+
75
+ # 3. Run your Agent
76
+ results_log = []
77
+ answers_payload = []
78
+ print(f"Running agent on {len(questions_data)} questions...")
79
+ for item in questions_data:
80
+ task_id = item.get("task_id")
81
+ question_text = item.get("question")
82
+ if not task_id or question_text is None:
83
+ print(f"Skipping item with missing task_id or question: {item}")
84
+ continue
85
+ try:
86
+ submitted_answer = agent(question_text)
87
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
88
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
89
+ except Exception as e:
90
+ print(f"Error running agent on task {task_id}: {e}")
91
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
92
+
93
+ if not answers_payload:
94
+ print("Agent did not produce any answers to submit.")
95
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
96
+
97
+ # 4. Prepare Submission
98
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
99
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
100
+ print(status_update)
101
+
102
+ # 5. Submit
103
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
104
+ try:
105
+ response = requests.post(submit_url, json=submission_data, timeout=60)
106
+ response.raise_for_status()
107
+ result_data = response.json()
108
+ final_status = (
109
+ f"Submission Successful!\n"
110
+ f"User: {result_data.get('username')}\n"
111
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
112
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
113
+ f"Message: {result_data.get('message', 'No message received.')}"
114
+ )
115
+ print("Submission successful.")
116
+ results_df = pd.DataFrame(results_log)
117
+ return final_status, results_df
118
+ except requests.exceptions.HTTPError as e:
119
+ error_detail = f"Server responded with status {e.response.status_code}."
120
+ try:
121
+ error_json = e.response.json()
122
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
123
+ except requests.exceptions.JSONDecodeError:
124
+ error_detail += f" Response: {e.response.text[:500]}"
125
+ status_message = f"Submission Failed: {error_detail}"
126
+ print(status_message)
127
+ results_df = pd.DataFrame(results_log)
128
+ return status_message, results_df
129
+ except requests.exceptions.Timeout:
130
+ status_message = "Submission Failed: The request timed out."
131
+ print(status_message)
132
+ results_df = pd.DataFrame(results_log)
133
+ return status_message, results_df
134
+ except requests.exceptions.RequestException as e:
135
+ status_message = f"Submission Failed: Network error - {e}"
136
+ print(status_message)
137
+ results_df = pd.DataFrame(results_log)
138
+ return status_message, results_df
139
+ except Exception as e:
140
+ status_message = f"An unexpected error occurred during submission: {e}"
141
+ print(status_message)
142
+ results_df = pd.DataFrame(results_log)
143
+ return status_message, results_df
144
+
145
+
146
+ # --- Build Gradio Interface using Blocks ---
147
+ with gr.Blocks() as demo:
148
+ gr.Markdown("# Basic Agent Evaluation Runner")
149
+ gr.Markdown(
150
+ """
151
+ **Instructions:**
152
+
153
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
154
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
155
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
156
+
157
+ ---
158
+ **Disclaimers:**
159
+ 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).
160
+ 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.
161
+ """
162
+ )
163
+
164
+ gr.LoginButton()
165
+
166
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
167
+
168
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
169
+ # Removed max_rows=10 from DataFrame constructor
170
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
171
+
172
+ run_button.click(
173
+ fn=run_and_submit_all,
174
+ outputs=[status_output, results_table]
175
+ )
176
+
177
+ if __name__ == "__main__":
178
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
179
+ # Check for SPACE_HOST and SPACE_ID at startup for information
180
+ space_host_startup = os.getenv("SPACE_HOST")
181
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
182
+
183
+ if space_host_startup:
184
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
185
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
186
+ else:
187
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
188
+
189
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
190
+ print(f"✅ SPACE_ID found: {space_id_startup}")
191
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
192
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
193
+ else:
194
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
195
+
196
+ print("-"*(60 + len(" App Starting ")) + "\n")
197
+
198
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
199
+ demo.launch(debug=True, share=False)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests