s1144662 commited on
Commit
d01d8ee
·
verified ·
1 Parent(s): 2762f9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -2,41 +2,34 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
 
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
- # 1. 初始化模型 (使用 Qwen 2.5 Coder它是目前最強的源 Coding 模型之一)
14
- model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
15
-
16
- # 2. 初始化搜尋工具
17
  search_tool = DuckDuckGoSearchTool()
18
 
19
- # 3. 建立 Agent (CodeAgent 會寫 Python 程式碼來解決問題)
20
  self.agent = CodeAgent(
21
  tools=[search_tool],
22
- model=model,
23
- add_base_tools=True, # 允許 Agent 使用 Python 執行運算
24
  )
25
- print("BasicAgent initialized with smolagents.")
26
 
27
  def __call__(self, question: str) -> str:
28
- print(f"Agent received question: {question}")
29
  try:
30
- # Agent 思考並回答問題
31
- # 這裡我們直接回傳 Agent 的執行結果
32
- answer = self.agent.run(question)
33
- print(f"Agent answer: {answer}")
34
- return str(answer)
35
  except Exception as e:
36
- print(f"Error during agent execution: {e}")
37
- return "I couldn't find the answer."
38
 
39
- # --- 下面是評分系統的標準邏輯 (不用改) ---
40
  def run_and_submit_all(profile: gr.OAuthProfile | None):
41
  space_id = os.getenv("SPACE_ID")
42
  if profile:
@@ -67,8 +60,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
67
  for item in questions_data:
68
  task_id = item.get("task_id")
69
  question_text = item.get("question")
70
-
71
- # 執行 Agent
72
  try:
73
  submitted_answer = agent(question_text)
74
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
@@ -76,7 +67,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
76
  except Exception as e:
77
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
78
 
79
- # 提交答案
80
  submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
81
  try:
82
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -87,9 +77,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
87
 
88
  return status_msg, pd.DataFrame(results_log)
89
 
90
- # --- Gradio Interface ---
91
  with gr.Blocks() as demo:
92
- gr.Markdown("# Unit 4 Final Project: My AI Agent")
93
  gr.LoginButton()
94
  run_button = gr.Button("Run Evaluation & Submit All Answers")
95
  status_output = gr.Textbox(label="Result")
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ # 這裡只導入最基本的組件,避開 HfApiModel 導入錯誤
6
+ from smolagents import CodeAgent, DuckDuckGoSearchTool
7
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
11
  class BasicAgent:
12
  def __init__(self):
13
+ # 使用字串定義模型,讓 smolagents 內部去處理載入導入報錯
14
+ self.model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
 
 
15
  search_tool = DuckDuckGoSearchTool()
16
 
 
17
  self.agent = CodeAgent(
18
  tools=[search_tool],
19
+ model=self.model_id, # 直接傳入模型 ID 字串
20
+ add_base_tools=True,
21
  )
22
+ print("Agent initialized successfully.")
23
 
24
  def __call__(self, question: str) -> str:
 
25
  try:
26
+ # 執行 Agent 並確保傳結果是字串
27
+ result = self.agent.run(question)
28
+ return str(result)
 
 
29
  except Exception as e:
30
+ print(f"Error: {e}")
31
+ return "I am sorry, I cannot answer this right now."
32
 
 
33
  def run_and_submit_all(profile: gr.OAuthProfile | None):
34
  space_id = os.getenv("SPACE_ID")
35
  if profile:
 
60
  for item in questions_data:
61
  task_id = item.get("task_id")
62
  question_text = item.get("question")
 
 
63
  try:
64
  submitted_answer = agent(question_text)
65
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
67
  except Exception as e:
68
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
69
 
 
70
  submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
71
  try:
72
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
77
 
78
  return status_msg, pd.DataFrame(results_log)
79
 
 
80
  with gr.Blocks() as demo:
81
+ gr.Markdown("# Unit 4 Final Project: AI Agent")
82
  gr.LoginButton()
83
  run_button = gr.Button("Run Evaluation & Submit All Answers")
84
  status_output = gr.Textbox(label="Result")