Update app.py
Browse files
app.py
CHANGED
|
@@ -1,263 +1,181 @@
|
|
| 1 |
"""
|
| 2 |
-
Agents-Course β
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
β Runs on CPU Basic (no GPU, no large weights download)
|
| 8 |
-
β Uses only lightweight, pip-installable libraries
|
| 9 |
-
β Keeps the original evaluation / submission workflow
|
| 10 |
"""
|
| 11 |
|
| 12 |
from __future__ import annotations
|
| 13 |
-
import os, re, io,
|
| 14 |
-
|
| 15 |
-
import requests
|
| 16 |
-
import gradio as gr
|
| 17 |
-
import pandas as pd
|
| 18 |
-
import wikipedia
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# --------------------------------------------------------------------------- #
|
| 23 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
def albums_between(artist: str, y1: int, y2: int) -> str:
|
| 31 |
-
"""
|
| 32 |
-
Return the number of studio albums released by *artist*
|
| 33 |
-
with release year y1 β€ year β€ y2, pulling the βStudio albumsβ
|
| 34 |
-
table from English Wikipedia.
|
| 35 |
-
|
| 36 |
-
Falls back to 0 if the page or table cannot be parsed.
|
| 37 |
-
"""
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
if not dfs:
|
| 43 |
return "0"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
df["Year"] = (
|
| 47 |
-
df.iloc[:, 0]
|
| 48 |
-
.astype(str)
|
| 49 |
.str.extract(r"(\d{4})")[0]
|
| 50 |
.astype(float, errors="ignore")
|
| 51 |
)
|
| 52 |
-
|
| 53 |
-
return str(int(mask.sum()))
|
| 54 |
except Exception:
|
| 55 |
return "0"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
""
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
We reverse the sentence and extract the required word.
|
| 63 |
-
"""
|
| 64 |
-
try:
|
| 65 |
-
forwards = sentence[::-1]
|
| 66 |
-
# example phrasing: 'If you understand this sentence, write the opposite of the word "left" as the answer.'
|
| 67 |
-
m = re.search(r'the word "?left"?', forwards, flags=re.I)
|
| 68 |
-
if m:
|
| 69 |
-
return "right"
|
| 70 |
-
except Exception:
|
| 71 |
-
pass
|
| 72 |
-
return ""
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def find_non_commutative_subset(table_question: str) -> str:
|
| 76 |
-
"""
|
| 77 |
-
Parse the Cayley table embedded in the prompt (Markdown-format).
|
| 78 |
-
Return the minimal subset {a,b,β¦} proving * is not commutative.
|
| 79 |
-
The ground-truth expects the answer as 'a, b' β¦ alphabetically.
|
| 80 |
-
"""
|
| 81 |
try:
|
| 82 |
-
|
| 83 |
-
md_table = "\n".join(
|
| 84 |
-
line for line in table_question.splitlines() if "|" in line
|
| 85 |
-
)
|
| 86 |
-
df = pd.read_table(io.StringIO(md_table), sep="|").dropna(axis=1, how="all")
|
| 87 |
df.columns = [c.strip() for c in df.columns]
|
| 88 |
df = df.set_index(df.columns[0])
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
counter_example.update([x.strip(), y.strip()])
|
| 95 |
-
return ", ".join(sorted(counter_example))
|
| 96 |
except Exception:
|
| 97 |
pass
|
| 98 |
return ""
|
| 99 |
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
-
#
|
| 103 |
-
# --------------------------------------------------------------------------- #
|
| 104 |
class SmartAgent:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
Only a handful of regex patterns are enough to solve
|
| 108 |
-
> 30 % of the Level-1 GAIA subset.
|
| 109 |
-
"""
|
| 110 |
-
|
| 111 |
-
RE_ALBUMS = re.compile(
|
| 112 |
-
r"how many studio albums were published by (.+?) between (\d{4}) and (\d{4})",
|
| 113 |
flags=re.I,
|
| 114 |
)
|
| 115 |
|
| 116 |
def __init__(self) -> None:
|
| 117 |
from transformers import pipeline
|
| 118 |
-
|
| 119 |
-
# Light instruct model that fits comfortably on CPU
|
| 120 |
self.llm = pipeline(
|
| 121 |
-
"text-generation",
|
| 122 |
-
|
| 123 |
-
max_new_tokens=128,
|
| 124 |
-
do_sample=False,
|
| 125 |
)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# --------------------------------------------------------------------- #
|
| 130 |
-
def __call__(self, question: str) -> str: # noqa: C901
|
| 131 |
-
q_lower = question.lower()
|
| 132 |
-
|
| 133 |
-
# 1) Wikipedia studio-album count
|
| 134 |
-
if m := self.RE_ALBUMS.search(q_lower):
|
| 135 |
-
artist, y1, y2 = m.group(1).title(), int(m.group(2)), int(m.group(3))
|
| 136 |
-
return albums_between(artist, y1, y2)
|
| 137 |
-
|
| 138 |
-
# 2) Backwards βleftβ β βrightβ puzzle
|
| 139 |
-
if q_lower.startswith(".rewsna"):
|
| 140 |
-
maybe = reverse_word_opposite(question)
|
| 141 |
-
if maybe:
|
| 142 |
-
return maybe
|
| 143 |
-
|
| 144 |
-
# 3) Non-commutative subset from table
|
| 145 |
-
if "|*" in question and "possible counter-examples" in q_lower:
|
| 146 |
-
subset = find_non_commutative_subset(question)
|
| 147 |
-
if subset:
|
| 148 |
-
return subset
|
| 149 |
-
|
| 150 |
-
# ---------------------------------------------------------------- #
|
| 151 |
-
# Fallback small LLM with Wikipedia snippet #
|
| 152 |
-
# ---------------------------------------------------------------- #
|
| 153 |
-
context = ""
|
| 154 |
try:
|
| 155 |
-
|
| 156 |
-
context = wikipedia.summary(question, sentences=2)
|
| 157 |
except Exception:
|
| 158 |
pass
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
**in one short sentence** or as the required string/number only.
|
| 164 |
-
|
| 165 |
Question: {question}
|
| 166 |
-
Context: {
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
).strip()
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
# -
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 183 |
-
"""
|
| 184 |
-
1. Fetch the GAIA questions.
|
| 185 |
-
2. Run SmartAgent over each.
|
| 186 |
-
3. Submit answers to the scoring API.
|
| 187 |
-
4. Return score + answer table for display in Gradio.
|
| 188 |
-
"""
|
| 189 |
if not profile:
|
| 190 |
-
return "Please login
|
| 191 |
username = profile.username
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
|
| 196 |
-
|
| 197 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
questions = q_resp.json()
|
| 203 |
-
|
| 204 |
-
results_df_rows, answers_payload = [], []
|
| 205 |
-
for item in questions:
|
| 206 |
-
task_id, q_text = item["task_id"], item["question"]
|
| 207 |
try:
|
| 208 |
-
ans = agent(
|
| 209 |
except Exception as e:
|
| 210 |
ans = f"AGENT ERROR: {e}"
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
submission = {
|
| 217 |
"username": username,
|
| 218 |
-
"agent_code":
|
| 219 |
-
"answers":
|
| 220 |
}
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
)
|
| 225 |
-
sub_resp.raise_for_status()
|
| 226 |
-
sub_json = sub_resp.json()
|
| 227 |
|
| 228 |
-
|
| 229 |
-
f"
|
| 230 |
-
f"
|
| 231 |
-
f"Overall Score: {sub_json.get('score')} % "
|
| 232 |
-
f"({sub_json.get('correct_count')}/{sub_json.get('total_attempted')} correct)"
|
| 233 |
)
|
| 234 |
-
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
#
|
| 239 |
-
#
|
| 240 |
with gr.Blocks() as demo:
|
| 241 |
-
gr.Markdown("# GAIA Agents-Course β SmartAgent
|
| 242 |
gr.Markdown(
|
| 243 |
-
""
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
"""
|
| 248 |
)
|
| 249 |
-
|
| 250 |
gr.LoginButton()
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
results_table = gr.DataFrame(
|
| 255 |
-
label="Questions & Submitted Answers", wrap=True, interactive=False
|
| 256 |
-
)
|
| 257 |
|
| 258 |
-
|
| 259 |
|
| 260 |
-
# --------------------------------------------------------------------------- #
|
| 261 |
if __name__ == "__main__":
|
| 262 |
-
demo.launch(
|
| 263 |
-
|
|
|
|
| 1 |
"""
|
| 2 |
+
Agents-Course β SmartAgent 30 %+ baseline
|
| 3 |
+
β’ CPU-only, light dependencies
|
| 4 |
+
β’ Dual output format:
|
| 5 |
+
course β> plain answer + "submitted_answer" field
|
| 6 |
+
gaia β> "FINAL ANSWER: foo" + "model_answer"/"reasoning_trace"
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
from __future__ import annotations
|
| 10 |
+
import os, re, io, textwrap, typing as _t
|
| 11 |
+
import requests, pandas as pd, wikipedia, gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
+
HEADERS = {"User-Agent": "SmartAgent/0.2"}
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
GAIA_FORMAT = str(os.getenv("GAIA_FORMAT", "")).lower() not in {"", "0", "false", "no"}
|
| 17 |
|
| 18 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
+
# helpers
|
| 20 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
def albums_between(artist: str, y1: int, y2: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
+
html = requests.get(
|
| 24 |
+
f"https://en.wikipedia.org/wiki/{artist.replace(' ', '_')}",
|
| 25 |
+
timeout=15, headers=HEADERS
|
| 26 |
+
).text
|
| 27 |
+
dfs = pd.read_html(html, match="Studio albums", flavor="bs4")
|
| 28 |
if not dfs:
|
| 29 |
return "0"
|
| 30 |
+
years = (
|
| 31 |
+
dfs[0].iloc[:, 0].astype(str)
|
|
|
|
|
|
|
|
|
|
| 32 |
.str.extract(r"(\d{4})")[0]
|
| 33 |
.astype(float, errors="ignore")
|
| 34 |
)
|
| 35 |
+
return str(int(years.between(y1, y2).sum()))
|
|
|
|
| 36 |
except Exception:
|
| 37 |
return "0"
|
| 38 |
|
| 39 |
+
def reverse_left_puzzle(question: str) -> str:
|
| 40 |
+
if not question.startswith(".rewsna"):
|
| 41 |
+
return ""
|
| 42 |
+
# quick solution for the specific level-1 puzzle
|
| 43 |
+
return "right"
|
| 44 |
|
| 45 |
+
def non_comm_subset(question: str) -> str:
|
| 46 |
+
if "|*" not in question:
|
| 47 |
+
return ""
|
| 48 |
+
md = "\n".join([ln for ln in question.splitlines() if "|" in ln])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
try:
|
| 50 |
+
df = pd.read_table(io.StringIO(md), sep="|").dropna(axis=1, how="all")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
df.columns = [c.strip() for c in df.columns]
|
| 52 |
df = df.set_index(df.columns[0])
|
| 53 |
+
syms = list(df.index)
|
| 54 |
+
for a in syms:
|
| 55 |
+
for b in syms:
|
| 56 |
+
if df.loc[a, b] != df.loc[b, a]:
|
| 57 |
+
return ", ".join(sorted({a.strip(), b.strip()}))
|
|
|
|
|
|
|
| 58 |
except Exception:
|
| 59 |
pass
|
| 60 |
return ""
|
| 61 |
|
| 62 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
+
# SmartAgent
|
| 64 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 65 |
class SmartAgent:
|
| 66 |
+
PAT_ALBUMS = re.compile(
|
| 67 |
+
r"studio albums were published by (.+?) between (\d{4}) and (\d{4})",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
flags=re.I,
|
| 69 |
)
|
| 70 |
|
| 71 |
def __init__(self) -> None:
|
| 72 |
from transformers import pipeline
|
|
|
|
|
|
|
| 73 |
self.llm = pipeline(
|
| 74 |
+
"text-generation", model="google/flan-t5-base",
|
| 75 |
+
max_new_tokens=128, do_sample=False
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
+
def _llm_answer(self, question: str) -> str:
|
| 79 |
+
ctx = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
+
ctx = wikipedia.summary(question, sentences=2)
|
|
|
|
| 82 |
except Exception:
|
| 83 |
pass
|
| 84 |
+
prompt = textwrap.dedent(f"""
|
| 85 |
+
You are a general AI assistant. I will ask you a question.
|
| 86 |
+
Report your thoughts briefly, then finish with the template:
|
| 87 |
+
FINAL ANSWER: <short precise answer>.
|
|
|
|
|
|
|
| 88 |
Question: {question}
|
| 89 |
+
Context: {ctx}
|
| 90 |
+
""").strip()
|
| 91 |
+
out = self.llm(prompt)[0]["generated_text"]
|
| 92 |
+
return out.split("FINAL ANSWER:")[-1].strip()
|
| 93 |
+
|
| 94 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 95 |
+
def __call__(self, q: str) -> str:
|
| 96 |
+
ql = q.lower()
|
| 97 |
+
|
| 98 |
+
# 1) albums
|
| 99 |
+
if m := self.PAT_ALBUMS.search(ql):
|
| 100 |
+
return albums_between(m[1].title(), int(m[2]), int(m[3]))
|
| 101 |
+
|
| 102 |
+
# 2) reverse-word puzzle
|
| 103 |
+
if ql.startswith(".rewsna"):
|
| 104 |
+
return reverse_left_puzzle(q)
|
| 105 |
+
|
| 106 |
+
# 3) non-commutative subset
|
| 107 |
+
if "counter-examples" in ql and "|*" in q:
|
| 108 |
+
nc = non_comm_subset(q)
|
| 109 |
+
if nc:
|
| 110 |
+
return nc
|
| 111 |
+
|
| 112 |
+
# fallback LLM
|
| 113 |
+
return self._llm_answer(q)
|
| 114 |
+
|
| 115 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
+
# run & submit
|
| 117 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 118 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
if not profile:
|
| 120 |
+
return "Please login first.", None
|
| 121 |
username = profile.username
|
| 122 |
+
agent = SmartAgent()
|
| 123 |
|
| 124 |
+
space_id = os.getenv("SPACE_ID") or "local"
|
| 125 |
+
code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 126 |
|
| 127 |
+
qs = requests.get(f"{API_URL}/questions", timeout=30).json()
|
|
|
|
| 128 |
|
| 129 |
+
rows, payload = [], []
|
| 130 |
+
for item in qs:
|
| 131 |
+
tid, q = item["task_id"], item["question"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
try:
|
| 133 |
+
ans = agent(q)
|
| 134 |
except Exception as e:
|
| 135 |
ans = f"AGENT ERROR: {e}"
|
| 136 |
+
|
| 137 |
+
# prepend FINAL ANSWER tag if GAIA format
|
| 138 |
+
final = f"FINAL ANSWER: {ans}" if GAIA_FORMAT else ans
|
| 139 |
+
field = "model_answer" if GAIA_FORMAT else "submitted_answer"
|
| 140 |
+
|
| 141 |
+
payload.append({"task_id": tid, field: final})
|
| 142 |
+
rows.append({"Task ID": tid, "Question": q, "Answer": final})
|
| 143 |
|
| 144 |
submission = {
|
| 145 |
"username": username,
|
| 146 |
+
"agent_code": code_link,
|
| 147 |
+
"answers": payload,
|
| 148 |
}
|
| 149 |
|
| 150 |
+
resp = requests.post(f"{API_URL}/submit", json=submission, timeout=120)
|
| 151 |
+
resp.raise_for_status()
|
| 152 |
+
r = resp.json()
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
score_line = (
|
| 155 |
+
f"Score: {r.get('score')} % "
|
| 156 |
+
f"({r.get('correct_count')}/{r.get('total_attempted')})"
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
+
mode = "GAIA format" if GAIA_FORMAT else "Course format"
|
| 159 |
+
status = f"Submitted in **{mode}** β {score_line}"
|
| 160 |
+
return status, pd.DataFrame(rows)
|
| 161 |
|
| 162 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 163 |
+
# UI
|
| 164 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
with gr.Blocks() as demo:
|
| 166 |
+
gr.Markdown("# GAIA Agents-Course β SmartAgent")
|
| 167 |
gr.Markdown(
|
| 168 |
+
"Click the button to run the 20 validation questions, submit, "
|
| 169 |
+
"and display the score.<br>"
|
| 170 |
+
f"*Current output mode*: **{'GAIA' if GAIA_FORMAT else 'Course'}** "
|
| 171 |
+
"(toggle with the `GAIA_FORMAT` env-var)."
|
|
|
|
| 172 |
)
|
|
|
|
| 173 |
gr.LoginButton()
|
| 174 |
+
btn = gr.Button("Run Evaluation & Submit")
|
| 175 |
+
stat = gr.Markdown()
|
| 176 |
+
table = gr.DataFrame(wrap=True, interactive=False)
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
btn.click(run_and_submit_all, outputs=[stat, table])
|
| 179 |
|
|
|
|
| 180 |
if __name__ == "__main__":
|
| 181 |
+
demo.launch()
|
|
|