Update app.py
Browse files
app.py
CHANGED
|
@@ -178,4 +178,179 @@ class SmartAgent:
|
|
| 178 |
return "Date not specified"
|
| 179 |
|
| 180 |
# Grocery/cooking questions
|
| 181 |
-
elif "grocery" in question_lower or "pie" in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
return "Date not specified"
|
| 179 |
|
| 180 |
# Grocery/cooking questions
|
| 181 |
+
elif "grocery" in question_lower or "pie" in question_lower:
|
| 182 |
+
return "Check recipe for ingredients"
|
| 183 |
+
|
| 184 |
+
else:
|
| 185 |
+
return "Need more information to answer accurately"
|
| 186 |
+
|
| 187 |
+
def cleanup(self):
|
| 188 |
+
"""Clean up resources"""
|
| 189 |
+
logging.info("Cleaning up agent resources...")
|
| 190 |
+
logging.info("SmartAgent resources cleaned up.")
|
| 191 |
+
|
| 192 |
+
# --- Main Function (Fixed) ---
|
| 193 |
+
def run_and_submit_all(request: gr.Request):
|
| 194 |
+
"""Run agent evaluation and submit results"""
|
| 195 |
+
# Get username from request or fallback
|
| 196 |
+
username = None
|
| 197 |
+
|
| 198 |
+
if hasattr(request, 'oauth_profile') and request.oauth_profile:
|
| 199 |
+
username = request.oauth_profile.username
|
| 200 |
+
logging.info(f"Found username via oauth_profile: {username}")
|
| 201 |
+
|
| 202 |
+
if not username:
|
| 203 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 204 |
+
if "/" in space_id:
|
| 205 |
+
username = space_id.split("/")[0]
|
| 206 |
+
logging.info(f"Using fallback username from SPACE_ID: {username}")
|
| 207 |
+
else:
|
| 208 |
+
logging.info("No authentication detected.")
|
| 209 |
+
yield "❌ Unable to detect logged-in user.", pd.DataFrame([])
|
| 210 |
+
return
|
| 211 |
+
|
| 212 |
+
logging.info(f"Processing with username: {username}")
|
| 213 |
+
|
| 214 |
+
api_url = DEFAULT_API_URL
|
| 215 |
+
space_id = os.getenv("SPACE_ID")
|
| 216 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "N/A_LOCAL_RUN"
|
| 217 |
+
|
| 218 |
+
# Setup HTTP session
|
| 219 |
+
from requests.adapters import HTTPAdapter
|
| 220 |
+
from urllib3.util.retry import Retry
|
| 221 |
+
|
| 222 |
+
retry_strategy = Retry(
|
| 223 |
+
total=5,
|
| 224 |
+
backoff_factor=1,
|
| 225 |
+
status_forcelist=[429, 500, 502, 503, 504],
|
| 226 |
+
allowed_methods=["HEAD", "GET", "POST", "PUT", "DELETE", "OPTIONS", "TRACE"]
|
| 227 |
+
)
|
| 228 |
+
adapter = HTTPAdapter(max_retries=retry_strategy)
|
| 229 |
+
|
| 230 |
+
http_session = requests.Session()
|
| 231 |
+
http_session.mount("https://", adapter)
|
| 232 |
+
http_session.mount("http://", adapter)
|
| 233 |
+
|
| 234 |
+
agent = None
|
| 235 |
+
results_log = []
|
| 236 |
+
|
| 237 |
+
try:
|
| 238 |
+
# Initialize agent
|
| 239 |
+
agent = SmartAgent(username=username, http_session=http_session)
|
| 240 |
+
agent.setup()
|
| 241 |
+
|
| 242 |
+
yield "🚀 Initializing and fetching questions...", pd.DataFrame([])
|
| 243 |
+
|
| 244 |
+
# Fetch questions
|
| 245 |
+
questions_url = f"{api_url}/questions"
|
| 246 |
+
logging.info(f"Fetching questions from: {questions_url}")
|
| 247 |
+
response = requests.get(questions_url, timeout=15)
|
| 248 |
+
response.raise_for_status()
|
| 249 |
+
questions_data = response.json()
|
| 250 |
+
|
| 251 |
+
if not questions_data:
|
| 252 |
+
yield "No questions available.", pd.DataFrame([])
|
| 253 |
+
return
|
| 254 |
+
|
| 255 |
+
logging.info(f"Fetched {len(questions_data)} questions.")
|
| 256 |
+
|
| 257 |
+
# Process questions
|
| 258 |
+
answers_payload = []
|
| 259 |
+
|
| 260 |
+
for i, item in enumerate(questions_data, 1):
|
| 261 |
+
task_id = item.get("task_id")
|
| 262 |
+
question_text = item.get("question")
|
| 263 |
+
question_type = item.get("question_type", "")
|
| 264 |
+
question_preview = item.get("question_preview", question_text[:50] + "...")
|
| 265 |
+
|
| 266 |
+
if not task_id or question_text is None:
|
| 267 |
+
continue
|
| 268 |
+
|
| 269 |
+
start_time = time.time()
|
| 270 |
+
try:
|
| 271 |
+
# Process question with file attachment if present
|
| 272 |
+
file_name = item.get("file_name")
|
| 273 |
+
if file_name:
|
| 274 |
+
file_url = f"{api_url}/files/{task_id}"
|
| 275 |
+
question_with_file = f"{question_text}\n\nAttached file: {file_url}"
|
| 276 |
+
else:
|
| 277 |
+
question_with_file = question_text
|
| 278 |
+
|
| 279 |
+
submitted_answer = agent(question_with_file, question_type)
|
| 280 |
+
processing_time = time.time() - start_time
|
| 281 |
+
status = "OK"
|
| 282 |
+
|
| 283 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 284 |
+
results_log.append({
|
| 285 |
+
"Task ID": task_id,
|
| 286 |
+
"Question": question_preview,
|
| 287 |
+
"Submitted Answer": submitted_answer,
|
| 288 |
+
"Status": status
|
| 289 |
+
})
|
| 290 |
+
|
| 291 |
+
logging.info(f"Q{i} Answer: {submitted_answer}")
|
| 292 |
+
|
| 293 |
+
except Exception as e:
|
| 294 |
+
processing_time = time.time() - start_time
|
| 295 |
+
logging.error(f"Error on task {task_id}: {e}")
|
| 296 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": "ERROR"})
|
| 297 |
+
results_log.append({
|
| 298 |
+
"Task ID": task_id,
|
| 299 |
+
"Question": question_preview,
|
| 300 |
+
"Submitted Answer": "ERROR",
|
| 301 |
+
"Status": "ERROR"
|
| 302 |
+
})
|
| 303 |
+
|
| 304 |
+
yield f"Processing Q{i}/{len(questions_data)}", pd.DataFrame(results_log)
|
| 305 |
+
|
| 306 |
+
# Submit answers
|
| 307 |
+
submission_data = {
|
| 308 |
+
"username": username.strip(),
|
| 309 |
+
"agent_code": agent_code,
|
| 310 |
+
"answers": answers_payload
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
submit_url = f"{api_url}/submit"
|
| 314 |
+
response = requests.post(submit_url, json=submission_data, timeout=120)
|
| 315 |
+
response.raise_for_status()
|
| 316 |
+
result_data = response.json()
|
| 317 |
+
|
| 318 |
+
final_status = (
|
| 319 |
+
f"🎉 Submission Successful!\n"
|
| 320 |
+
f"User: {result_data.get('username')}\n"
|
| 321 |
+
f"Score: {result_data.get('score', 'N/A')}% "
|
| 322 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 323 |
+
f"Message: {result_data.get('message', 'No message')}"
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
yield final_status, pd.DataFrame(results_log)
|
| 327 |
+
|
| 328 |
+
except Exception as e:
|
| 329 |
+
yield f"Error: {str(e)}", pd.DataFrame(results_log)
|
| 330 |
+
finally:
|
| 331 |
+
if agent:
|
| 332 |
+
agent.cleanup()
|
| 333 |
+
|
| 334 |
+
# --- Gradio Interface ---
|
| 335 |
+
with gr.Blocks() as demo:
|
| 336 |
+
gr.Markdown("# Hugging Face Agent Certification Runner")
|
| 337 |
+
gr.Markdown("""
|
| 338 |
+
**Instructions:**
|
| 339 |
+
1. Log in to your Hugging Face account using the button below.
|
| 340 |
+
2. Click 'Run Evaluation & Submit All Answers' to start the evaluation.
|
| 341 |
+
3. The process may take several minutes.
|
| 342 |
+
""")
|
| 343 |
+
|
| 344 |
+
gr.LoginButton()
|
| 345 |
+
run_button = gr.Button("🚀 Run Evaluation & Submit All Answers")
|
| 346 |
+
status_output = gr.Textbox(label="Status", lines=5, interactive=False)
|
| 347 |
+
results_table = gr.DataFrame(label="Results", wrap=True)
|
| 348 |
+
|
| 349 |
+
run_button.click(
|
| 350 |
+
fn=run_and_submit_all,
|
| 351 |
+
outputs=[status_output, results_table],
|
| 352 |
+
show_progress=True,
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
if __name__ == "__main__":
|
| 356 |
+
demo.launch(debug=True, share=False)
|