johnnychiang commited on
Commit
63bad53
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -4
app.py CHANGED
@@ -3,6 +3,9 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
@@ -10,14 +13,84 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
13
  class BasicAgent:
 
 
 
 
14
  def __init__(self):
15
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import re
7
+ from huggingface_hub import InferenceClient
8
+
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
 
13
 
14
  # --- Basic Agent Definition ---
15
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
+ # --- Basic Agent Definition ---
17
  class BasicAgent:
18
+ """
19
+ Minimal LLM-based agent for GAIA level-1 style questions.
20
+ Goal: >=30% (at least 6/20 exact match).
21
+ """
22
  def __init__(self):
23
+ print("BasicAgent initialized (LLM mode).")
24
+
25
+ # 必須先在 Space 設定 Secret:HF_TOKEN(你的 Hugging Face access token)
26
+ self.hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
27
+ if not self.hf_token:
28
+ raise RuntimeError("Missing HF_TOKEN. Please set it in Space Settings → Secrets.")
29
+
30
+ # 先用 7B 最穩最容易跑完;不夠分再升 14B/32B
31
+ self.model_id = os.getenv("MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
32
+
33
+ # 重要:用 router,不要用 api-inference(你之前 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
+ def _sanitize(self, text: str) -> str:
42
+ if not text:
43
+ return ""
44
+ t = text.strip()
45
+
46
+ # 移除 FINAL ANSWER 這種字眼(課程有說不要加)
47
+ t = re.sub(r"(?i)\bFINAL ANSWER\b\s*[:\-]*\s*", "", t).strip()
48
+
49
+ # 如果模型分行,取最後一行(通常答案會在最後)
50
+ lines = [ln.strip() for ln in t.splitlines() if ln.strip()]
51
+ if lines:
52
+ t = lines[-1]
53
+
54
+ # 去掉引號
55
+ t = t.strip().strip('"').strip("'").strip()
56
+ return t
57
+
58
  def __call__(self, question: str) -> str:
59
  print(f"Agent received question (first 50 chars): {question[:50]}...")
60
+
61
+ system = (
62
+ "You are a precise question-answering assistant.\n"
63
+ "Return ONLY the final answer, nothing else.\n"
64
+ "No explanations. No extra words. No punctuation unless required.\n"
65
+ "If the answer is a number/date/name, output it exactly.\n"
66
+ )
67
+
68
+ prompt = f"{system}\nQuestion: {question}\nAnswer:"
69
+
70
+ # 用 chat completion 風格(InferenceClient 會依模型支援)
71
+ try:
72
+ out = self.client.text_generation(
73
+ prompt,
74
+ max_new_tokens=128,
75
+ temperature=0.0,
76
+ do_sample=False,
77
+ return_full_text=False,
78
+ )
79
+ except Exception as e:
80
+ # 如果 text_generation 因模型接口差異出錯,退回 chat_completion
81
+ print("text_generation failed, fallback to chat_completion:", e)
82
+ out = self.client.chat_completion(
83
+ messages=[
84
+ {"role": "system", "content": system},
85
+ {"role": "user", "content": question},
86
+ ],
87
+ max_tokens=128,
88
+ temperature=0.0,
89
+ ).choices[0].message.content
90
+
91
+ ans = self._sanitize(str(out))
92
+ print(f"Agent answer: {ans}")
93
+ return ans
94
 
95
  def run_and_submit_all( profile: gr.OAuthProfile | None):
96
  """