TingWei0328 commited on
Commit
10bbaf6
·
verified ·
1 Parent(s): 7381d3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -80
app.py CHANGED
@@ -2,160 +2,120 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, LiteLLMModel
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  # --- Basic Agent Definition ---
11
- # ----- 這裡我們使用 smolagents 來打造強力 Agent ------
12
  class BasicAgent:
13
  def __init__(self):
14
  print("BasicAgent initialized.")
15
 
16
- # 1. 設定模型:使用 Hugging Face 的免費 Inference API
17
- # Qwen2.5-Coder 是目前免費 API 中寫程式和邏輯最強的模型之一
18
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
19
 
20
- self.model = HfApiModel(
21
- model_id=model_id,
22
- token=os.getenv("HF_TOKEN") # 確保你的 Space Settings 裡有設定 HF_TOKEN
23
- )
 
 
24
 
25
- # 2. 設定工具:加入搜尋工具 (DuckDuckGo)
26
- # 這能幫助解決 Wikipedia 和 影片內容的問題
27
  search_tool = DuckDuckGoSearchTool()
28
 
29
  # 3. 建立 Agent
30
- # CodeAgent 會嘗試寫 Python 程式碼來解決問題,這對數學/集合論題目非常有效
31
- self.agent = CodeAgent(
32
- tools=[search_tool],
33
- model=self.model,
34
- add_base_tools=True, # 加入基本工具 (如 Python 解譯器)
35
- max_steps=4 # 限制步數避免超時
36
- )
 
 
37
 
38
  def __call__(self, question: str) -> str:
39
  print(f"Agent received question: {question}")
 
 
 
 
 
40
  try:
41
- # Agent 思考並執行
42
- # 我們加入提示讓它知道可以使用工具
43
  answer = self.agent.run(question)
44
  print(f"Agent answer: {answer}")
45
  return str(answer)
46
  except Exception as e:
47
- print(f"Error during agent run: {e}")
48
- return "I could not answer the question due to an internal error."
 
49
 
50
- # --- 下面是原本的評估邏輯,基本保持不變 ---
51
 
52
  def run_and_submit_all(profile: gr.OAuthProfile | None):
53
- """
54
- Fetches all questions, runs the BasicAgent on them, submits all answers,
55
- and displays the results.
56
- """
57
- # --- Determine HF Space Runtime URL and Repo URL ---
58
  space_id = os.getenv("SPACE_ID")
59
-
60
  if profile:
61
  username= f"{profile.username}"
62
- print(f"User logged in: {username}")
63
  else:
64
- print("User not logged in.")
65
  return "Please Login to Hugging Face with the button.", None
66
 
67
  api_url = DEFAULT_API_URL
68
  questions_url = f"{api_url}/questions"
69
  submit_url = f"{api_url}/submit"
70
 
71
- # 1. Instantiate Agent
72
  try:
73
  agent = BasicAgent()
74
  except Exception as e:
75
- print(f"Error instantiating agent: {e}")
76
  return f"Error initializing agent: {e}", None
77
 
78
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
79
- print(agent_code)
80
 
81
- # 2. Fetch Questions
82
- print(f"Fetching questions from: {questions_url}")
83
  try:
84
  response = requests.get(questions_url, timeout=15)
85
- response.raise_for_status()
86
  questions_data = response.json()
87
- if not questions_data:
88
- print("Fetched questions list is empty.")
89
- return "Fetched questions list is empty or invalid format.", None
90
- print(f"Fetched {len(questions_data)} questions.")
91
  except Exception as e:
92
- print(f"Error fetching questions: {e}")
93
  return f"Error fetching questions: {e}", None
94
 
95
- # 3. Run your Agent
96
  results_log = []
97
  answers_payload = []
98
- print(f"Running agent on {len(questions_data)} questions...")
99
 
100
  for item in questions_data:
101
  task_id = item.get("task_id")
102
  question_text = item.get("question")
103
-
104
- if not task_id or question_text is None:
105
- continue
106
 
107
  try:
108
- print(f"Processing Task: {task_id}")
109
  submitted_answer = agent(question_text)
110
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
111
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
112
  except Exception as e:
113
- print(f"Error running agent on task {task_id}: {e}")
114
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
115
 
116
- if not answers_payload:
117
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
118
-
119
- # 4. Prepare Submission
120
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
121
- print(f"Submitting {len(answers_payload)} answers...")
122
 
123
- # 5. Submit
124
  try:
125
  response = requests.post(submit_url, json=submission_data, timeout=60)
126
- response.raise_for_status()
127
  result_data = response.json()
128
  final_status = (
129
- f"Submission Successful!\n"
130
- f"User: {result_data.get('username')}\n"
131
- f"Overall Score: {result_data.get('score', 'N/A')}% "
132
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
133
- f"Message: {result_data.get('message', 'No message received.')}"
134
  )
135
- print("Submission successful.")
136
- results_df = pd.DataFrame(results_log)
137
- return final_status, results_df
138
  except Exception as e:
