Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import re
|
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
# ===============================
|
| 9 |
-
# Constants
|
| 10 |
# ===============================
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
@@ -16,175 +16,15 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 16 |
class BasicAgent:
|
| 17 |
"""
|
| 18 |
Minimal GAIA Level-1 agent.
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized (PASS MODE).")
|
| 24 |
|
| 25 |
-
# 必須在 Space → Settings → Secrets 設定
|
| 26 |
self.hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 27 |
if not self.hf_token:
|
| 28 |
raise RuntimeError("HF_TOKEN missing. Set it in Space Settings → Secrets.")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
self.model_id = os.getenv("MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
|
| 32 |
-
|
| 33 |
-
# ⚠️ 一定要用 router(避免 410)
|
| 34 |
-
self.client = InferenceClient(
|
| 35 |
-
model=self.model_id,
|
| 36 |
-
token=self.hf_token,
|
| 37 |
-
base_url="https://router.huggingface.co",
|
| 38 |
-
timeout=120,
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
# 超嚴格 system prompt(EXACT MATCH 核心)
|
| 42 |
-
self.system = (
|
| 43 |
-
"You answer questions with EXACT MATCH.\n"
|
| 44 |
-
"Return ONLY the final answer.\n"
|
| 45 |
-
"No explanation.\n"
|
| 46 |
-
"No extra words.\n"
|
| 47 |
-
"No punctuation unless required.\n"
|
| 48 |
-
"No quotes.\n"
|
| 49 |
-
"If the answer is a name, output the name only.\n"
|
| 50 |
-
"If the answer is a number or date, output it exactly.\n"
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
def _sanitize(self, text: str) -> str:
|
| 54 |
-
if not text:
|
| 55 |
-
return ""
|
| 56 |
-
|
| 57 |
-
t = str(text).strip()
|
| 58 |
-
|
| 59 |
-
# 移除常見前綴
|
| 60 |
-
t = re.sub(r"(?i)final answer\s*[:\-]*", "", t)
|
| 61 |
-
t = re.sub(r"(?i)answer\s*[:\-]*", "", t)
|
| 62 |
-
|
| 63 |
-
# 只留最後一行
|
| 64 |
-
lines = [ln.strip() for ln in t.splitlines() if ln.strip()]
|
| 65 |
-
if lines:
|
| 66 |
-
t = lines[-1]
|
| 67 |
-
|
| 68 |
-
# 去掉引號
|
| 69 |
-
t = t.strip().strip('"').strip("'")
|
| 70 |
-
|
| 71 |
-
# 🔥 關鍵:移除句尾標點(GAIA 最常死在這)
|
| 72 |
-
t = re.sub(r"[.,;:!?]$", "", t)
|
| 73 |
-
|
| 74 |
-
return t
|
| 75 |
-
|
| 76 |
-
def __call__(self, question: str) -> str:
|
| 77 |
-
print(f"Q: {question[:60]}")
|
| 78 |
-
|
| 79 |
-
prompt = f"{self.system}\nQuestion: {question}\nAnswer:"
|
| 80 |
-
|
| 81 |
-
try:
|
| 82 |
-
out = self.client.text_generation(
|
| 83 |
-
prompt,
|
| 84 |
-
max_new_tokens=64,
|
| 85 |
-
temperature=0.0,
|
| 86 |
-
do_sample=False,
|
| 87 |
-
return_full_text=False,
|
| 88 |
-
)
|
| 89 |
-
except Exception:
|
| 90 |
-
# fallback(保險)
|
| 91 |
-
out = self.client.chat_completion(
|
| 92 |
-
messages=[
|
| 93 |
-
{"role": "system", "content": self.system},
|
| 94 |
-
{"role": "user", "content": question},
|
| 95 |
-
],
|
| 96 |
-
max_tokens=64,
|
| 97 |
-
temperature=0.0,
|
| 98 |
-
).choices[0].message.content
|
| 99 |
-
|
| 100 |
-
ans = self._sanitize(out)
|
| 101 |
-
print(f"A: {ans}")
|
| 102 |
-
return ans
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# ===============================
|
| 106 |
-
# Run & Submit
|
| 107 |
-
# ===============================
|
| 108 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 109 |
-
|
| 110 |
-
space_id = os.getenv("SPACE_ID")
|
| 111 |
-
|
| 112 |
-
if not profile:
|
| 113 |
-
return "Please login with Hugging Face.", None
|
| 114 |
-
|
| 115 |
-
username = profile.username
|
| 116 |
-
print(f"User: {username}")
|
| 117 |
-
|
| 118 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 119 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 120 |
-
|
| 121 |
-
try:
|
| 122 |
-
agent = BasicAgent()
|
| 123 |
-
except Exception as e:
|
| 124 |
-
return f"Agent init error: {e}", None
|
| 125 |
-
|
| 126 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 127 |
-
|
| 128 |
-
# Fetch questions
|
| 129 |
-
resp = requests.get(questions_url, timeout=20)
|
| 130 |
-
resp.raise_for_status()
|
| 131 |
-
questions = resp.json()
|
| 132 |
-
|
| 133 |
-
answers_payload = []
|
| 134 |
-
log_rows = []
|
| 135 |
-
|
| 136 |
-
for q in questions:
|
| 137 |
-
task_id = q["task_id"]
|
| 138 |
-
question = q["question"]
|
| 139 |
-
try:
|
| 140 |
-
ans = agent(question)
|
| 141 |
-
except Exception as e:
|
| 142 |
-
ans = ""
|
| 143 |
-
print("Agent error:", e)
|
| 144 |
-
|
| 145 |
-
answers_payload.append({
|
| 146 |
-
"task_id": task_id,
|
| 147 |
-
"submitted_answer": ans
|
| 148 |
-
})
|
| 149 |
-
|
| 150 |
-
log_rows.append({
|
| 151 |
-
"Task ID": task_id,
|
| 152 |
-
"Question": question,
|
| 153 |
-
"Submitted Answer": ans
|
| 154 |
-
})
|
| 155 |
-
|
| 156 |
-
submission = {
|
| 157 |
-
"username": username,
|
| 158 |
-
"agent_code": agent_code,
|
| 159 |
-
"answers": answers_payload
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
resp = requests.post(submit_url, json=submission, timeout=60)
|
| 163 |
-
resp.raise_for_status()
|
| 164 |
-
result = resp.json()
|
| 165 |
-
|
| 166 |
-
status = (
|
| 167 |
-
f"Submission Successful!\n"
|
| 168 |
-
f"User: {result.get('username')}\n"
|
| 169 |
-
f"Score: {result.get('score')}% "
|
| 170 |
-
f"({result.get('correct_count')}/{result.get('total_attempted')})\n"
|
| 171 |
-
f"{result.get('message')}"
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
return status, pd.DataFrame(log_rows)
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
# ===============================
|
| 178 |
-
# Gradio UI
|
| 179 |
-
# ===============================
|
| 180 |
-
with gr.Blocks() as demo:
|
| 181 |
-
gr.Markdown("# Basic Agent Evaluation Runner (PASS MODE)")
|
| 182 |
-
gr.LoginButton()
|
| 183 |
-
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 184 |
-
status = gr.Textbox(label="Result", lines=6)
|
| 185 |
-
table = gr.DataFrame(label="Answers", wrap=True)
|
| 186 |
-
|
| 187 |
-
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
| 188 |
-
|
| 189 |
-
if __name__ == "__main__":
|
| 190 |
-
demo.launch()
|
|
|
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
# ===============================
|
| 9 |
+
# Constants
|
| 10 |
# ===============================
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
|
|
| 16 |
class BasicAgent:
|
| 17 |
"""
|
| 18 |
Minimal GAIA Level-1 agent.
|
| 19 |
+
Target: >=30% exact match
|
| 20 |
"""
|
| 21 |
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized (PASS MODE).")
|
| 24 |
|
| 25 |
+
# 必須在 Space → Settings → Secrets 設定
|
| 26 |
self.hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 27 |
if not self.hf_token:
|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