dawid-lorek commited on
Commit
4f957d6
·
verified ·
1 Parent(s): 759cedb

Update app.py

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