139
- status_message = f"Submission Failed: {e}"
140
- print(status_message)
141
- results_df = pd.DataFrame(results_log)
142
- return status_message, results_df
143
 
144
-
145
- # --- Build Gradio Interface ---
146
  with gr.Blocks() as demo:
147
- gr.Markdown("# Agent Evaluation Runner (SmolAgents Powered)")
148
- gr.Markdown("**Note:** This agent uses Qwen2.5-Coder and DuckDuckGo Search to solve the tasks.")
149
-
150
  gr.LoginButton()
151
- run_button = gr.Button("Run Evaluation & Submit All Answers")
152
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
153
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
154
 
155
- run_button.click(
156
- fn=run_and_submit_all,
157
- outputs=[status_output, results_table]
158
- )
159
 
160
  if __name__ == "__main__":
161
- demo.launch(debug=True)
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  # --- Basic Agent Definition ---
 
11
  class BasicAgent:
12
  def __init__(self):
13
  print("BasicAgent initialized.")
14
 
15
+ # 1. 設定模型:不設定 Token,嘗試使用匿名存取
16
+ # 注意:沒有 Token 比較容易遇到 Rate Limit (429 Error)
17
  model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
18
 
19
+ try:
20
+ # 這裡移除了 token=os.getenv("HF_TOKEN")
21
+ self.model = HfApiModel(model_id=model_id)
22
+ except Exception as e:
23
+ print(f"Model init warning: {e}")
24
+ self.model = None
25
 
26
+ # 2. 設定工具
 
27
  search_tool = DuckDuckGoSearchTool()
28
 
29
  # 3. 建立 Agent
30
+ if self.model:
31
+ self.agent = CodeAgent(
32
+ tools=[search_tool],
33
+ model=self.model,
34
+ add_base_tools=True,
35
+ max_steps=4 # 步數少一點以避免超時
36
+ )
37
+ else:
38
+ self.agent = None
39
 
40
  def __call__(self, question: str) -> str:
41
  print(f"Agent received question: {question}")
42
+
43
+ # 防呆機制:如果 Agent 初始化失敗,或是跑的過程出錯,回傳預設答案
44
+ if not self.agent:
45
+ return "Agent not initialized correctly."
46
+
47
  try:
48
+ # 嘗試讓 Agent 思考並執行
 
49
  answer = self.agent.run(question)
50
  print(f"Agent answer: {answer}")
51
  return str(answer)
52
  except Exception as e:
53
+ print(f"Error during agent run (Likely Rate Limit or Auth): {e}")
54
+ # 如果因為沒 Token 導致失敗,回傳一個安全答案,而不是讓程式崩潰
55
+ return "I could not answer due to anonymous rate limits. Please set HF_TOKEN for better results."
56
 
57
+ # --- 以下是評測跑分邏輯 (不用動) ---
58
 
59
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
 
 
60
  space_id = os.getenv("SPACE_ID")
 
61
  if profile:
62
  username= f"{profile.username}"
 
63
  else:
 
64
  return "Please Login to Hugging Face with the button.", None
65
 
66
  api_url = DEFAULT_API_URL
67
  questions_url = f"{api_url}/questions"
68
  submit_url = f"{api_url}/submit"
69
 
 
70
  try:
71
  agent = BasicAgent()
72
  except Exception as e:
 
73
  return f"Error initializing agent: {e}", None
74
 
75
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
76
 
 
 
77
  try:
78
  response = requests.get(questions_url, timeout=15)
 
79
  questions_data = response.json()
 
 
 
 
80
  except Exception as e:
 
81
  return f"Error fetching questions: {e}", None
82
 
 
83
  results_log = []
84
  answers_payload = []
 
85
 
86
  for item in questions_data:
87
  task_id = item.get("task_id")
88
  question_text = item.get("question")
89
+ if not task_id or question_text is None: continue
 
 
90
 
91
  try:
 
92
  submitted_answer = agent(question_text)
93
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
94
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
95
  except Exception as e:
 
96
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
97
 
 
 
 
 
98
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
99
 
 
100
  try:
101
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
102
  result_data = response.json()
103
  final_status = (
104
+ f"Submission Successful! Score: {result_data.get('score', 'N/A')}%\n"
105
+ f"Message: {result_data.get('message', 'No message.')}"
 
 
 
106
  )
107
+ return final_status, pd.DataFrame(results_log)
 
 
108
  except Exception as e:
109
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
110
 
 
 
111
  with gr.Blocks() as demo:
112
+ gr.Markdown("# Agent Evaluation (No Token Version)")
 
 
113
  gr.LoginButton()
114
+ run_button = gr.Button("Run Evaluation")
115
+ status_output = gr.Textbox(label="Status", lines=4)
116
+ results_table = gr.DataFrame(label="Results")
117
 
118
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
119
 
120
  if __name__ == "__main__":
121
+ demo.launch()