jarguello76 commited on
Commit
37264ea
·
verified ·
1 Parent(s): 28747d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -158
app.py CHANGED
@@ -1,236 +1,162 @@
1
  import os
2
- import gradio as gr
3
  import requests
4
- import inspect
5
- import pandas as pd
6
  import gradio as gr
7
- import random
8
-
9
  from langchain_core.messages import HumanMessage
10
- from smolagents import GradioUI, CodeAgent, HfApiModel, Tool
11
 
12
- # Import our custom tools from their modules
 
13
  from langchain_community.tools import DuckDuckGoSearchRun
14
 
15
- # Initialize the Hugging Face model
16
- model = HfApiModel()
17
 
18
- # Initialize the web search tool
19
- search_tool = DuckDuckGoSearchRun()
20
 
21
- # Custom Tool Class
 
22
  class CustomSearchTool(Tool):
23
- def __init__(self, search_tool):
 
 
 
 
 
 
24
  super().__init__()
25
- self.search_tool = search_tool
26
- self.description = "A custom search tool that uses DuckDuckGo to perform web searches."
27
- self.name = "custom_search_tool"
28
 
29
- def run(self, query):
30
- return self.search_tool.run(query)
 
31
 
32
- # Wrap the search tool in your custom tool class
33
- custom_search_tool = CustomSearchTool(search_tool)
 
34
 
35
- # Initialize the agent with the custom tool
36
  agent = CodeAgent(
37
- tools=[custom_search_tool],
38
  model=model,
39
- add_base_tools=True, # Add any additional base tools
40
- planning_interval=3 # Enable planning every 3 steps
41
  )
42
 
43
- if __name__ == "__main__":
44
- GradioUI(agent).launch()
45
-
46
- # (Keep Constants as is)
47
- # --- Constants ---
48
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
49
 
50
- # --- Basic Agent Definition ---
51
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
52
  class BasicAgent:
53
  def __init__(self):
54
- print("BasicAgent initialized.")
 
 
55
  def __call__(self, question: str) -> str:
56
- print(f"Agent received question (first 50 chars): {question[:50]}...")
57
  messages = [HumanMessage(content=question)]
58
- messages = self.graph.invoke({"messages": messages})
59
- answer = messages['messages'][-1].content
60
- return answer[14:]
 
61
 
62
- print(f"Agent returning fixed answer: {answer}")
63
- return answer
64
 
65
- def run_and_submit_all( profile: gr.OAuthProfile | None):
66
- """
67
- Fetches all questions, runs the BasicAgent on them, submits all answers,
68
- and displays the results.
69
- """
70
- # --- Determine HF Space Runtime URL and Repo URL ---
71
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
72
 
73
  if profile:
74
- username= f"{profile.username}"
75
- print(f"User logged in: {username}")
76
  else:
77
- print("User not logged in.")
78
- return "Please Login to Hugging Face with the button.", None
79
 
80
  api_url = DEFAULT_API_URL
81
  questions_url = f"{api_url}/questions"
82
  submit_url = f"{api_url}/submit"
 
83
 
84
- # 1. Instantiate Agent ( modify this part to create your agent)
85
  try:
86
- agent = BasicAgent()
87
  except Exception as e:
88
- print(f"Error instantiating agent: {e}")
89
- return f"Error initializing agent: {e}", None
90
- # 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)
91
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
92
- print(agent_code)
93
 
94
- # 2. Fetch Questions
95
- print(f"Fetching questions from: {questions_url}")
96
  try:
97
  response = requests.get(questions_url, timeout=15)
98
  response.raise_for_status()
99
  questions_data = response.json()
100
- if not questions_data:
101
- print("Fetched questions list is empty.")
102
- return "Fetched questions list is empty or invalid format.", None
103
- print(f"Fetched {len(questions_data)} questions.")
104
- except requests.exceptions.RequestException as e:
105
- print(f"Error fetching questions: {e}")
106
- return f"Error fetching questions: {e}", None
107
- except requests.exceptions.JSONDecodeError as e:
108
- print(f"Error decoding JSON response from questions endpoint: {e}")
109
- print(f"Response text: {response.text[:500]}")
110
- return f"Error decoding server response for questions: {e}", None
111
  except Exception as e:
112
- print(f"An unexpected error occurred fetching questions: {e}")
113
- return f"An unexpected error occurred fetching questions: {e}", None
114
 
115
- # 3. Run your Agent
116
  results_log = []
117
  answers_payload = []
118
- print(f"Running agent on {len(questions_data)} questions...")
119
  for item in questions_data:
120
  task_id = item.get("task_id")
121
  question_text = item.get("question")
122
  if not task_id or question_text is None:
123
- print(f"Skipping item with missing task_id or question: {item}")
124
  continue
