Christophe DUC commited on
Commit
7058f6e
·
1 Parent(s): 81917a3

40% scoring overall : need to improve the usage of attached file in questions

Browse files
Files changed (2) hide show
  1. app.py +58 -1
  2. requirements.txt +5 -1
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -12,10 +13,27 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
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
 
@@ -36,6 +54,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
36
 
37
  api_url = DEFAULT_API_URL
38
  questions_url = f"{api_url}/questions"
 
 
 
39
  submit_url = f"{api_url}/submit"
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
@@ -52,8 +73,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
52
  print(f"Fetching questions from: {questions_url}")
53
  try:
54
  response = requests.get(questions_url, timeout=15)
 
55
  response.raise_for_status()
56
  questions_data = response.json()
 
 
 
57
  if not questions_data:
58
  print("Fetched questions list is empty.")
59
  return "Fetched questions list is empty or invalid format.", None
@@ -72,14 +97,46 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
72
  # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []
 
75
  print(f"Running agent on {len(questions_data)} questions...")
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})
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, VisitWebpageTool, OpenAIServerModel
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
 
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
+ print("Start BasicAgent initialization.")
17
+
18
+ #TODO : define your OpenAI API key
19
+ self.openai_key = os.getenv("OPENAI_API_KEY")
20
+
21
+ self.model = OpenAIServerModel(model_id="o4-mini", api_key=self.openai_key)
22
+
23
+ #TODO : change verbosity_level for debug
24
+ self.agent = CodeAgent(
25
+ tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
26
+ model=self.model,
27
+ additional_authorized_imports=["helium"],
28
+ max_steps=10,
29
+ verbosity_level=1,
30
+ )
31
+
32
  print("BasicAgent initialized.")
33
+
34
  def __call__(self, question: str) -> str:
35
  print(f"Agent received question (first 50 chars): {question[:50]}...")
36
+ fixed_answer = self.agent.run(question)
37
  print(f"Agent returning fixed answer: {fixed_answer}")
38
  return fixed_answer
39
 
 
54
 
55
  api_url = DEFAULT_API_URL
56
  questions_url = f"{api_url}/questions"
57
+ #TODO : use only one question for debug
58
+ #questions_url = f"{api_url}/random-question"
59
+
60
  submit_url = f"{api_url}/submit"
61
 
62
  # 1. Instantiate Agent ( modify this part to create your agent)
 
73
  print(f"Fetching questions from: {questions_url}")
74
  try:
75
  response = requests.get(questions_url, timeout=15)
76
+ print(f"response.json() = {response.json()}")
77
  response.raise_for_status()
78
  questions_data = response.json()
79
+ # Ensure questions_data is a list. If it's a single question (dict), wrap it in a list
80
+ if isinstance(questions_data, dict):
81
+ questions_data = [questions_data]
82
  if not questions_data:
83
  print("Fetched questions list is empty.")
84
  return "Fetched questions list is empty or invalid format.", None
 
97
  # 3. Run your Agent
98
  results_log = []
99
  answers_payload = []
100
+ print(f"questions_data = {questions_data}")
101
  print(f"Running agent on {len(questions_data)} questions...")
102
  for item in questions_data:
103
+ print(f"==================================================================")
104
+ print(f"================ Question {item.get('task_id')} ==================")
105
+ print(f"==================================================================")
106
  task_id = item.get("task_id")
107
  question_text = item.get("question")
108
+ file_name = item.get("file_name")
109
+
110
  if not task_id or question_text is None:
111
  print(f"Skipping item with missing task_id or question: {item}")
112
  continue
113
+
114
  try:
115
+ # Check if there's a file to download
116
+ if file_name:
117
+ print(f"Question has an attached file: {file_name}")
118
+ # Construct the file download URL
119
+ file_url = f"{api_url}/file/{task_id}"
120
+ try:
121
+ # Download the file
122
+ file_response = requests.get(file_url, timeout=30)
123
+ file_response.raise_for_status()
124
+
125
+ # Create temporary directory if it doesn't exist
126
+ os.makedirs("temp_files", exist_ok=True)
127
+
128
+ # Save the file locally
129
+ file_path = os.path.join("temp_files", file_name)
130
+ with open(file_path, "wb") as f:
131
+ f.write(file_response.content)
132
+
133
+ # Modify the question to include information about the attached file
134
+ question_text = f"{question_text}\n\nI've attached a file named '{file_name}' that you can find at the path: {file_path}"
135
+ print(f"Downloaded file to {file_path}")
136
+ except Exception as e:
137
+ print(f"Error downloading file for task {task_id}: {e}")
138
+ question_text = f"{question_text}\n\nNote: There was supposed to be an attached file named '{file_name}', but it couldn't be downloaded: {e}"
139
+
140
  submitted_answer = agent(question_text)
141
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
142
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
requirements.txt CHANGED
@@ -1,2 +1,6 @@
1
  gradio
2
- requests
 
 
 
 
 
1
  gradio
2
+ requests
3
+ gradio[oauth]
4
+ smolagents
5
+ duckduckgo_search
6
+ smolagents[openai]