dawid-lorek commited on
Commit
f8e24f8
·
verified ·
1 Parent(s): 2f5e8a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -7,6 +7,7 @@ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
7
 
8
  # Constants
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
10
 
11
  # --- Agent Definition using smolagents ---
12
  class SmartGAIAAgent:
@@ -16,15 +17,16 @@ class SmartGAIAAgent:
16
  raise ValueError("Missing OPENAI_API_KEY")
17
  self.model = OpenAIServerModel(model_id="gpt-4", api_key=self.api_key)
18
 
19
- # Agent with DuckDuckGo and code execution tool
20
  self.agent = CodeAgent(
21
  tools=[DuckDuckGoSearchTool()],
22
  model=self.model,
23
- add_base_tools=True # adds code interpreter and other built-ins
24
  )
25
 
26
  def __call__(self, question: str) -> str:
27
  try:
 
28
  result = self.agent.run(question)
29
  return result.strip()
30
  except Exception as e:
@@ -64,8 +66,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
64
  for item in questions_data:
65
  task_id = item.get("task_id")
66
  question_text = item.get("question")
 
 
67
  if not task_id or not question_text:
68
  continue
 
 
 
 
 
 
 
69
  try:
70
  submitted_answer = agent(question_text)
71
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
@@ -95,10 +106,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
95
  response.raise_for_status()
96
  result_data = response.json()
97
  final_status = (
98
- f"Submission Successful!\n"
99
- f"User: {result_data.get('username')}\n"
100
  f"Score: {result_data.get('score')}% "
101
- f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\n"
102
  f"Message: {result_data.get('message')}"
103
  )
104
  return final_status, pd.DataFrame(results_log)
 
7
 
8
  # Constants
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
+ MAX_QUESTION_LENGTH = 4000 # to avoid GPT-4 8k token limit
11
 
12
  # --- Agent Definition using smolagents ---
13
  class SmartGAIAAgent:
 
17
  raise ValueError("Missing OPENAI_API_KEY")
18
  self.model = OpenAIServerModel(model_id="gpt-4", api_key=self.api_key)
19
 
20
+ # Agent with DuckDuckGo + built-in Python interpreter
21
  self.agent = CodeAgent(
22
  tools=[DuckDuckGoSearchTool()],
23
  model=self.model,
24
+ add_base_tools=True
25
  )
26
 
27
  def __call__(self, question: str) -> str:
28
  try:
29
+ question = question[:MAX_QUESTION_LENGTH]
30
  result = self.agent.run(question)
31
  return result.strip()
32
  except Exception as e:
 
66
  for item in questions_data:
67
  task_id = item.get("task_id")
68
  question_text = item.get("question")
69
+
70
+ # Skip invalid or long/multimodal questions
71
  if not task_id or not question_text:
72
  continue
73
+ if len(question_text) > MAX_QUESTION_LENGTH:
74
+ print(f"Skipping long question: {task_id}")
75
+ continue
76
+ if any(keyword in question_text.lower() for keyword in ['attached', '.mp3', '.wav', '.png', '.jpg', 'image']):
77
+ print(f"Skipping file/audio/image question: {task_id}")
78
+ continue
79
+
80
  try:
81
  submitted_answer = agent(question_text)
82
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
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"Score: {result_data.get('score')}% "
112
+ f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})\\n"
113
  f"Message: {result_data.get('message')}"
114
  )
115
  return final_status, pd.DataFrame(results_log)