125
  try:
126
- submitted_answer = agent(question_text)
127
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
128
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
129
  except Exception as e:
130
- print(f"Error running agent on task {task_id}: {e}")
131
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
132
 
133
  if not answers_payload:
134
- print("Agent did not produce any answers to submit.")
135
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
136
 
137
- # 4. Prepare Submission
138
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
139
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
140
- print(status_update)
 
 
141
 
142
- # 5. Submit
143
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
144
  try:
145
  response = requests.post(submit_url, json=submission_data, timeout=60)
146
  response.raise_for_status()
147
  result_data = response.json()
 
148
  final_status = (
149
- f"Submission Successful!\n"
150
  f"User: {result_data.get('username')}\n"
151
- f"Overall Score: {result_data.get('score', 'N/A')}% "
152
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
153
  f"Message: {result_data.get('message', 'No message received.')}"
154
  )
155
- print("Submission successful.")
156
- results_df = pd.DataFrame(results_log)
157
- return final_status, results_df
158
- except requests.exceptions.HTTPError as e:
159
- error_detail = f"Server responded with status {e.response.status_code}."
160
- try:
161
- error_json = e.response.json()
162
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
163
- except requests.exceptions.JSONDecodeError:
164
- error_detail += f" Response: {e.response.text[:500]}"
165
- status_message = f"Submission Failed: {error_detail}"
166
- print(status_message)
167
- results_df = pd.DataFrame(results_log)
168
- return status_message, results_df
169
- except requests.exceptions.Timeout:
170
- status_message = "Submission Failed: The request timed out."
171
- print(status_message)
172
- results_df = pd.DataFrame(results_log)
173
- return status_message, results_df
174
- except requests.exceptions.RequestException as e:
175
- status_message = f"Submission Failed: Network error - {e}"
176
- print(status_message)
177
- results_df = pd.DataFrame(results_log)
178
- return status_message, results_df
179
  except Exception as e:
180
- status_message = f"An unexpected error occurred during submission: {e}"
181
- print(status_message)
182
- results_df = pd.DataFrame(results_log)
183
- return status_message, results_df
184
 
185
- # --- Build Gradio Interface using Blocks ---
186
  with gr.Blocks() as demo:
187
- gr.Markdown("# Basic Agent Evaluation Runner")
188
  gr.Markdown(
189
- """
190
- **Instructions:**
191
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
192
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
193
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
194
- ---
195
- **Disclaimers:**
196
- 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).
197
- 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.
198
- """
199
  )
200
 
201
  gr.LoginButton()
202
-
203
- run_button = gr.Button("Run Evaluation & Submit All Answers")
204
-
205
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
206
- # Removed max_rows=10 from DataFrame constructor
207
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
208
 
209
  run_button.click(
210
  fn=run_and_submit_all,
211
  outputs=[status_output, results_table]
212
  )
213
 
214
- if __name__ == "__main__":
215
- print("\n" + "-"*30 + " App Starting " + "-"*30)
216
- # Check for SPACE_HOST and SPACE_ID at startup for information
217
- space_host_startup = os.getenv("SPACE_HOST")
218
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
219
-
220
- if space_host_startup:
221
- print(f"✅ SPACE_HOST found: {space_host_startup}")
222
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
223
- else:
224
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
225
-
226
- if space_id_startup: # Print repo URLs if SPACE_ID is found
227
- print(f"✅ SPACE_ID found: {space_id_startup}")
228
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
229
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
230
- else:
231
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
232
-
233
- print("-"*(60 + len(" App Starting ")) + "\n")
234
 
235
- print("Launching Gradio Interface for Basic Agent Evaluation...")
236
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
 
1
  import os
 
2
  import requests
 
 
3
  import gradio as gr
4
+ import pandas as pd
 
5
  from langchain_core.messages import HumanMessage
 
6
 
7
+ from smolagents import GradioUI, CodeAgent, HfApiModel
8
+ from smolagents.tools import Tool
9
  from langchain_community.tools import DuckDuckGoSearchRun
10
 
 
 
11
 
12
+ # --- Constants ---
13
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
+
16
+ # --- Custom Tool using DuckDuckGo ---
17
  class CustomSearchTool(Tool):
18
+ name = "duckduckgo_search"
19
+ description = "Search the web for up-to-date information using DuckDuckGo."
20
+ inputs = ["query"]
21
+ outputs = ["result"]
22
+
23
+ def __init__(self):
24
+ self.search = DuckDuckGoSearchRun()
25
  super().__init__()
 
 
 
26
 
27
+ def run(self, query: str) -> str:
28
+ return self.search.run(query)
29
+
30
 
