|
|
import os |
|
|
import sys |
|
|
import subprocess |
|
|
import gradio as gr |
|
|
import requests |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
print("正在檢查環境...") |
|
|
def force_install(package_name): |
|
|
try: |
|
|
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name]) |
|
|
print(f"✅ 強制安裝成功: {package_name}") |
|
|
except Exception as e: |
|
|
print(f"⚠️ 安裝 {package_name} 遇到問題 (可能已存在): {e}") |
|
|
|
|
|
|
|
|
try: |
|
|
import litellm |
|
|
except ImportError: |
|
|
force_install("litellm") |
|
|
|
|
|
try: |
|
|
import duckduckgo_search |
|
|
except ImportError: |
|
|
force_install("duckduckgo-search") |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel |
|
|
except ImportError: |
|
|
|
|
|
force_install("git+https://github.com/huggingface/smolagents.git") |
|
|
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel |
|
|
|
|
|
|
|
|
def get_agent(): |
|
|
|
|
|
|
|
|
model = LiteLLMModel( |
|
|
model_id="huggingface/Qwen/Qwen2.5-Coder-32B-Instruct", |
|
|
api_key=os.getenv("HF_TOKEN") |
|
|
) |
|
|
|
|
|
search_tool = DuckDuckGoSearchTool() |
|
|
|
|
|
agent = CodeAgent( |
|
|
tools=[search_tool], |
|
|
model=model, |
|
|
add_base_tools=True, |
|
|
max_steps=4 |
|
|
) |
|
|
return agent |
|
|
|
|
|
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
|
if profile: |
|
|
username = f"{profile.username}" |
|
|
else: |
|
|
return "⚠️ 請先點擊 Login 按鈕登入", None |
|
|
|
|
|
try: |
|
|
agent = get_agent() |
|
|
except Exception as e: |
|
|
return f"❌ Agent 啟動失敗: {e}\n建議點擊上方 Settings -> Factory Reboot", None |
|
|
|
|
|
api_url = "https://agents-course-unit4-scoring.hf.space" |
|
|
try: |
|
|
questions = requests.get(f"{api_url}/questions").json() |
|
|
except: |
|
|
return "❌ 連線失敗,無法抓取題目", None |
|
|
|
|
|
results_log = [] |
|
|
answers_payload = [] |
|
|
|
|
|
for item in questions: |
|
|
task_id = item["task_id"] |
|
|
question = item["question"] |
|
|
|
|
|
print(f"解題中: {task_id}") |
|
|
try: |
|
|
|
|
|
answer = agent.run(question) |
|
|
final_answer = str(answer) |
|
|
except Exception as e: |
|
|
print(f"錯誤: {e}") |
|
|
|
|
|
final_answer = "Could not answer due to internal error." |
|
|
|
|
|
answers_payload.append({"task_id": task_id, "submitted_answer": final_answer}) |
|
|
results_log.append({"Question": question, "Answer": final_answer}) |
|
|
|
|
|
submission_data = { |
|
|
"username": username, |
|
|
"agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main", |
|
|
"answers": answers_payload |
|
|
} |
|
|
|
|
|
try: |
|
|
response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60) |
|
|
result = response.json() |
|
|
score = result.get('score', 0) |
|
|
msg = f"✅ 執行完成!\n分數: {score}% ({result.get('correct_count')}/{result.get('total_attempted')} 題正確)" |
|
|
return msg, pd.DataFrame(results_log) |
|
|
except Exception as e: |
|
|
return f"❌ 提交失敗: {e}", pd.DataFrame(results_log) |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# Unit 4 Agent - 暴力安裝版") |
|
|
gr.Markdown("此版本會自動修復缺少的套件 (litellm / smolagents)") |
|
|
|
|
|
gr.LoginButton() |
|
|
run_btn = gr.Button("開始跑分 (Run)") |
|
|
output_text = gr.Textbox(label="狀態") |
|
|
output_table = gr.DataFrame(label="結果") |
|
|
|
|
|
run_btn.click(fn=run_and_submit_all, outputs=[output_text, output_table]) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |