sirine1712 commited on
Commit
30881f0
·
verified ·
1 Parent(s): 2766a21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -88
app.py CHANGED
@@ -5,20 +5,13 @@ import pandas as pd
5
  import math
6
 
7
  from smolagents import ToolCallingAgent, tool
8
- from instructor import OpenAIChat
9
  from duckduckgo_search import DDGS
10
 
11
  # --- Tools ---
12
  @tool
13
  def web_search(query: str) -> str:
14
- """Performs a web search using DuckDuckGo.
15
-
16
- Args:
17
- query: The search query string.
18
-
19
- Returns:
20
- A formatted string with search results.
21
- """
22
  try:
23
  with DDGS() as ddgs:
24
  results = ddgs.text(query, max_results=3)
@@ -27,114 +20,76 @@ def web_search(query: str) -> str:
27
  for r in results
28
  ) if results else "No results found."
29
  except Exception as e:
30
- return f"Search error: {str(e)}"
31
 
32
  @tool
33
  def calculate(expression: str) -> str:
34
- """Evaluates mathematical expressions.
35
-
36
- Args:
37
- expression: The math expression to evaluate.
38
-
39
- Returns:
40
- The result as a string or error message.
41
- """
42
  try:
43
  safe_dict = {k: v for k, v in math.__dict__.items() if not k.startswith("__")}
44
- safe_dict.update({
45
- '__builtins__': None,
46
- 'abs': abs,
47
- 'round': round
48
- })
49
  result = eval(expression, {"__builtins__": None}, safe_dict)
50
  return str(result)
51
  except Exception as e:
52
- return f"Calculation error: {str(e)}"
53
 
54
  # --- Agent ---
55
  class GAIAAgent:
56
  def __init__(self):
57
- self.system_prompt = """You are an AI assistant that answers questions using tools:
58
- - Use web_search for factual queries
59
- - Use calculate for math problems
60
- - Be concise and accurate"""
61
-
62
- try:
63
- self.agent = ToolCallingAgent(
64
- name="GAIA_Agent",
65
- description=self.system_prompt,
66
- tools=[web_search, calculate],
67
- model=OpenAIChat(model="gpt-3.5-turbo"), # ✅ Fixed model wrapper
68
- planning_interval=3
69
- )
70
- print("✅ Agent initialized successfully")
71
- except Exception as e:
72
- raise RuntimeError(f"Agent init failed: {str(e)}")
73
 
74
  def __call__(self, question: str) -> str:
75
  try:
76
- response = self.agent.run(question)
77
- return str(response) if response else "No answer generated"
 
 
 
 
 
 
78
  except Exception as e:
79
- return f"Error: {str(e)}"
80
 
81
- # --- Gradio Submission Logic ---
82
  def submit_answers(profile: gr.OAuthProfile | None):
83
  if not profile:
84
  return "Please login to Hugging Face", None
85
 
86
- try:
87
- agent = GAIAAgent()
88
- response = requests.get("https://agents-course-unit4-scoring.hf.space/questions", timeout=20)
89
- questions = response.json() or []
90
-
91
- answers = []
92
- results = []
93
-
94
- for item in questions[:15]: # Limit to 15 for speed
95
- task_id = item.get("task_id", "")
96
- question = item.get("question", "")
97
- answer = agent(question)
98
 
99
- answers.append({
100
- "task_id": task_id,
101
- "submitted_answer": answer[:1000]
102
- })
103
- results.append({
104
- "Task ID": task_id,
105
- "Question": question[:100],
106
- "Answer": answer[:200]
107
- })
108
 
109
- submit_response = requests.post(
110
- "https://agents-course-unit4-scoring.hf.space/submit",
111
- json={
112
- "username": profile.username,
113
- "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}",
114
- "answers": answers
115
- },
116
- timeout=60
117
- )
118
- data = submit_response.json()
119
-
120
- return (
121
- f"Submitted {len(answers)} answers\n"
122
- f"Score: {data.get('score', 'N/A')}%\n"
123
- f"Correct: {data.get('correct_count', 0)}/{len(answers)}",
124
- pd.DataFrame(results)
125
- )
126
 
127
- except Exception as e:
128
- return f"Error: {str(e)}", None
129
 
130
- # --- Gradio Interface ---
131
  with gr.Blocks() as demo:
132
- gr.Markdown("# GAIA Agent with DuckDuckGo + Calculator")
133
  gr.LoginButton()
134
  btn = gr.Button("Run Evaluation", variant="primary")
135
  status = gr.Textbox(label="Results")
136
- df = gr.DataFrame(label="Evaluation Details")
137
  btn.click(submit_answers, outputs=[status, df])
138
 
139
  if __name__ == "__main__":
140
- demo.launch()
 
5
  import math
6
 
7
  from smolagents import ToolCallingAgent, tool
8
+ from smolagents.models import OpenAIServerModel
9
  from duckduckgo_search import DDGS
10
 
11
  # --- Tools ---
12
  @tool
13
  def web_search(query: str) -> str:
14
+ """Performs a web search using DuckDuckGo."""
 
 
 
 
 
 
 
15
  try:
16
  with DDGS() as ddgs:
17
  results = ddgs.text(query, max_results=3)
 
20
  for r in results
21
  ) if results else "No results found."
22
  except Exception as e:
23
+ return f"Search error: {e}"
24
 
25
  @tool
26
  def calculate(expression: str) -> str:
27
+ """Evaluates mathematical expressions."""
 
 
 
 
 
 
 
28
  try:
29
  safe_dict = {k: v for k, v in math.__dict__.items() if not k.startswith("__")}
30
+ safe_dict.update({'abs': abs, 'round': round})
 
 
 
 
31
  result = eval(expression, {"__builtins__": None}, safe_dict)
32
  return str(result)
33
  except Exception as e:
34
+ return f"Calculation error: {e}"
35
 
36
  # --- Agent ---
37
  class GAIAAgent:
38
  def __init__(self):
39
+ model = OpenAIServerModel(model_id="gpt-3.5-turbo", api_key=os.getenv("OPENAI_API_KEY"))
40
+ self.agent = ToolCallingAgent(
41
+ name="GAIA_Agent",
42
+ description="AI assistant using web_search and calculate tools.",
43
+ tools=[web_search, calculate],
44
+ model=model,
45
+ )
 
 
 
 
 
 
 
 
 
46
 
47
  def __call__(self, question: str) -> str:
48
  try:
49
+ return self.agent.run(
50
+ question,
51
+ planning_interval=3,
52
+ system_prompt=(
53
+ "You are an AI assistant. Use web_search for factual queries "
54
+ "and calculate for math problems. Be concise and accurate."
55
+ )
56
+ )
57
  except Exception as e:
58
+ return f"Error: {e}"
59
 
60
+ # --- Gradio UI + Submission ---
61
  def submit_answers(profile: gr.OAuthProfile | None):
62
  if not profile:
63
  return "Please login to Hugging Face", None
64
 
65
+ agent = GAIAAgent()
66
+ resp = requests.get("https://agents-course-unit4-scoring.hf.space/questions", timeout=20)
67
+ questions = resp.json() or []
 
 
 
 
 
 
 
 
 
68
 
69
+ answers, rows = [], []
70
+ for item in questions[:15]:
71
+ tid, q = item.get("task_id"), item.get("question")
72
+ ans = agent(q)
73
+ answers.append({"task_id": tid, "submitted_answer": ans[:1000]})
74
+ rows.append({"Task ID": tid, "Question": q[:100], "Answer": ans[:200]})
 
 
 
75
 
76
+ post = requests.post("https://agents-course-unit4-scoring.hf.space/submit", json={
77
+ "username": profile.username,
78
+ "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}",
79
+ "answers": answers
80
+ }, timeout=60)
81
+ data = post.json()
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ out = f"Submitted {len(answers)} answers\nScore: {data.get('score', 'N/A')}%\nCorrect: {data.get('correct_count',0)}/{len(answers)}"
84
+ return out, pd.DataFrame(rows)
85
 
 
86
  with gr.Blocks() as demo:
87
+ gr.Markdown("# GAIA Agent")
88
  gr.LoginButton()
89
  btn = gr.Button("Run Evaluation", variant="primary")
90
  status = gr.Textbox(label="Results")
91
+ df = gr.DataFrame(label="Details")
92
  btn.click(submit_answers, outputs=[status, df])
93
 
94
  if __name__ == "__main__":
95
+ demo.launch()