31
+ # --- Initialize the model and tools ---
32
+ model = HfApiModel()
33
+ search_tool = CustomSearchTool()
34
 
 
35
  agent = CodeAgent(
36
+ tools=[search_tool],
37
  model=model,
38
+ add_base_tools=True,
39
+ planning_interval=3,
40
  )
41
 
 
 
 
 
 
 
42
 
43
+ # --- Evaluation Agent Wrapper ---
 
44
  class BasicAgent:
45
  def __init__(self):
46
+ self.graph = agent
47
+ print("✅ BasicAgent initialized with SmolAgent.")
48
+
49
  def __call__(self, question: str) -> str:
50
+ print(f"Received question: {question}")
51
  messages = [HumanMessage(content=question)]
52
+ result = self.graph.invoke({"messages": messages})
53
+ answer = result['messages'][-1].content
54
+ print(f"Returning answer: {answer}")
55
+ return answer[14:] # optional slicing depending on model behavior
56
 
 
 
57
 
58
+ # --- Evaluation Logic ---
59
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
60
+ space_id = os.getenv("SPACE_ID")
 
 
 
 
61
 
62
  if profile:
63
+ username = profile.username
64
+ print(f"🔐 User logged in: {username}")
65
  else:
66
+ return "⚠️ Please log in to Hugging Face.", None
 
67
 
68
  api_url = DEFAULT_API_URL
69
  questions_url = f"{api_url}/questions"
70
  submit_url = f"{api_url}/submit"
71
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
72
 
 
73
  try:
74
+ agent_instance = BasicAgent()
75
  except Exception as e:
76
+ return f"Agent initialization error: {e}", None
 
 
 
 
77
 
78
+ # 1. Fetch questions
 
79
  try:
80
  response = requests.get(questions_url, timeout=15)
81
  response.raise_for_status()
82
  questions_data = response.json()
 
 
 
 
 
 
 
 
 
 
 
83
  except Exception as e:
84
+ return f"Error fetching questions: {e}", None
 
85
 
86
+ # 2. Run Agent
87
  results_log = []
88
  answers_payload = []
89
+
90
  for item in questions_data:
91
  task_id = item.get("task_id")
92
  question_text = item.get("question")
93
  if not task_id or question_text is None:
 
94
  continue
95
  try:
96
+ submitted_answer = agent_instance(question_text)
97
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
98
+ results_log.append({
99
+ "Task ID": task_id,
100
+ "Question": question_text,
101
+ "Submitted Answer": submitted_answer
102
+ })
103
  except Exception as e:
104
+ results_log.append({
105
+ "Task ID": task_id,
106
+ "Question": question_text,
107
+ "Submitted Answer": f"AGENT ERROR: {e}"
108
+ })
109
 
110
  if not answers_payload:
111
+ return "Agent did not produce answers.", pd.DataFrame(results_log)
 
112
 
113
+ # 3. Submit answers
114
+ submission_data = {
115
+ "username": username.strip(),
116
+ "agent_code": agent_code,
117
+ "answers": answers_payload
118
+ }
119
 
 
 
120
  try:
121
  response = requests.post(submit_url, json=submission_data, timeout=60)
122
  response.raise_for_status()
123
  result_data = response.json()
124
+
125
  final_status = (
126
+ f"Submission Successful!\n"
127
  f"User: {result_data.get('username')}\n"
128
+ f"Score: {result_data.get('score', 'N/A')}%\n"
129
+ f"Correct: {result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')}\n"
130
  f"Message: {result_data.get('message', 'No message received.')}"
131
  )
132
+ return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  except Exception as e:
134
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
135
+
 
 
136
 
137
+ # --- Gradio Interface ---
138
  with gr.Blocks() as demo:
139
+ gr.Markdown("## 🧠 SmolAgent Evaluation App")
140
  gr.Markdown(
141
+ "Log in with Hugging Face and click the button to fetch questions, run the agent, and submit answers."
 
 
 
 
 
 
 
 
 
142
  )
143
 
144
  gr.LoginButton()
145
+ run_button = gr.Button("▶️ Run Evaluation & Submit All Answers")
146
+ status_output = gr.Textbox(label="Submission Status", lines=5, interactive=False)
147
+ results_table = gr.DataFrame(label="Agent Answers")
 
 
 
148
 
149
  run_button.click(
150
  fn=run_and_submit_all,
151
  outputs=[status_output, results_table]
152
  )
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ # --- App Launch ---
156
+ if __name__ == "__main__":
157
+ print("🚀 Launching app...")
158
+ space_id = os.getenv("SPACE_ID")
159
+ if space_id:
160
+ print(f"HF Space ID: {space_id}")
161
+ print(f"Repo: https://huggingface.co/spaces/{space_id}/tree/main")
162
+ demo.launch(debug=True)