CoolFace
Apppublic

CH-Presidio/Final_Assignment_Template

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
app.py204 linesDownload Raw Back to root
1import os2import gradio as gr3import requests4import pandas as pd5import tempfile6from hybrid_agent import GeminiAgent7 8# --- Constants ---9DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"10 11 12class BasicAgent:13    def __init__(self):14        api_key = os.environ.get("GOOGLE_API_KEY")15        if not api_key:16            raise ValueError("GOOGLE_API_KEY environment variable not set.")17        self.agent = GeminiAgent(api_key=api_key)18        print("BasicAgent initialized.")19 20    def __call__(self, question: str) -> str:21        print(f"Agent received question (first 50 chars): {question[:50]}...")22        return self.agent.run(question)23 24 25def run_and_submit_all(profile: gr.OAuthProfile | None):26    space_id = os.getenv("SPACE_ID")27 28    if profile:29        username = f"{profile.username}"30        print(f"User logged in: {username}")31    else:32        print("User not logged in.")33        return "Please Login to Hugging Face with the button.", None34 35    api_url = DEFAULT_API_URL36    questions_url = f"{api_url}/questions"37    submit_url = f"{api_url}/submit"38 39    # 1. Instantiate Agent40    try:41        agent = BasicAgent()42    except Exception as e:43        print(f"Error instantiating agent: {e}")44        return f"Error initializing agent: {e}", None45 46    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"47    print(agent_code)48 49    # 2. Fetch Questions50    print(f"Fetching questions from: {questions_url}")51    try:52        response = requests.get(questions_url, timeout=15)53        response.raise_for_status()54        questions_data = response.json()55        if not questions_data:56            print("Fetched questions list is empty.")57            return "Fetched questions list is empty or invalid format.", None58        print(f"Fetched {len(questions_data)} questions.")59    except requests.exceptions.RequestException as e:60        print(f"Error fetching questions: {e}")61        return f"Error fetching questions: {e}", None62    except Exception as e:63        print(f"An unexpected error occurred fetching questions: {e}")64        return f"An unexpected error occurred fetching questions: {e}", None65 66    # 3. Run Agent67    results_log = []68    answers_payload = []69    temp_files = []70    print(f"Running agent on {len(questions_data)} questions...")71 72    for item in questions_data:73        task_id = item.get("task_id")74        question_text = item.get("question")75        file_name = item.get("file_name")76 77        if not task_id or question_text is None:78            print(f"Skipping item with missing task_id or question: {item}")79            continue80 81        # Download attached file if present82        augmented_question = question_text83        if file_name:84            try:85                file_response = requests.get(f"{api_url}/files/{task_id}", timeout=30)86                if file_response.status_code == 200:87                    suffix = os.path.splitext(file_name)[1] or ".bin"88                    tmp = tempfile.NamedTemporaryFile(89                        suffix=suffix, delete=False, prefix=f"gaia_{task_id}_"90                    )91                    tmp.write(file_response.content)92                    tmp.close()93                    temp_files.append(tmp.name)94                    augmented_question = (95                        f"{question_text}\n\n"96                        f"[Attached file '{file_name}' is saved locally at: {tmp.name}]"97                    )98                    print(f"Downloaded attachment '{file_name}' -> {tmp.name}")99                else:100                    print(f"Could not download file for task {task_id}: HTTP {file_response.status_code}")101            except Exception as e:102                print(f"Failed to download file for task {task_id}: {e}")103 104        try:105            submitted_answer = agent(augmented_question)106            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})107            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})108        except Exception as e:109            print(f"Error running agent on task {task_id}: {e}")110            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})111 112    # Clean up temp files113    for f in temp_files:114        try:115            os.unlink(f)116        except Exception:117            pass118 119    if not answers_payload:120        print("Agent did not produce any answers to submit.")121        return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)122 123    # 4. Prepare Submission124    submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}125    status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."126    print(status_update)127 128    # 5. Submit129    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")130    try:131        response = requests.post(submit_url, json=submission_data, timeout=60)132        response.raise_for_status()133        result_data = response.json()134        final_status = (135            f"Submission Successful!\n"136            f"User: {result_data.get('username')}\n"137            f"Overall Score: {result_data.get('score', 'N/A')}% "138            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"139            f"Message: {result_data.get('message', 'No message received.')}"140        )141        print("Submission successful.")142        return final_status, pd.DataFrame(results_log)143    except requests.exceptions.HTTPError as e:144        error_detail = f"Server responded with status {e.response.status_code}."145        try:146            error_json = e.response.json()147            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"148        except requests.exceptions.JSONDecodeError:149            error_detail += f" Response: {e.response.text[:500]}"150        status_message = f"Submission Failed: {error_detail}"151        print(status_message)152        return status_message, pd.DataFrame(results_log)153    except requests.exceptions.Timeout:154        status_message = "Submission Failed: The request timed out."155        print(status_message)156        return status_message, pd.DataFrame(results_log)157    except requests.exceptions.RequestException as e:158        status_message = f"Submission Failed: Network error - {e}"159        print(status_message)160        return status_message, pd.DataFrame(results_log)161    except Exception as e:162        status_message = f"An unexpected error occurred during submission: {e}"163        print(status_message)164        return status_message, pd.DataFrame(results_log)165 166 167# --- Build Gradio Interface ---168with gr.Blocks() as demo:169    gr.Markdown("# Basic Agent Evaluation Runner")170    gr.Markdown(171        """172        **Instructions:**173        1. Log in to your Hugging Face account using the button below.174        2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.175        """176    )177    gr.LoginButton()178    run_button = gr.Button("Run Evaluation & Submit All Answers")179    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)180    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)181    run_button.click(182        fn=run_and_submit_all,183        outputs=[status_output, results_table]184    )185 186if __name__ == "__main__":187    print("\n" + "-" * 30 + " App Starting " + "-" * 30)188    space_host_startup = os.getenv("SPACE_HOST")189    space_id_startup = os.getenv("SPACE_ID")190    if space_host_startup:191        print(f"✅ SPACE_HOST found: {space_host_startup}")192        print(f"   Runtime URL should be: https://{space_host_startup}.hf.space")193    else:194        print("ℹ️  SPACE_HOST environment variable not found (running locally?).")195    if space_id_startup:196        print(f"✅ SPACE_ID found: {space_id_startup}")197        print(f"   Repo URL: https://huggingface.co/spaces/{space_id_startup}")198        print(f"   Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")199    else:200        print("ℹ️  SPACE_ID environment variable not found (running locally?).")201    print("-" * (60 + len(" App Starting ")) + "\n")202    print("Launching Gradio Interface for Basic Agent Evaluation...")203    demo.launch(debug=True, share=False)204