dlaima commited on
Commit
4dd855b
·
verified ·
1 Parent(s): 5aee165

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -13
app.py CHANGED
@@ -8,7 +8,6 @@ from smolagents.models import OpenAIServerModel
8
 
9
  from tools import WikipediaTool, WikipediaSearchTool
10
 
11
-
12
  # Define the system prompt
13
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
14
  Report your thoughts, and finish your answer with the following template:
@@ -26,16 +25,27 @@ class PatchedOpenAIServerModel(OpenAIServerModel):
26
  messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
27
  else:
28
  raise TypeError("Expected 'messages' to be a list of message dicts")
29
-
30
  return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
31
 
32
  class MyAgent:
33
  def __init__(self):
34
- self.model = PatchedOpenAIServerModel(model_id="gpt-4-turbo") #to be changed gpt-4
35
- self.agent = CodeAgent(tools=[DuckDuckGoSearchTool(), WikipediaSearchTool(), WikipediaTool()], model=self.model) #add a new tool
36
-
37
- def __call__(self, question: str) -> str:
38
- return self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  def run_and_submit_all(profile: gr.OAuthProfile | None):
41
  space_id = os.getenv("SPACE_ID")
@@ -76,21 +86,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
76
  print(f"Running agent on {len(questions_data)} questions...")
77
  for item in questions_data:
78
  task_id = item.get("task_id")
79
- question_text = item.get("question")
80
- if not task_id or question_text is None:
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
  error_msg = f"AGENT ERROR: {e}"
88
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
 
 
 
 
89
 
90
  if not answers_payload:
91
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
92
 
93
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
94
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
95
  try:
96
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
8
 
9
  from tools import WikipediaTool, WikipediaSearchTool
10
 
 
11
  # Define the system prompt
12
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
13
  Report your thoughts, and finish your answer with the following template:
 
25
  messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
26
  else:
27
  raise TypeError("Expected 'messages' to be a list of message dicts")
 
28
  return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
29
 
30
  class MyAgent:
31
  def __init__(self):
32
+ self.model = PatchedOpenAIServerModel(model_id="gpt-4-turbo")
33
+ self.agent = CodeAgent(tools=[
34
+ DuckDuckGoSearchTool(),
35
+ WikipediaSearchTool(),
36
+ WikipediaTool()
37
+ ], model=self.model)
38
+
39
+ def __call__(self, task: dict) -> str:
40
+ question_text = task.get("question", "")
41
+
42
+ # Merge any code or attachment content if available
43
+ if "code" in task:
44
+ question_text += f"\n\nAttached code:\n{task['code']}"
45
+ elif "attachment" in task:
46
+ question_text += f"\n\nAttached content:\n{task['attachment']}"
47
+
48
+ return self.agent.run(question_text)
49
 
50
  def run_and_submit_all(profile: gr.OAuthProfile | None):
51
  space_id = os.getenv("SPACE_ID")
 
86
  print(f"Running agent on {len(questions_data)} questions...")
87
  for item in questions_data:
88
  task_id = item.get("task_id")
89
+ if not task_id:
 
90
  continue
91
  try:
92
+ submitted_answer = agent(item)
93
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
94
+ results_log.append({
95
+ "Task ID": task_id,
96
+ "Question": item.get("question", ""),
97
+ "Submitted Answer": submitted_answer
98
+ })
99
  except Exception as e:
100
  error_msg = f"AGENT ERROR: {e}"
101
+ results_log.append({
102
+ "Task ID": task_id,
103
+ "Question": item.get("question", ""),
104
+ "Submitted Answer": error_msg
105
+ })
106
 
107
  if not answers_payload:
108
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
109
 
110
+ submission_data = {
111
+ "username": username.strip(),
112
+ "agent_code": agent_code,
113
+ "answers": answers_payload
114
+ }
115
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
116
  try:
117
  response = requests.post(submit_url, json=submission_data, timeout=60)