mianf4884 commited on
Commit
42aeb7b
·
verified ·
1 Parent(s): 1552a23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -30
app.py CHANGED
@@ -2,7 +2,8 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, VisitWebpageTool
 
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -14,31 +15,25 @@ class AgentArchitect:
14
  hf_token = os.getenv("HF_TOKEN")
15
 
16
  if not hf_token:
17
- print("Warning: HF_TOKEN is missing. Please add it to your Space Secrets.")
18
 
19
- # We use HfApiModel to stay on the free 'Serverless Inference' tier.
20
- # Qwen 2.5 Coder 32B is highly capable of writing the Python logic
21
- # needed for botany sorting and Excel math.
22
- self.model = HfApiModel(
23
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
24
- token=hf_token
25
  )
26
 
27
- # Tools are the agent's 'hands'
28
- # DuckDuckGo for search, VisitWebpage for reading deep into sites
29
  self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
30
 
31
- # CodeAgent is the 'Brain' - it can write Python code to solve problems
32
  self.agent = CodeAgent(
33
  tools=self.tools,
34
  model=self.model,
35
- add_base_tools=True # Provides built-in math and basic logic tools
36
  )
37
 
38
  def __call__(self, question: str) -> str:
39
  try:
40
- # We enforce conciseness. The GAIA benchmark grades on 'Exact Match'.
41
- # If the answer is 'India' and the agent says 'The winner is India', it fails.
42
  prompt = (
43
  f"{question}\n\n"
44
  f"Instructions: Think step-by-step. Use tools if needed. "
@@ -47,7 +42,6 @@ class AgentArchitect:
47
  result = self.agent.run(prompt)
48
  return str(result)
49
  except Exception as e:
50
- print(f"Agent Runtime Error: {e}")
51
  return f"Error: {e}"
52
 
53
  def run_and_submit_all(profile: gr.OAuthProfile | None):
@@ -62,11 +56,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
62
  submit_url = f"{api_url}/submit"
63
 
64
  try:
65
- # Initialize the Agent
66
  agent_instance = AgentArchitect()
67
 
68
- # 1. Fetch Questions from the course server
69
- print("Fetching questions...")
70
  response = requests.get(questions_url, timeout=15)
71
  response.raise_for_status()
72
  questions_data = response.json()
@@ -74,19 +66,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
74
  results_log = []
75
  answers_payload = []
76
 
77
- # 2. Loop through questions and generate answers
78
- # This will take 10-15 minutes to finish all 20.
79
  for item in questions_data:
80
  task_id = item.get("task_id")
81
  question_text = item.get("question")
82
 
83
- print(f"Processing Task: {task_id}")
84
  submitted_answer = agent_instance(question_text)
85
 
86
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
87
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
88
 
89
- # 3. Submit results back for scoring
90
  agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
91
  submission_data = {
92
  "username": username.strip(),
@@ -94,7 +84,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
94
  "answers": answers_payload
95
  }
96
 
97
- print("Submitting answers...")
98
  submit_response = requests.post(submit_url, json=submission_data, timeout=60)
99
  submit_response.raise_for_status()
100
  result_data = submit_response.json()
@@ -113,18 +102,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
113
  # --- Gradio UI ---
114
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
115
  gr.Markdown("# 🚀 Professional Agent Evaluator")
116
- gr.Markdown("Click 'Login' first, then click the 'Run' button. This may take up to 15 minutes.")
117
-
118
  gr.LoginButton()
119
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
120
-
121
  status_output = gr.Textbox(label="Status", lines=4)
122
  results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
123
 
124
- run_button.click(
125
- fn=run_and_submit_all,
126
- outputs=[status_output, results_table]
127
- )
128
 
129
  if __name__ == "__main__":
130
  demo.launch()
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ # Use LiteLLMModel instead of HfApiModel to avoid the import error
6
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, VisitWebpageTool
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
15
  hf_token = os.getenv("HF_TOKEN")
16
 
17
  if not hf_token:
18
+ print("Warning: HF_TOKEN is missing in Space Secrets.")
19
 
20
+ # LiteLLMModel is the more robust class in the latest smolagents
21
+ # We add the 'huggingface/' prefix to tell it exactly where to go
22
+ self.model = LiteLLMModel(
23
+ model_id="huggingface/Qwen/Qwen2.5-Coder-32B-Instruct",
24
+ api_key=hf_token
 
25
  )
26
 
 
 
27
  self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
28
 
 
29
  self.agent = CodeAgent(
30
  tools=self.tools,
31
  model=self.model,
32
+ add_base_tools=True
33
  )
34
 
35
  def __call__(self, question: str) -> str:
36
  try:
 
 
37
  prompt = (
38
  f"{question}\n\n"
39
  f"Instructions: Think step-by-step. Use tools if needed. "
 
42
  result = self.agent.run(prompt)
43
  return str(result)
44
  except Exception as e:
 
45
  return f"Error: {e}"
46
 
47
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
56
  submit_url = f"{api_url}/submit"
57
 
58
  try:
 
59
  agent_instance = AgentArchitect()
60
 
61
+ # 1. Fetch Questions
 
62
  response = requests.get(questions_url, timeout=15)
63
  response.raise_for_status()
64
  questions_data = response.json()
 
66
  results_log = []
67
  answers_payload = []
68
 
69
+ # 2. Run Agent
 
70
  for item in questions_data:
71
  task_id = item.get("task_id")
72
  question_text = item.get("question")
73
 
 
74
  submitted_answer = agent_instance(question_text)
75
 
76
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
77
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
78
 
79
+ # 3. Submit
80
  agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
81
  submission_data = {
82
  "username": username.strip(),
 
84
  "answers": answers_payload
85
  }
86
 
 
87
  submit_response = requests.post(submit_url, json=submission_data, timeout=60)
88
  submit_response.raise_for_status()
89
  result_data = submit_response.json()
 
102
  # --- Gradio UI ---
103
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
104
  gr.Markdown("# 🚀 Professional Agent Evaluator")
 
 
105
  gr.LoginButton()
106
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
 
107
  status_output = gr.Textbox(label="Status", lines=4)
108
  results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
109
 
110
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
111
 
112
  if __name__ == "__main__":
113
  demo.launch()