Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,34 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
|
|
|
|
| 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 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# 2. 初始化搜尋工具
|
| 17 |
search_tool = DuckDuckGoSearchTool()
|
| 18 |
|
| 19 |
-
# 3. 建立 Agent (CodeAgent 會寫 Python 程式碼來解決問題)
|
| 20 |
self.agent = CodeAgent(
|
| 21 |
tools=[search_tool],
|
| 22 |
-
model=
|
| 23 |
-
add_base_tools=True,
|
| 24 |
)
|
| 25 |
-
print("
|
| 26 |
|
| 27 |
def __call__(self, question: str) -> str:
|
| 28 |
-
print(f"Agent received question: {question}")
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
print(f"Agent answer: {answer}")
|
| 34 |
-
return str(answer)
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"Error
|
| 37 |
-
return "I
|
| 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:
|
| 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")
|