framsouza commited on
Commit
e5f33ab
·
verified ·
1 Parent(s): 5055785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import os
2
  import time
3
  import requests
@@ -5,22 +7,21 @@ import pandas as pd
5
  import gradio as gr
6
 
7
  # --- Config from Env ---
8
- API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
9
- SPACE_ID = os.getenv("SPACE_ID")
10
- HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
11
- MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
 
12
 
13
- if not all([SPACE_ID, HF_TOKEN]):
14
  raise RuntimeError(
15
- "❌ Please set these in your Space Secrets:\n"
16
- " • SPACE_ID (e.g. user/your-space)\n"
17
- " • HUGGINGFACEHUB_API_TOKEN"
18
  )
19
 
20
  WELCOME = """
21
  ## GAIA Benchmark Runner 🎉
22
 
23
- Build your agent, score **≥30%** to earn the Certificate of Completion,
24
  and see where you land on the Student Leaderboard!
25
  """
26
 
@@ -42,7 +43,7 @@ class GAIAAgent:
42
  resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
43
  resp.raise_for_status()
44
  data = resp.json()
45
- # data is a list of {generated_text: "..."}
46
  if isinstance(data, list) and data and "generated_text" in data[0]:
47
  return data[0]["generated_text"].strip()
48
  return str(data)
@@ -52,6 +53,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
52
  if profile is None:
53
  return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
54
  username = profile.username
 
 
 
 
 
 
 
55
 
56
  # 1) Fetch GAIA questions
57
  q_resp = requests.get(f"{API_URL}/questions", timeout=15)
@@ -61,15 +69,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
61
  return "❌ No questions found. Check your API_URL.", pd.DataFrame()
62
 
63
  # 2) Init agent
64
- agent = GAIAAgent(MODEL_ID, HF_TOKEN)
65
 
66
  # 3) Answer each
67
  results = []
68
  payload = []
69
  for item in questions:
70
  tid = item.get("task_id")
71
- qtxt = item.get("question","")
72
- # Some tasks include a file path; you can fetch and append it if you like.
73
  try:
74
  ans = agent.answer(qtxt)
75
  except Exception as e:
@@ -101,15 +108,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
101
  # --- Gradio UI ---
102
  with gr.Blocks() as demo:
103
  gr.Markdown(WELCOME)
104
- login = gr.LoginButton()
105
- run_btn = gr.Button("▶️ Run GAIA Benchmark")
106
- status_md = gr.Markdown()
107
- table_df = gr.Dataframe(headers=["Task ID","Question","Answer"], wrap=True)
108
 
109
  run_btn.click(
110
  fn=run_and_submit_all,
111
  inputs=[login],
112
- outputs=[status_md, table_df]
113
  )
114
 
115
  if __name__ == "__main__":
 
1
+ # app.py
2
+
3
  import os
4
  import time
5
  import requests
 
7
  import gradio as gr
8
 
9
  # --- Config from Env ---
10
+ API_URL = os.getenv("API_URL", "https://agents-course-unit4-scoring.hf.space")
11
+ SPACE_ID = os.getenv("SPACE_ID") # e.g. "user/your-space"
12
+ MODEL_ID = os.getenv("MODEL_ID", "meta-llama/Llama-2-7b-instruct")
13
+ # HF token: either from Secrets or from login profile
14
+ HF_TOKEN_ENV = os.getenv("HUGGINGFACEHUB_API_TOKEN")
15
 
16
+ if not SPACE_ID:
17
  raise RuntimeError(
18
+ "❌ Please set SPACE_ID in your Space Secrets: e.g. user/your-space"
 
 
19
  )
20
 
21
  WELCOME = """
22
  ## GAIA Benchmark Runner 🎉
23
 
24
+ Build your agent, score **≥30%** to earn your Certificate,
25
  and see where you land on the Student Leaderboard!
26
  """
27
 
 
43
  resp = requests.post(url, headers=self.headers, json=payload, timeout=60)
44
  resp.raise_for_status()
45
  data = resp.json()
46
+ # data is list of {"generated_text": "..."}
47
  if isinstance(data, list) and data and "generated_text" in data[0]:
48
  return data[0]["generated_text"].strip()
49
  return str(data)
 
53
  if profile is None:
54
  return "⚠️ Please log in with your Hugging Face account.", pd.DataFrame()
55
  username = profile.username
56
+ hf_token = HF_TOKEN_ENV or getattr(profile, "access_token", None)
57
+ if not hf_token:
58
+ return (
59
+ "❌ No Hugging Face token found.\n"
60
+ "Set HUGGINGFACEHUB_API_TOKEN in Secrets or log in via the button.",
61
+ pd.DataFrame()
62
+ )
63
 
64
  # 1) Fetch GAIA questions
65
  q_resp = requests.get(f"{API_URL}/questions", timeout=15)
 
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
  tid = item.get("task_id")
79
+ qtxt = item.get("question", "")
 
80
  try:
81
  ans = agent.answer(qtxt)
82
  except Exception as e:
 
108
  # --- Gradio UI ---
109
  with gr.Blocks() as demo:
110
  gr.Markdown(WELCOME)
111
+ login = gr.LoginButton()
112
+ run_btn = gr.Button("▶️ Run GAIA Benchmark")
113
+ status = gr.Markdown()
114
+ table_df = gr.Dataframe(headers=["Task ID", "Question", "Answer"], wrap=True)
115
 
116
  run_btn.click(
117
  fn=run_and_submit_all,
118
  inputs=[login],
119
+ outputs=[status, table_df]
120
  )
121
 
122
  if __name__ == "__main__":