Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,11 @@ def save_cache(cache: dict):
|
|
| 31 |
|
| 32 |
|
| 33 |
def download_task_file(api_url: str, task_id: str, file_name: str) -> str | None:
|
| 34 |
-
"""Download the file attached to a task (if any) and return its local path.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if not file_name:
|
| 36 |
return None
|
| 37 |
path = os.path.join(FILES_DIR, f"{task_id}_{file_name}")
|
|
@@ -41,9 +45,10 @@ def download_task_file(api_url: str, task_id: str, file_name: str) -> str | None
|
|
| 41 |
try:
|
| 42 |
r = requests.get(f"{api_url}/files/{task_id}", timeout=60)
|
| 43 |
r.raise_for_status()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
print(f"API file download failed for {task_id}: {e}")
|
| 49 |
|
|
@@ -65,16 +70,19 @@ def download_task_file(api_url: str, task_id: str, file_name: str) -> str | None
|
|
| 65 |
|
| 66 |
def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None = None):
|
| 67 |
"""Fetch all questions, run the agent on them, submit, and show the score."""
|
| 68 |
-
space_id = os.getenv("SPACE_ID", "
|
| 69 |
|
| 70 |
-
if profile and
|
| 71 |
username = f"{profile.username}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
elif space_id and "/" in space_id:
|
| 73 |
username = space_id.split("/")[0]
|
|
|
|
| 74 |
else:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
print(f"Running evaluation for user: {username}")
|
| 78 |
|
| 79 |
api_url = DEFAULT_API_URL
|
| 80 |
questions_url = f"{api_url}/questions"
|
|
@@ -86,10 +94,14 @@ def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None =
|
|
| 86 |
except Exception as e:
|
| 87 |
return f"Error initializing agent: {e}", None
|
| 88 |
|
| 89 |
-
agent_code =
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
# 2. Fetch questions (check local questions.json first if
|
| 93 |
questions_data = None
|
| 94 |
if os.path.exists("questions.json"):
|
| 95 |
try:
|
|
@@ -97,7 +109,7 @@ def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None =
|
|
| 97 |
questions_data = json.load(f)
|
| 98 |
print(f"Loaded {len(questions_data)} questions from local questions.json.")
|
| 99 |
except Exception as e:
|
| 100 |
-
print(f"
|
| 101 |
|
| 102 |
if not questions_data:
|
| 103 |
try:
|
|
@@ -130,7 +142,7 @@ def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None =
|
|
| 130 |
else:
|
| 131 |
file_path = download_task_file(api_url, task_id, file_name)
|
| 132 |
submitted_answer = ""
|
| 133 |
-
for attempt in range(2):
|
| 134 |
submitted_answer = agent(question_text, file_path)
|
| 135 |
if submitted_answer and submitted_answer != "Error":
|
| 136 |
break
|
|
@@ -139,7 +151,7 @@ def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None =
|
|
| 139 |
if submitted_answer and submitted_answer != "Error":
|
| 140 |
cache[task_id] = submitted_answer
|
| 141 |
save_cache(cache)
|
| 142 |
-
time.sleep(
|
| 143 |
|
| 144 |
answers_payload.append(
|
| 145 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
|
@@ -185,15 +197,17 @@ with gr.Blocks() as demo:
|
|
| 185 |
gr.Markdown(
|
| 186 |
"""
|
| 187 |
**Instructions**
|
| 188 |
-
1.
|
| 189 |
2. Log in with Hugging Face below.
|
| 190 |
-
3. Click **Run Evaluation & Submit All Answers**.
|
|
|
|
|
|
|
| 191 |
"""
|
| 192 |
)
|
| 193 |
gr.LoginButton()
|
| 194 |
use_cache_checkbox = gr.Checkbox(
|
| 195 |
value=True,
|
| 196 |
-
label="Use cached answers from previous runs (uncheck to
|
| 197 |
)
|
| 198 |
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 199 |
status_output = gr.Textbox(
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
def download_task_file(api_url: str, task_id: str, file_name: str) -> str | None:
|
| 34 |
+
"""Download the file attached to a task (if any) and return its local path.
|
| 35 |
+
|
| 36 |
+
1) try the course scoring API 2) fall back to the GAIA dataset on the Hub
|
| 37 |
+
(needs access to the gated dataset gaia-benchmark/GAIA + HF_TOKEN).
|
| 38 |
+
"""
|
| 39 |
if not file_name:
|
| 40 |
return None
|
| 41 |
path = os.path.join(FILES_DIR, f"{task_id}_{file_name}")
|
|
|
|
| 45 |
try:
|
| 46 |
r = requests.get(f"{api_url}/files/{task_id}", timeout=60)
|
| 47 |
r.raise_for_status()
|
| 48 |
+
if len(r.content) > 0:
|
| 49 |
+
with open(path, "wb") as f:
|
| 50 |
+
f.write(r.content)
|
| 51 |
+
return path
|
| 52 |
except Exception as e:
|
| 53 |
print(f"API file download failed for {task_id}: {e}")
|
| 54 |
|
|
|
|
| 70 |
|
| 71 |
def run_and_submit_all(use_cache: bool = True, profile: gr.OAuthProfile | None = None):
|
| 72 |
"""Fetch all questions, run the agent on them, submit, and show the score."""
|
| 73 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 74 |
|
| 75 |
+
if profile and getattr(profile, "username", None):
|
| 76 |
username = f"{profile.username}"
|
| 77 |
+
print(f"User logged in via OAuth: {username}")
|
| 78 |
+
elif os.getenv("HF_USERNAME"):
|
| 79 |
+
username = os.getenv("HF_USERNAME")
|
| 80 |
+
print(f"Using HF_USERNAME env var: {username}")
|
| 81 |
elif space_id and "/" in space_id:
|
| 82 |
username = space_id.split("/")[0]
|
| 83 |
+
print(f"Using Space owner username fallback: {username}")
|
| 84 |
else:
|
| 85 |
+
return "Please Login to Hugging Face with the button above.", None
|
|
|
|
|
|
|
| 86 |
|
| 87 |
api_url = DEFAULT_API_URL
|
| 88 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 94 |
except Exception as e:
|
| 95 |
return f"Error initializing agent: {e}", None
|
| 96 |
|
| 97 |
+
agent_code = (
|
| 98 |
+
f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 99 |
+
if space_id
|
| 100 |
+
else f"https://huggingface.co/spaces/{username}/Final_Assignment_Template/tree/main"
|
| 101 |
+
)
|
| 102 |
+
print(f"Agent code link: {agent_code}")
|
| 103 |
|
| 104 |
+
# 2. Fetch questions (check local questions.json first if present, otherwise live API)
|
| 105 |
questions_data = None
|
| 106 |
if os.path.exists("questions.json"):
|
| 107 |
try:
|
|
|
|
| 109 |
questions_data = json.load(f)
|
| 110 |
print(f"Loaded {len(questions_data)} questions from local questions.json.")
|
| 111 |
except Exception as e:
|
| 112 |
+
print(f"Warning reading local questions.json: {e}")
|
| 113 |
|
| 114 |
if not questions_data:
|
| 115 |
try:
|
|
|
|
| 142 |
else:
|
| 143 |
file_path = download_task_file(api_url, task_id, file_name)
|
| 144 |
submitted_answer = ""
|
| 145 |
+
for attempt in range(2): # one retry on empty answer / transient error
|
| 146 |
submitted_answer = agent(question_text, file_path)
|
| 147 |
if submitted_answer and submitted_answer != "Error":
|
| 148 |
break
|
|
|
|
| 151 |
if submitted_answer and submitted_answer != "Error":
|
| 152 |
cache[task_id] = submitted_answer
|
| 153 |
save_cache(cache)
|
| 154 |
+
time.sleep(2) # be gentle with rate limits
|
| 155 |
|
| 156 |
answers_payload.append(
|
| 157 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
|
|
|
| 197 |
gr.Markdown(
|
| 198 |
"""
|
| 199 |
**Instructions**
|
| 200 |
+
1. Add `GEMINI_API_KEY` (and optionally `HF_TOKEN`) in **Space Settings -> Variables and secrets**.
|
| 201 |
2. Log in with Hugging Face below.
|
| 202 |
+
3. Click **Run Evaluation & Submit All Answers** (takes ~5–10 minutes).
|
| 203 |
+
|
| 204 |
+
Keep your Space **public** so the code link can be verified.
|
| 205 |
"""
|
| 206 |
)
|
| 207 |
gr.LoginButton()
|
| 208 |
use_cache_checkbox = gr.Checkbox(
|
| 209 |
value=True,
|
| 210 |
+
label="Use cached answers from previous runs (uncheck to re-run all 20 questions from scratch)",
|
| 211 |
)
|
| 212 |
run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
|
| 213 |
status_output = gr.Textbox(
|