dawid-lorek commited on
Commit
1dcff21
·
verified ·
1 Parent(s): b539194

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -51
app.py CHANGED
@@ -3,24 +3,29 @@ import time
3
  import gradio as gr
4
  import requests
5
  import pandas as pd
6
- import tiktoken # For token length
7
 
8
  from smolagents import CodeAgent, OpenAIServerModel
9
- from smolagents.tools import code_tools # Includes WebSearchTool and more
10
 
11
  # Constants
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
- MAX_TOKENS = 3000 # Limit to avoid GPT-4 token overflow
14
-
15
- # Token counting helper
16
- def token_length(text: str, model: str = "gpt-4") -> int:
17
- try:
18
- encoding = tiktoken.encoding_for_model(model)
19
- return len(encoding.encode(text))
20
- except Exception:
21
- return len(text) // 4 # fallback estimate
22
-
23
- # Main agent class
 
 
 
 
 
 
24
  class SmartGAIAAgent:
25
  def __init__(self):
26
  key = os.getenv("OPENAI_API_KEY")
@@ -28,53 +33,42 @@ class SmartGAIAAgent:
28
  raise ValueError("Missing OPENAI_API_KEY")
29
  model = OpenAIServerModel(model_id="gpt-4", api_key=key)
30
  self.agent = CodeAgent(
31
- tools=code_tools(), # Includes WebSearchTool, Code Interpreter, etc.
32
- model=model
 
33
  )
34
 
35
  def __call__(self, question: str) -> str:
 
36
  try:
37
- return self.agent.run(question).strip()
38
  except Exception as e:
39
  print("Agent error:", e)
40
  return "error"
41
 
42
- # Run agent + submit answers to API
43
  def run_and_submit_all(profile: gr.OAuthProfile | None):
44
  username = profile.username if profile else None
45
  if not username:
46
- return "Please login to Hugging Face", None
47
 
48
  try:
49
  agent = SmartGAIAAgent()
50
  except Exception as e:
51
  return f"Error initializing agent: {e}", None
52
 
53
- # Fetch questions
54
- try:
55
- resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
56
- resp.raise_for_status()
57
- questions = resp.json()
58
- except Exception as e:
59
- return f"Failed to fetch questions: {e}", None
60
 
61
- skip_kw = ['.mp3', '.wav', '.png', '.jpg', 'youtube', 'video', 'watch', 'listen']
62
  payload, logs = [], []
63
-
64
  for item in questions:
65
  tid = item.get("task_id")
66
  q = item.get("question", "")
67
- if not tid or not q:
68
  continue
69
- if token_length(q) > MAX_TOKENS or any(k in q.lower() for k in skip_kw):
70
- continue
71
-
72
- try:
73
- ans = agent(q)
74
- except Exception as e:
75
- print(f"[Error Task {tid}] {e}")
76
- ans = "error"
77
-
78
  payload.append({"task_id": tid, "submitted_answer": ans})
79
  logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
80
 
@@ -86,26 +80,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
86
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
87
  "answers": payload
88
  }
89
-
90
- try:
91
- resp = requests.post(f"{DEFAULT_API_URL}/submit", json=sub, timeout=60)
92
- resp.raise_for_status()
93
- result = resp.json()
94
- score = result.get("score")
95
- correct = result.get("correct_count")
96
- attempted = result.get("total_attempted")
97
- status = f"Score: {score}% ({correct}/{attempted})"
98
- except Exception as e:
99
- status = f"Submission failed: {e}"
100
-
101
  return status, pd.DataFrame(logs)
102
 
103
- # Gradio UI
104
  with gr.Blocks() as demo:
105
  gr.Markdown("# GAIA Agent")
106
  gr.LoginButton()
107
  run_btn = gr.Button("Run & Submit")
108
- status = gr.Textbox(lines=4, label="Result")
109
  table = gr.DataFrame()
110
  run_btn.click(run_and_submit_all, outputs=[status, table])
111
 
 
3
  import gradio as gr
4
  import requests
5
  import pandas as pd
 
6
 
7
  from smolagents import CodeAgent, OpenAIServerModel
8
+ from smolagents.tools.web_search import WebSearchTool
9
 
10
  # Constants
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
+ MAX_QUESTION_LENGTH = 4000
13
+
14
+ # Retryable search tool
15
+ class ReliableWebSearchTool(WebSearchTool):
16
+ def run(self, query: str) -> str:
17
+ for attempt in range(3):
18
+ try:
19
+ return super().run(query)
20
+ except Exception as e:
21
+ if "rate" in str(e).lower():
22
+ print(f"[WebSearch] Rate limit, retry {attempt+1}/3")
23
+ time.sleep(2 * (attempt + 1))
24
+ else:
25
+ raise
26
+ raise RuntimeError("Web search failed after retries")
27
+
28
+ # Agent definition
29
  class SmartGAIAAgent:
30
  def __init__(self):
31
  key = os.getenv("OPENAI_API_KEY")
 
33
  raise ValueError("Missing OPENAI_API_KEY")
34
  model = OpenAIServerModel(model_id="gpt-4", api_key=key)
35
  self.agent = CodeAgent(
36
+ tools=[ReliableWebSearchTool()],
37
+ model=model,
38
+ add_base_tools=True
39
  )
40
 
41
  def __call__(self, question: str) -> str:
42
+ q = question[:MAX_QUESTION_LENGTH]
43
  try:
44
+ return self.agent.run(q).strip()
45
  except Exception as e:
46
  print("Agent error:", e)
47
  return "error"
48
 
49
+ # Submission logic
50
  def run_and_submit_all(profile: gr.OAuthProfile | None):
51
  username = profile.username if profile else None
52
  if not username:
53
+ return "Please Login to Hugging Face", None
54
 
55
  try:
56
  agent = SmartGAIAAgent()
57
  except Exception as e:
58
  return f"Error initializing agent: {e}", None
59
 
60
+ resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
61
+ resp.raise_for_status()
62
+ questions = resp.json()
 
 
 
 
63
 
 
64
  payload, logs = [], []
65
+ skip_kw = ['.mp3','.wav','.png','.jpg','youtube','video','watch','listen']
66
  for item in questions:
67
  tid = item.get("task_id")
68
  q = item.get("question", "")
69
+ if not tid or not q or len(q) > MAX_QUESTION_LENGTH or any(k in q.lower() for k in skip_kw):
70
  continue
71
+ ans = agent(q)
 
 
 
 
 
 
 
 
72
  payload.append({"task_id": tid, "submitted_answer": ans})
73
  logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
74
 
 
80
  "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
81
  "answers": payload
82
  }
83
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=sub, timeout=60)
84
+ resp.raise_for_status()
85
+ result = resp.json()
86
+ status = f"Score: {result.get('score')}% ({result.get('correct_count')}/{result.get('total_attempted')})"
 
 
 
 
 
 
 
 
87
  return status, pd.DataFrame(logs)
88
 
89
+ # UI
90
  with gr.Blocks() as demo:
91
  gr.Markdown("# GAIA Agent")
92
  gr.LoginButton()
93
  run_btn = gr.Button("Run & Submit")
94
+ status = gr.Textbox(lines=5)
95
  table = gr.DataFrame()
96
  run_btn.click(run_and_submit_all, outputs=[status, table])
97