framsouza commited on
Commit
6de08e3
Β·
verified Β·
1 Parent(s): 0bb868f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -39
app.py CHANGED
@@ -7,10 +7,17 @@ import requests
7
  import pandas as pd
8
  import gradio as gr
9
 
10
- # --- Config from Env ---
11
- API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
12
- MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
13
- HF_TOKEN_ENV = os.getenv("HUGGINGFACEHUB_API_TOKEN")
 
 
 
 
 
 
 
14
 
15
  WELCOME = """
16
  ## GAIA Benchmark Runner πŸŽ‰
@@ -19,14 +26,26 @@ Build your agent, score **β‰₯30%** to earn your Certificate,
19
  and see where you land on the Student Leaderboard!
20
  """
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  class GAIAAgent:
23
- def __init__(self, model_id: str, token: str):
24
- print(f"[DEBUG] Initializing GAIAAgent with model={model_id}") # debug
25
  self.model_id = model_id
26
- self.headers = {"Authorization": f"Bearer {token}"}
27
 
28
  def answer(self, prompt: str) -> str:
29
- print(f"[DEBUG] Sending prompt of length {len(prompt)} to HF Inference") # debug
30
  payload = {
31
  "inputs": prompt,
32
  "parameters": {"max_new_tokens": 512, "temperature": 0.2}
@@ -35,65 +54,50 @@ class GAIAAgent:
35
  resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
36
  resp.raise_for_status()
37
  data = resp.json()
38
- print(f"[DEBUG] Got response from model: {data!r}") # debug
39
  if isinstance(data, list) and data and "generated_text" in data[0]:
40
  return data[0]["generated_text"].strip()
41
  return str(data)
42
 
43
- def run_and_submit_all(profile: gr.OAuthProfile | None):
 
44
  try:
45
- print("[DEBUG] run_and_submit_all called") # debug
46
- if profile is None:
47
- print("[DEBUG] No profile provided") # debug
48
- return ("⚠️ Please log in with your Hugging Face account.", pd.DataFrame())
49
- print(f"[DEBUG] Logged in as: {profile.username}") # debug
50
- username = profile.username
51
-
52
- hf_token = HF_TOKEN_ENV or getattr(profile, "access_token", None)
53
- print(f"[DEBUG] Using HF token from {'env' if HF_TOKEN_ENV else 'profile'}") # debug
54
- if not hf_token:
55
- print("[DEBUG] No HF token found") # debug
56
- return (
57
- "❌ No Hugging Face token found.\n"
58
- "Set HUGGINGFACEHUB_API_TOKEN in Secrets or log in via the button.",
59
- pd.DataFrame()
60
- )
61
 
62
  # 1) Fetch GAIA questions
63
- print(f"[DEBUG] Fetching questions from {API_URL}/questions") # debug
64
  q_resp = requests.get(f"{API_URL}/questions", timeout=15)
65
  q_resp.raise_for_status()
66
  questions = q_resp.json() or []
67
- print(f"[DEBUG] Received {len(questions)} questions") # debug
68
  if not questions:
69
- return ("❌ No questions found. Check your API_URL.", pd.DataFrame())
70
 
71
  # 2) Init agent
72
- agent = GAIAAgent(MODEL_ID, hf_token)
73
 
74
  # 3) Answer each
75
  results = []
76
  payload = []
77
  for item in questions:
78
- print(f"[DEBUG] Processing task_id={item.get('task_id')}") # debug
79
  tid = item.get("task_id")
80
  qtxt = item.get("question", "")
81
  try:
82
  ans = agent.answer(qtxt)
83
  except Exception as e:
84
  ans = f"ERROR: {e}"
85
- print(f"[DEBUG] Error answering: {e}") # debug
86
  results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
87
  payload.append({"task_id": tid, "submitted_answer": ans})
88
  time.sleep(0.5)
89
 
90
- # 4) Submit
91
- print(f"[DEBUG] Submitting payload with {len(payload)} answers") # debug
92
- submission = {"username": username, "answers": payload}
 
 
93
  s_resp = requests.post(f"{API_URL}/submit", json=submission, timeout=60)
94
  s_resp.raise_for_status()
95
  data = s_resp.json()
96
- print(f"[DEBUG] Submission response: {data!r}") # debug
97
 
98
  # 5) Build status text
99
  status = (
@@ -108,18 +112,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
108
  except Exception as e:
109
  tb = traceback.format_exc()
110
  print("[ERROR] Unhandled exception:\n", tb)
111
- return (f"❌ Unexpected error:\n{e}", pd.DataFrame())
112
 
 
113
  with gr.Blocks() as demo:
114
  gr.Markdown(WELCOME)
115
- login = gr.LoginButton()
116
  run_btn = gr.Button("▢️ Run GAIA Benchmark")
117
  status = gr.Markdown()
118
  table_df = gr.Dataframe(headers=["Task ID", "Question", "Answer"], wrap=True)
119
 
120
  run_btn.click(
121
  fn=run_and_submit_all,
122
- inputs=[login],
123
  outputs=[status, table_df]
124
  )
125
 
 
7
  import pandas as pd
8
  import gradio as gr
9
 
10
+ # ─── Configuration ──────────────────────────────────────────────────────────
11
+ API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
12
+ MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
13
+ HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
14
+
15
+ if not HF_TOKEN:
16
+ raise RuntimeError(
17
+ "❌ Please set HUGGINGFACEHUB_API_TOKEN in your Space Secrets."
18
+ )
19
+
20
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
21
 
22
  WELCOME = """
23
  ## GAIA Benchmark Runner πŸŽ‰
 
26
  and see where you land on the Student Leaderboard!
27
  """
28
 
29
+ # ─── Utility to fetch your HF username from the token ────────────────────────
30
+ def get_hf_username():
31
+ try:
32
+ resp = requests.get("https://huggingface.co/api/whoami-v2", headers=HEADERS, timeout=10)
33
+ resp.raise_for_status()
34
+ data = resp.json()
35
+ # V2 returns {"user": { "id": ..., "username": ... }, ...}
36
+ return data.get("user", {}).get("username") or data.get("name")
37
+ except Exception as e:
38
+ print("[DEBUG] whoami failed:", e)
39
+ return None
40
+
41
+ # ─── Simple HF-Inference Agent ─────────────────────────────────────────────
42
  class GAIAAgent:
43
+ def __init__(self, model_id: str):
44
+ print(f"[DEBUG] Initializing with model {model_id}")
45
  self.model_id = model_id
46
+ self.headers = HEADERS
47
 
48
  def answer(self, prompt: str) -> str:
 
49
  payload = {
50
  "inputs": prompt,
51
  "parameters": {"max_new_tokens": 512, "temperature": 0.2}
 
54
  resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
55
  resp.raise_for_status()
56
  data = resp.json()
 
57
  if isinstance(data, list) and data and "generated_text" in data[0]:
58
  return data[0]["generated_text"].strip()
59
  return str(data)
60
 
61
+ # ─── Gradio callback ────────────────────────────────────────────────────────
62
+ def run_and_submit_all():
63
  try:
64
+ # 0) Resolve username
65
+ username = get_hf_username()
66
+ if not username:
67
+ return "❌ Could not fetch your HF username. Check your token.", pd.DataFrame()
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  # 1) Fetch GAIA questions
 
70
  q_resp = requests.get(f"{API_URL}/questions", timeout=15)
71
  q_resp.raise_for_status()
72
  questions = q_resp.json() or []
 
73
  if not questions:
74
+ return "❌ No questions returned; check your API_URL.", pd.DataFrame()
75
 
76
  # 2) Init agent
77
+ agent = GAIAAgent(MODEL_ID)
78
 
79
  # 3) Answer each
80
  results = []
81
  payload = []
82
  for item in questions:
 
83
  tid = item.get("task_id")
84
  qtxt = item.get("question", "")
85
  try:
86
  ans = agent.answer(qtxt)
87
  except Exception as e:
88
  ans = f"ERROR: {e}"
 
89
  results.append({"Task ID": tid, "Question": qtxt, "Answer": ans})
90
  payload.append({"task_id": tid, "submitted_answer": ans})
91
  time.sleep(0.5)
92
 
93
+ # 4) Submit all answers
94
+ submission = {
95
+ "username": username,
96
+ "answers": payload
97
+ }
98
  s_resp = requests.post(f"{API_URL}/submit", json=submission, timeout=60)
99
  s_resp.raise_for_status()
100
  data = s_resp.json()
 
101
 
102
  # 5) Build status text
103
  status = (
 
112
  except Exception as e:
113
  tb = traceback.format_exc()
114
  print("[ERROR] Unhandled exception:\n", tb)
115
+ return (f"❌ Unexpected error:\n{e}\n\nSee logs for details."), pd.DataFrame()
116
 
117
+ # ─── Gradio UI ──────────────────────────────────────────────────────────────
118
  with gr.Blocks() as demo:
119
  gr.Markdown(WELCOME)
 
120
  run_btn = gr.Button("▢️ Run GAIA Benchmark")
121
  status = gr.Markdown()
122
  table_df = gr.Dataframe(headers=["Task ID", "Question", "Answer"], wrap=True)
123
 
124
  run_btn.click(
125
  fn=run_and_submit_all,
126
+ inputs=[],
127
  outputs=[status, table_df]
128
  )
129