huytofu92 commited on
Commit
97f889e
·
1 Parent(s): b0f8ecb

Save dataset to huggingface

Browse files
Files changed (1) hide show
  1. app.py +60 -6
app.py CHANGED
@@ -2,11 +2,10 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
  from mini_agents import master_agent
6
  from utils import get_full_file_path
7
- import subprocess
8
-
9
- subprocess.run(["playwright", "install"])
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
@@ -14,13 +13,63 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  class BasicAgent:
18
  def __init__(self):
19
  self.agent = master_agent
20
  print("Master Agent initialized.")
21
- def __call__(self, question: str) -> str:
22
  print(f"Agent received question (first 50 chars): {question[:50]}...")
23
  fixed_answer = self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  print(f"Agent returning fixed answer: {fixed_answer}")
25
  return fixed_answer
26
 
@@ -90,7 +139,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, mock_submission: bool =
90
  try:
91
  if file_path:
92
  question_text = question_text + f"\n\nHere is also the path to the file for the task (file name matches with task ID and is not in plain English): {file_path}"
93
- submitted_answer = agent(question_text)
94
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
95
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
96
  except Exception as e:
@@ -106,7 +155,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None, mock_submission: bool =
106
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
107
  print(status_update)
108
 
109
- # 5. Submit
 
 
 
 
 
110
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
111
  if mock_submission:
112
  answer_df = pd.DataFrame(results_log, columns=["Task ID", "Question", "Submitted Answer"])
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ import datasets
6
  from mini_agents import master_agent
7
  from utils import get_full_file_path
8
+ from smolagents.memory import ActionStep, PlanningStep, TaskStep, SystemPromptStep, FinalAnswerStep
 
 
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
 
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
+ columns = [
17
+ 'task_id',
18
+ 'step_class',
19
+ # Common attributes (from MemoryStep base class)
20
+ 'model_input_messages',
21
+ 'tool_calls',
22
+ 'start_time',
23
+ 'end_time',
24
+ 'step_number',
25
+ 'error',
26
+ 'duration',
27
+ 'model_output_message',
28
+ 'model_output',
29
+ 'observations',
30
+ 'observations_images',
31
+ 'action_output',
32
+ # PlanningStep attributes
33
+ 'plan',
34
+ # TaskStep attributes
35
+ 'task',
36
+ 'task_images',
37
+ # SystemPromptStep attributes
38
+ 'system_prompt',
39
+ # FinalAnswerStep attributes
40
+ 'final_answer'
41
+ ]
42
+ df_agent_steps = pd.DataFrame(columns=columns)
43
+
44
  class BasicAgent:
45
  def __init__(self):
46
  self.agent = master_agent
47
  print("Master Agent initialized.")
48
+ def __call__(self, question: str, task_id: str) -> str:
49
  print(f"Agent received question (first 50 chars): {question[:50]}...")
50
  fixed_answer = self.agent.run(question)
51
+ all_steps = self.agent.memory.get_full_steps()
52
+ for step in all_steps:
53
+ if isinstance(step, ActionStep):
54
+ step_class = "ActionStep"
55
+ elif isinstance(step, PlanningStep):
56
+ step_class = "PlanningStep"
57
+ elif isinstance(step, TaskStep):
58
+ step_class = "TaskStep"
59
+ elif isinstance(step, SystemPromptStep):
60
+ step_class = "SystemPromptStep"
61
+ elif isinstance(step, FinalAnswerStep):
62
+ step_class = "FinalAnswerStep"
63
+ else:
64
+ step_class = "UnknownStep"
65
+
66
+ step_dict = step.dict()
67
+ df_agent_steps.loc[len(df_agent_steps)] = None
68
+ df_agent_steps.at[len(df_agent_steps), 'task_id'] = task_id
69
+ df_agent_steps.at[len(df_agent_steps), 'step_class'] = step_class
70
+ for key, value in step_dict.items():
71
+ df_agent_steps.at[len(df_agent_steps), key] = value
72
+
73
  print(f"Agent returning fixed answer: {fixed_answer}")
74
  return fixed_answer
75
 
 
139
  try:
140
  if file_path:
141
  question_text = question_text + f"\n\nHere is also the path to the file for the task (file name matches with task ID and is not in plain English): {file_path}"
142
+ submitted_answer = agent(question_text, task_id)
143
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
144
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
145
  except Exception as e:
 
155
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
156
  print(status_update)
157
 
158
+ # 5. Save steps data to huggingface dataset
159
+ print("Commiting steps data to huggingface dataset...")
160
+ dataset = datasets.Dataset.from_pandas(df_agent_steps)
161
+ dataset.push_to_hub("huytofu92/agent_steps_huggingface_course_unit4")
162
+ print("Agent steps data committed to huggingface dataset.")
163
+ # 6. Submit
164
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
165
  if mock_submission:
166
  answer_df = pd.DataFrame(results_log, columns=["Task ID", "Question", "Submitted Answer"])