Ap98 commited on
Commit
6736fb6
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -11
app.py CHANGED
@@ -1,23 +1,135 @@
 
 
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 ---
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
  class BasicAgent:
14
  def __init__(self):
 
 
 
 
 
 
15
  print("BasicAgent initialized.")
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -76,16 +188,29 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
 
 
79
  if not task_id or question_text is None:
80
  print(f"Skipping item with missing task_id or question: {item}")
81
  continue
 
82
  try:
83
- submitted_answer = agent(question_text)
 
 
 
 
 
 
 
 
84
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
86
  except Exception as e:
87
- print(f"Error running agent on task {task_id}: {e}")
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
89
 
90
  if not answers_payload:
91
  print("Agent did not produce any answers to submit.")
@@ -146,11 +271,9 @@ with gr.Blocks() as demo:
146
  gr.Markdown(
147
  """
148
  **Instructions:**
149
-
150
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
151
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
152
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
153
-
154
  ---
155
  **Disclaimers:**
156
  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).
@@ -193,4 +316,4 @@ if __name__ == "__main__":
193
  print("-"*(60 + len(" App Starting ")) + "\n")
194
 
195
  print("Launching Gradio Interface for Basic Agent Evaluation...")
196
- demo.launch(debug=True, share=False)
 
1
+
2
+
3
  import os
4
+ import gradio as gr
5
  import requests
6
  import inspect
7
+ import pandas as pd
8
+ import time
9
+ import re
10
+ from markdownify import markdownify
11
+ from smolagents import Tool, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool, LiteLLMModel
12
 
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
+
18
+ class DownloadTaskAttachmentTool(Tool):
19
+ name = "download_file"
20
+ description = "Downloads the file attached to the task ID"
21
+ inputs = {'task_id': {'type': 'string', 'description': 'The task id to download attachment from.'}}
22
+ output_type = "string"
23
+
24
+ def forward(self, task_id: str) -> str:
25
+ """
26
+ Downloads a file associated with the given task ID.
27
+ Returns the file path where the file is saved locally.
28
+ """
29
+ file_url = f"{DEFAULT_API_URL}/files/{task_id}"
30
+ local_file_path = f"downloads/{task_id}.file"
31
+
32
+ print(f"Downloading file for task ID {task_id} from {file_url}...")
33
+ try:
34
+ response = requests.get(file_url, stream=True, timeout=15)
35
+ response.raise_for_status()
36
+
37
+ os.makedirs("downloads", exist_ok=True)
38
+ with open(local_file_path, "wb") as file:
39
+ for chunk in response.iter_content(chunk_size=8192):
40
+ file.write(chunk)
41
+
42
+ print(f"File downloaded successfully: {local_file_path}")
43
+ return local_file_path
44
+ except requests.exceptions.RequestException as e:
45
+ print(f"Error downloading file for task {task_id}: {e}")
46
+ raise
47
+
48
+ def __init__(self, *args, **kwargs):
49
+ self.is_initialized = False
50
+
51
+
52
+ class VisitWebpageTool(Tool):
53
+ name = "visit_webpage"
54
+ description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
55
+ inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
56
+ output_type = "string"
57
+
58
+ def forward(self, url: str) -> str:
59
+ try:
60
+ import requests
61
+ from markdownify import markdownify
62
+ from requests.exceptions import RequestException
63
+
64
+ from smolagents.utils import truncate_content
65
+ except ImportError as e:
66
+ raise ImportError(
67
+ "You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
68
+ ) from e
69
+ try:
70
+ # Send a GET request to the URL with a 20-second timeout
71
+ response = requests.get(url, timeout=20)
72
+ response.raise_for_status() # Raise an exception for bad status codes
73
+
74
+ # Convert the HTML content to Markdown
75
+ markdown_content = markdownify(response.text).strip()
76
+
77
+ # Remove multiple line breaks
78
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
79
+
80
+ return truncate_content(markdown_content, 10000)
81
+
82
+ except requests.exceptions.Timeout:
83
+ return "The request timed out. Please try again later or check the URL."
84
+ except RequestException as e:
85
+ return f"Error fetching the webpage: {str(e)}"
86
+ except Exception as e:
87
+ return f"An unexpected error occurred: {str(e)}"
88
+
89
+ def __init__(self, *args, **kwargs):
90
+ self.is_initialized = False
91
+
92
+
93
  # --- Basic Agent Definition ---
94
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
95
  class BasicAgent:
96
  def __init__(self):
97
+ self.agent = CodeAgent(
98
+ model=LiteLLMModel(model_id="openrouter/meta-llama/llama-4-maverick:free", api_key=os.getenv("OPENROUTER_KEY")),
99
+ tools=[DuckDuckGoSearchTool(), WikipediaSearchTool(), VisitWebpageTool(), DownloadTaskAttachmentTool()],
100
+ add_base_tools=True,
101
+ additional_authorized_imports=['pandas','numpy','csv','subprocess', 'exec']
102
+ )
103
  print("BasicAgent initialized.")
104
  def __call__(self, question: str) -> str:
105
  print(f"Agent received question (first 50 chars): {question[:50]}...")
106
+ agent_answer = self.agent.run(question)
107
+ print(f"Agent returning answer: {agent_answer}")
108
+ return agent_answer
109
+
110
+ def download_file(self, task_id: str) -> str:
111
+ """
112
+ Downloads a file associated with the given task ID.
113
+ Returns the file path where the file is saved locally.
114
+ """
115
+ file_url = f"{DEFAULT_API_URL}/files/{task_id}"
116
+ local_file_path = f"downloads/{task_id}.file"
117
+
118
+ print(f"Downloading file for task ID {task_id} from {file_url}...")
119
+ try:
120
+ response = requests.get(file_url, stream=True, timeout=15)
121
+ response.raise_for_status()
122
+
123
+ os.makedirs("downloads", exist_ok=True)
124
+ with open(local_file_path, "wb") as file:
125
+ for chunk in response.iter_content(chunk_size=8192):
126
+ file.write(chunk)
127
+
128
+ print(f"File downloaded successfully: {local_file_path}")
129
+ return local_file_path
130
+ except requests.exceptions.RequestException as e:
131
+ print(f"Error downloading file for task {task_id}: {e}")
132
+ raise
133
 
134
  def run_and_submit_all( profile: gr.OAuthProfile | None):
135
  """
 
188
  for item in questions_data:
189
  task_id = item.get("task_id")
190
  question_text = item.get("question")
191
+ requires_file = item.get("requires_file", False)
192
+
193
  if not task_id or question_text is None:
194
  print(f"Skipping item with missing task_id or question: {item}")
195
  continue
196
+
197
  try:
198
+ # Download file if required
199
+ if requires_file:
200
+ file_path = agent.download_file(task_id)
201
+ print(f"File for task {task_id} saved at: {file_path}")
202
+ # Optionally, pass the file path to the agent if needed
203
+ submitted_answer = agent(f"{question_text} (File: {file_path})")
204
+ else:
205
+ submitted_answer = agent(question_text)
206
+
207
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
208
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
209
+ time.sleep(2)
210
  except Exception as e:
211
+ print(f"Error running agent on task {task_id}: {e}")
212
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
213
+
214
 
215
  if not answers_payload:
216
  print("Agent did not produce any answers to submit.")
 
271
  gr.Markdown(
272
  """
273
  **Instructions:**
 
274
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
275
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
276
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
277
  ---
278
  **Disclaimers:**
279
  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).
 
316
  print("-"*(60 + len(" App Starting ")) + "\n")
317
 
318
  print("Launching Gradio Interface for Basic Agent Evaluation...")
319
+ demo.launch(debug=True, share=False)