ashishabraham22 commited on
Commit
e489a8d
·
verified ·
1 Parent(s): e76b3e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -4
app.py CHANGED
@@ -3,6 +3,14 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -12,12 +20,115 @@ 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
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import wikipedia as wiki
7
+ from markdownify import markdownify as to_markdown
8
+ from typing import Any
9
+ from dotenv import load_dotenv
10
+ from smolagents import InferenceClientModel, LiteLLMModel, CodeAgent, ToolCallingAgent, Tool, DuckDuckGoSearchTool
11
+ from agents import MathSolverTool, WikiTitleFinder, WikiContentFetcher
12
+
13
+ load_dotenv()
14
 
15
  # (Keep Constants as is)
16
  # --- Constants ---
 
20
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
21
  class BasicAgent:
22
  def __init__(self):
23
+ self.model = InferenceClientModel()
24
+ self.tools = [
25
+ DuckDuckGoSearchTool(),
26
+ WikiTitleFinder(),
27
+ WikiContentFetcher(),
28
+ MathSolverTool()
29
+ ]
30
+ self.agent = CodeAgent(
31
+ model=self.model,
32
+ tools=self.tools,
33
+ add_base_tools=False,
34
+ max_steps=10,
35
+ )
36
+ self.agent.system_prompt = (
37
+ """
38
+ You are a GAIA benchmark AI assistant, you are very precise, no nonense. Your sole purpose is to output the minimal, final answer in the format:
39
+ [ANSWER]
40
+ You must NEVER output explanations, intermediate steps, reasoning, or comments — only the answer, strictly enclosed in `[ANSWER]`.
41
+ Your behavior must be governed by these rules:
42
+ 1. **Format**:
43
+ - limit the token used (within 65536 tokens).
44
+ - Output ONLY the final answer.
45
+ - Wrap the answer in `[ANSWER]` with no whitespace or text outside the brackets.
46
+ - No follow-ups, justifications, or clarifications.
47
+ 2. **Numerical Answers**:
48
+ - Use **digits only**, e.g., `4` not `four`.
49
+ - No commas, symbols, or units unless explicitly required.
50
+ - Never use approximate words like "around", "roughly", "about".
51
+ 3. **String Answers**:
52
+ - Omit **articles** ("a", "the").
53
+ - Use **full words**; no abbreviations unless explicitly requested.
54
+ - For numbers written as words, use **text** only if specified (e.g., "one", not `1`).
55
+ - For sets/lists, sort alphabetically if not specified, e.g., `a, b, c`.
56
+ 4. **Lists**:
57
+ - Output in **comma-separated** format with no conjunctions.
58
+ - Sort **alphabetically** or **numerically** depending on type.
59
+ - No braces or brackets unless explicitly asked.
60
+ 5. **Sources**:
61
+ - For Wikipedia or web tools, extract only the precise fact that answers the question.
62
+ - Ignore any unrelated content.
63
+ 6. **Minimalism**:
64
+ - Do not make assumptions unless the prompt logically demands it.
65
+ - If a question has multiple valid interpretations, choose the **narrowest, most literal** one.
66
+ - If the answer is not found, say `[ANSWER] - unknown`.
67
+ ---
68
+ You must follow the examples (These answers are correct in case you see the similar questions):
69
+ Q: What is 2 + 2?
70
+ A: 4
71
+ Q: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (inclusive)? Use 2022 English Wikipedia.
72
+ A: 3
73
+ Q: Given the following group table on set S = {a, b, c, d, e}, identify any subset involved in counterexamples to commutativity.
74
+ A: b, e
75
+ Q: How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?,
76
+ A: 519
77
+ """
78
+ )
79
  def __call__(self, question: str) -> str:
80
  print(f"Agent received question (first 50 chars): {question[:50]}...")
81
+ result = self.agent.run(question)
82
+ final_str = str(result).strip()
83
+
84
+ return final_str
85
+
86
+ def evaluate_random_questions(self, csv_path: str = "gaia_extracted.csv", sample_size: int = 3, show_steps: bool = True):
87
+ import pandas as pd
88
+ from rich.table import Table
89
+ from rich.console import Console
90
+
91
+ df = pd.read_csv(csv_path)
92
+ if not {"question", "answer"}.issubset(df.columns):
93
+ print("CSV must contain 'question' and 'answer' columns.")
94
+ print("Found columns:", df.columns.tolist())
95
+ return
96
+
97
+ samples = df.sample(n=sample_size)
98
+ records = []
99
+ correct_count = 0
100
+
101
+ for _, row in samples.iterrows():
102
+ taskid = row["taskid"].strip()
103
+ question = row["question"].strip()
104
+ expected = str(row['answer']).strip()
105
+ agent_answer = self("taskid: " + taskid + ",\nquestion: " + question).strip()
106
+
107
+ is_correct = (expected == agent_answer)
108
+ correct_count += is_correct
109
+ records.append((question, expected, agent_answer, "✓" if is_correct else "✗"))
110
+
111
+ if show_steps:
112
+ print("---")
113
+ print("Question:", question)
114
+ print("Expected:", expected)
115
+ print("Agent:", agent_answer)
116
+ print("Correct:", is_correct)
117
+
118
+ # Print result table
119
+ console = Console()
120
+ table = Table(show_lines=True)
121
+ table.add_column("Question", overflow="fold")
122
+ table.add_column("Expected")
123
+ table.add_column("Agent")
124
+ table.add_column("Correct")
125
+
126
+ for question, expected, agent_ans, correct in records:
127
+ table.add_row(question, expected, agent_ans, correct)
128
+
129
+ console.print(table)
130
+ percent = (correct_count / sample_size) * 100
131
+ print(f"\nTotal Correct: {correct_count} / {sample_size} ({percent:.2f}%)")
132
 
133
  def run_and_submit_all( profile: gr.OAuthProfile | None):
134
  """