kingkw1 commited on
Commit
9815d83
·
verified ·
1 Parent(s): 6219a6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -4
app.py CHANGED
@@ -24,11 +24,34 @@ llm = HuggingFaceEndpoint(
24
  huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),
25
  )
26
 
 
27
  chat = ChatHuggingFace(llm=llm, verbose=True)
28
 
 
29
  search_tool = DuckDuckGoSearchRun()
30
 
31
- tools = [search_tool]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  chat_with_tools = chat.bind_tools(tools)
33
 
34
  class AgentState(TypedDict):
@@ -39,11 +62,13 @@ def assistant(state:AgentState):
39
  "messages": [chat_with_tools.invoke(state["messages"])]
40
  }
41
 
42
- # Graph
43
  builder = StateGraph(AgentState)
 
44
  # Graph Nodes
45
  builder.add_node("assistant", assistant)
46
  builder.add_node("tools", ToolNode(tools))
 
47
  #Graph Edges
48
  builder.add_edge(START, "assistant")
49
  builder.add_conditional_edges(
@@ -51,6 +76,8 @@ builder.add_conditional_edges(
51
  tools_condition
52
  )
53
  builder.add_edge("tools", "assistant")
 
 
54
  my_agent = builder.compile()
55
 
56
  class BasicAgent:
@@ -58,8 +85,25 @@ class BasicAgent:
58
  print("BasicAgent initialized.")
59
  def __call__(self, question: str) -> str:
60
  print(f"Agent received question (first 50 chars): {question[:50]}...")
61
- messages = [HumanMessage(content=question)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  response_state = my_agent.invoke({"messages": messages})
 
 
63
  final_answer = response_state["messages"][-1].content
64
  return final_answer
65
 
@@ -120,11 +164,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
120
  for item in questions_data:
121
  task_id = item.get("task_id")
122
  question_text = item.get("question")
 
 
123
  if not task_id or question_text is None:
124
  print(f"Skipping item with missing task_id or question: {item}")
125
  continue
126
  try:
127
- submitted_answer = agent(question_text)
128
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
129
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
130
  except Exception as e:
 
24
  huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),
25
  )
26
 
27
+ system_prompt = """You are an AI assistant taking the GAIA benchmark. You must output ONLY the final answer. Do not include any explanations, conversational text or formatting. If the answer is a number, output just the number. If it is a list, output a comma-separated list."""
28
  chat = ChatHuggingFace(llm=llm, verbose=True)
29
 
30
+ # Tool Definition
31
  search_tool = DuckDuckGoSearchRun()
32
 
33
+ @tool
34
+ def python_repl(code:str) -> str:
35
+ """
36
+ Executes Python code and returns the standard output.
37
+ Use this to read files (like CSVs or Excel), process data with pandas or do math.
38
+ CRITICAL: You MUST use print() to output the final result so it can be captured.
39
+ """
40
+
41
+ old_stdout = sys.stdout
42
+ redirected_output = sys.stdout = StringIO()
43
+
44
+ try:
45
+ # Execute the code in the global namespace
46
+ exec(code, globals())
47
+ sys.stdout = old_stdoutoutput = redirected_output.getvalue()
48
+ return output if output else "Code executed successfully, but printed nothing. Use print() to see output."
49
+ except Exception as e:
50
+ sys.stdout = old_stdout
51
+ return f"Error executing code: {e}"
52
+
53
+ # Tools Instantiation
54
+ tools = [search_tool, python_repl]
55
  chat_with_tools = chat.bind_tools(tools)
56
 
57
  class AgentState(TypedDict):
 
62
  "messages": [chat_with_tools.invoke(state["messages"])]
63
  }
64
 
65
+ # Graph Instantiation
66
  builder = StateGraph(AgentState)
67
+
68
  # Graph Nodes
69
  builder.add_node("assistant", assistant)
70
  builder.add_node("tools", ToolNode(tools))
71
+
72
  #Graph Edges
73
  builder.add_edge(START, "assistant")
74
  builder.add_conditional_edges(
 
76
  tools_condition
77
  )
78
  builder.add_edge("tools", "assistant")
79
+
80
+ # Agent Compile
81
  my_agent = builder.compile()
82
 
83
  class BasicAgent:
 
85
  print("BasicAgent initialized.")
86
  def __call__(self, question: str) -> str:
87
  print(f"Agent received question (first 50 chars): {question[:50]}...")
88
+
89
+ # 1. Construct the prompt with the file path if it exists
90
+ if file_name:
91
+ # Note: You may need to adjust the path depending on where your space saves downloaded files.
92
+ # Usually, the grading space provides the file name, and it sits in the same directory.
93
+ prompt = f"Question: {question}\nAttached File: {file_name}\n\nUse the python_repl tool to read and analyze this file using pandas."
94
+ else:
95
+ prompt = f"Question: {question}"
96
+
97
+ # Inject the strict GAIA System Prompt and the Human Prompt
98
+ messages = [
99
+ SystemMessage(content=system_prompt),
100
+ HumanMessage(content=question)
101
+ ]
102
+
103
+ # Invoke Agent
104
  response_state = my_agent.invoke({"messages": messages})
105
+
106
+ # Final answer
107
  final_answer = response_state["messages"][-1].content
108
  return final_answer
109
 
 
164
  for item in questions_data:
165
  task_id = item.get("task_id")
166
  question_text = item.get("question")
167
+ file_name = item.get("file_name")
168
+
169
  if not task_id or question_text is None:
170
  print(f"Skipping item with missing task_id or question: {item}")
171
  continue
172
  try:
173
+ submitted_answer = agent(question_text, file_name)
174
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
175
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
176
  except Exception as e: