johnnychiang commited on
Commit
cb0e6ce
·
verified ·
1 Parent(s): bc407d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -1
app.py CHANGED
@@ -27,4 +27,152 @@ class BasicAgent:
27
  if not self.hf_token:
28
  raise RuntimeError("HF_TOKEN missing. Set it in Space Settings → Secrets.")
29
 
30
- # 模型(可在 Variab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  if not self.hf_token:
28
  raise RuntimeError("HF_TOKEN missing. Set it in Space Settings → Secrets.")
29
 
30
+ # 模型(可在 Variables 改)
31
+ self.model_id = os.getenv("MODEL_ID", "Qwen/Qwen2.5-7B-Instruct")
32
+
33
+ # ✅ 正確用法:不要給 base_url
34
+ self.client = InferenceClient(
35
+ model=self.model_id,
36
+ token=self.hf_token,
37
+ timeout=120,
38
+ )
39
+
40
+ self.system = (
41
+ "You answer questions with EXACT MATCH.\n"
42
+ "Return ONLY the final answer.\n"
43
+ "No explanation.\n"
44
+ "No extra words.\n"
45
+ "No punctuation unless required.\n"
46
+ "No quotes.\n"
47
+ )
48
+
49
+ def _sanitize(self, text: str) -> str:
50
+ if not text:
51
+ return ""
52
+
53
+ t = str(text).strip()
54
+
55
+ t = re.sub(r"(?i)final answer\s*[:\-]*", "", t)
56
+ t = re.sub(r"(?i)answer\s*[:\-]*", "", t)
57
+
58
+ lines = [ln.strip() for ln in t.splitlines() if ln.strip()]
59
+ if lines:
60
+ t = lines[-1]
61
+
62
+ t = t.strip().strip('"').strip("'")
63
+ t = re.sub(r"[.,;:!?]$", "", t)
64
+
65
+ return t
66
+
67
+ def __call__(self, question: str) -> str:
68
+ print(f"Q: {question[:60]}")
69
+
70
+ prompt = f"{self.system}\nQuestion: {question}\nAnswer:"
71
+
72
+ try:
73
+ out = self.client.text_generation(
74
+ prompt,
75
+ max_new_tokens=64,
76
+ temperature=0.0,
77
+ do_sample=False,
78
+ return_full_text=False,
79
+ )
80
+ except Exception:
81
+ out = self.client.chat_completion(
82
+ messages=[
83
+ {"role": "system", "content": self.system},
84
+ {"role": "user", "content": question},
85
+ ],
86
+ max_tokens=64,
87
+ temperature=0.0,
88
+ ).choices[0].message.content
89
+
90
+ ans = self._sanitize(out)
91
+ print(f"A: {ans}")
92
+ return ans
93
+
94
+
95
+ # ===============================
96
+ # Run & Submit
97
+ # ===============================
98
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
99
+
100
+ space_id = os.getenv("SPACE_ID")
101
+
102
+ if not profile:
103
+ return "Please login with Hugging Face.", None
104
+
105
+ username = profile.username
106
+ print(f"User: {username}")
107
+
108
+ questions_url = f"{DEFAULT_API_URL}/questions"
109
+ submit_url = f"{DEFAULT_API_URL}/submit"
110
+
111
+ try:
112
+ agent = BasicAgent()
113
+ except Exception as e:
114
+ return f"Agent init error: {e}", None
115
+
116
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
117
+
118
+ resp = requests.get(questions_url, timeout=20)
119
+ resp.raise_for_status()
120
+ questions = resp.json()
121
+
122
+ answers_payload = []
123
+ log_rows = []
124
+
125
+ for q in questions:
126
+ task_id = q["task_id"]
127
+ question = q["question"]
128
+ try:
129
+ ans = agent(question)
130
+ except Exception:
131
+ ans = ""
132
+
133
+ answers_payload.append({
134
+ "task_id": task_id,
135
+ "submitted_answer": ans
136
+ })
137
+
138
+ log_rows.append({
139
+ "Task ID": task_id,
140
+ "Question": question,
141
+ "Submitted Answer": ans
142
+ })
143
+
144
+ submission = {
145
+ "username": username,
146
+ "agent_code": agent_code,
147
+ "answers": answers_payload
148
+ }
149
+
150
+ resp = requests.post(submit_url, json=submission, timeout=60)
151
+ resp.raise_for_status()
152
+ result = resp.json()
153
+
154
+ status = (
155
+ f"Submission Successful!\n"
156
+ f"User: {result.get('username')}\n"
157
+ f"Score: {result.get('score')}% "
158
+ f"({result.get('correct_count')}/{result.get('total_attempted')})\n"
159
+ f"{result.get('message')}"
160
+ )
161
+
162
+ return status, pd.DataFrame(log_rows)
163
+
164
+
165
+ # ===============================
166
+ # Gradio UI
167
+ # ===============================
168
+ with gr.Blocks() as demo:
169
+ gr.Markdown("# Basic Agent Evaluation Runner (PASS MODE)")
170
+ gr.LoginButton()
171
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
172
+ status = gr.Textbox(label="Result", lines=6)
173
+ table = gr.DataFrame(label="Answers", wrap=True)
174
+
175
+ run_btn.click(fn=run_and_submit_all, outputs=[status, table])
176
+
177
+ if __name__ == "__main__":
178
+ demo.launch()