punama/Final_Assignment_Template
0
1# import os2# import gradio as gr3# import requests4# import pandas as pd5 6# from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool # <-- FIXED7 8# # --- Constants ---9# DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"10 11 12# # --- REAL AGENT IMPLEMENTATION ---13# class BasicAgent:14# def __init__(self):15# print("Initializing GAIA Agent...")16 17# self.agent = CodeAgent(18# tools=[DuckDuckGoSearchTool()],19# model=InferenceClientModel("Qwen/Qwen2.5-3B-Instruct"), # <-- FIXED20# max_steps=821# )22 23# def __call__(self, question: str) -> str:24# try:25# result = self.agent.run(question)26# return str(result).strip()27# except Exception as e:28# print(f"Agent error: {e}")29# return "ERROR"30 31 32# # --- MAIN FUNCTION ---33# def run_and_submit_all(profile: gr.OAuthProfile | None):34 35# space_id = os.getenv("SPACE_ID")36 37# if profile:38# username = profile.username39# print(f"User logged in: {username}")40# else:41# return "Please login first.", None42 43# api_url = DEFAULT_API_URL44# questions_url = f"{api_url}/questions"45# submit_url = f"{api_url}/submit"46 47# # 1. Create agent48# agent = BasicAgent()49 50# agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"51# print("Code URL:", agent_code)52 53# # 2. Fetch questions54# try:55# response = requests.get(questions_url, timeout=20)56# response.raise_for_status()57# questions_data = response.json()58# except Exception as e:59# return f"Error fetching questions: {e}", None60 61# # 3. Run agent62# results_log = []63# answers_payload = []64 65# for item in questions_data:66# task_id = item.get("task_id")67# question = item.get("question")68 69# if not task_id or not question:70# continue71 72# try:73# answer = agent(question)74# answer = str(answer).strip()75 76# answers_payload.append({77# "task_id": task_id,78# "submitted_answer": answer79# })80 81# results_log.append({82# "Task ID": task_id,83# "Question": question,84# "Submitted Answer": answer85# })86 87# except Exception as e:88# results_log.append({89# "Task ID": task_id,90# "Question": question,91# "Submitted Answer": f"ERROR: {e}"92# })93 94# if not answers_payload:95# return "No answers generated.", pd.DataFrame(results_log)96 97# # 4. Submit payload98# submission_data = {99# "username": username,100# "agent_code": agent_code,101# "answers": answers_payload102# }103 104# try:105# response = requests.post(submit_url, json=submission_data, timeout=60)106# response.raise_for_status()107# result = response.json()108 109# status = (110# f"Submission Successful!\n"111# f"Score: {result.get('score', 'N/A')}%\n"112# f"{result.get('correct_count', '?')} / {result.get('total_attempted', '?')} correct"113# )114 115# return status, pd.DataFrame(results_log)116 117# except Exception as e:118# return f"Submission failed: {e}", pd.DataFrame(results_log)119 120 121# # --- GRADIO UI ---122# with gr.Blocks() as demo:123 124# gr.Markdown("# GAIA Agent Runner (SmolAgents)")125 126# gr.Markdown("""127# 1. Login with Hugging Face 128# 2. Click Run Evaluation 129# 3. System fetches questions → runs agent → submits results 130# """)131 132# gr.LoginButton()133 134# btn = gr.Button("Run Evaluation & Submit")135 136# status = gr.Textbox(label="Status", lines=6)137# table = gr.DataFrame(label="Results")138 139# btn.click(140# fn=run_and_submit_all,141# outputs=[status, table]142# )143 144 145# if __name__ == "__main__":146# demo.launch()147 148 149 150import os151import gradio as gr152import requests153import pandas as pd154 155from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool # <-- FIXED156 157# --- Constants ---158DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"159 160 161class BasicAgent:162 def __init__(self):163 print("Initializing GAIA Agent...")164 165 self.agent = CodeAgent(166 tools=[DuckDuckGoSearchTool()],167 model=InferenceClientModel(168 model_id="Qwen/Qwen2.5-72B-Instruct",169 token=os.environ.get("HF_TOKEN"),170 ),171 max_steps=8172 )173 174 def __call__(self, question: str) -> str:175 try:176 result = self.agent.run(question)177 return str(result).strip()178 except Exception as e:179 print(f"Agent error: {e}")180 return "ERROR"181 182# --- MAIN FUNCTION ---183def run_and_submit_all(profile: gr.OAuthProfile | None):184 185 space_id = os.getenv("SPACE_ID")186 187 if profile:188 username = profile.username189 print(f"User logged in: {username}")190 else:191 return "Please login first.", None192 193 api_url = DEFAULT_API_URL194 questions_url = f"{api_url}/questions"195 submit_url = f"{api_url}/submit"196 197 # 1. Create agent198 agent = BasicAgent()199 200 agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"201 print("Code URL:", agent_code)202 203 # 2. Fetch questions204 try:205 response = requests.get(questions_url, timeout=20)206 response.raise_for_status()207 questions_data = response.json()208 except Exception as e:209 return f"Error fetching questions: {e}", None210 211 # 3. Run agent212 results_log = []213 answers_payload = []214 215 for item in questions_data:216 task_id = item.get("task_id")217 question = item.get("question")218 219 if not task_id or not question:220 continue221 222 try:223 answer = agent(question)224 answer = str(answer).strip()225 226 answers_payload.append({227 "task_id": task_id,228 "submitted_answer": answer229 })230 231 results_log.append({232 "Task ID": task_id,233 "Question": question,234 "Submitted Answer": answer235 })236 237 except Exception as e:238 results_log.append({239 "Task ID": task_id,240 "Question": question,241 "Submitted Answer": f"ERROR: {e}"242 })243 244 if not answers_payload:245 return "No answers generated.", pd.DataFrame(results_log)246 247 # 4. Submit payload248 submission_data = {249 "username": username,250 "agent_code": agent_code,251 "answers": answers_payload252 }253 254 try:255 response = requests.post(submit_url, json=submission_data, timeout=60)256 response.raise_for_status()257 result = response.json()258 259 status = (260 f"Submission Successful!\n"261 f"Score: {result.get('score', 'N/A')}%\n"262 f"{result.get('correct_count', '?')} / {result.get('total_attempted', '?')} correct"263 )264 265 return status, pd.DataFrame(results_log)266 267 except Exception as e:268 return f"Submission failed: {e}", pd.DataFrame(results_log)269 270 271# --- GRADIO UI ---272with gr.Blocks() as demo:273 274 gr.Markdown("# GAIA Agent Runner (SmolAgents)")275 276 gr.Markdown("""277 1. Login with Hugging Face 278 2. Click Run Evaluation 279 3. System fetches questions → runs agent → submits results 280 """)281 282 gr.LoginButton()283 284 btn = gr.Button("Run Evaluation & Submit")285 286 status = gr.Textbox(label="Status", lines=6)287 table = gr.DataFrame(label="Results")288 289 btn.click(290 fn=run_and_submit_all,291 outputs=[status, table]292 )293 294 295if __name__ == "__main__":296 demo.launch()