import json import threading import requests import gradio as gr from agent import build_agent, answer OWNER='SystemReset' lock=threading.Lock() def require_owner(profile): if profile is None or profile.username != OWNER: raise gr.Error('Sign in as SystemReset to use these controls.') def respond(message, profile: gr.OAuthProfile | None): require_owner(profile) return answer(build_agent(use_case=True),message) def run_challenge(profile: gr.OAuthProfile | None): require_owner(profile) if not lock.acquire(blocking=False): raise gr.Error('An evaluation is already running.') try: response=requests.get('https://agents-course-unit4-scoring.hf.space/questions',timeout=60) response.raise_for_status() questions=response.json() agent=build_agent() results=[] for index,item in enumerate(questions): if item.get('file_name') or 'youtube.com/' in item['question']: row={'task_id':item['task_id'],'submitted_answer':'','status':'media unsupported'} else: try: result=answer(agent,item['question']) row={'task_id':item['task_id'],'submitted_answer':result,'status':'generated' if result else 'insufficient evidence'} except Exception as exc: row={'task_id':item['task_id'],'submitted_answer':'','status':type(exc).__name__} results.append(row) with open('challenge_results.json','w') as handle: json.dump(results,handle) yield f'Processed {index+1}/{len(questions)} questions',results finally: lock.release() def submit_challenge(profile: gr.OAuthProfile | None): require_owner(profile) if lock.locked(): raise gr.Error('Wait for evaluation to finish.') with open('challenge_results.json') as handle: results=json.load(handle) if len(results)!=20: raise gr.Error('Complete the evaluation first.') payload={'username':OWNER,'agent_code':'https://huggingface.co/spaces/SystemReset/alfred-agents-course/tree/main','answers':[{'task_id':row['task_id'],'submitted_answer':row['submitted_answer']} for row in results]} response=requests.post('https://agents-course-unit4-scoring.hf.space/submit',json=payload,timeout=120) response.raise_for_status() return response.json() with gr.Blocks() as demo: gr.Markdown('# Alfred: Course Research Agent\nSign in as SystemReset. Code is public; model calls and challenge controls are restricted to the owner.') gr.LoginButton() prompt=gr.Textbox(label='Question') reply=gr.Textbox(label='Answer') gr.Button('Ask Alfred').click(respond,inputs=[prompt],outputs=[reply]) gr.Markdown('## Final challenge\nRuns the real agent on benchmark questions. Unsupported media is left unanswered. Submit publishes generated answers and score to the course leaderboard.') status=gr.Textbox(label='Progress') results=gr.JSON(label='Generated answers') gr.Button('Run final challenge').click(run_challenge,outputs=[status,results]) gr.Button('Submit generated answers').click(submit_challenge,outputs=[results]) if __name__=='__main__': demo.queue().launch()