Team Ai
Apppublic

LukaszBergiel/Final_Assignment_Template_OpenAPI

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py197 linesDownload Raw Back to root
1import os2import gradio as gr3import requests4import inspect5import pandas as pd6from agent import BasicAgent7 8# (Keep Constants as is)9# --- Constants ---10DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"11 12# --- Basic Agent Definition ---13# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------14 15def run_and_submit_all(api_key: str, profile: gr.OAuthProfile | None = None):16    """17    Fetches all questions, runs the BasicAgent on them, submits all answers,18    and displays the results.19    """20    # --- Determine HF Space Runtime URL and Repo URL ---21    space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code22 23    if profile:24        username= f"{profile.username}"25        print(f"User logged in: {username}")26    else:27        print("User not logged in.")28        return "Please Login to Hugging Face with the button.", None29 30    api_url = DEFAULT_API_URL31    questions_url = f"{api_url}/questions"32    submit_url = f"{api_url}/submit"33 34    # 1. Instantiate Agent ( modify this part to create your agent)35    try:36        agent = BasicAgent(api_key=api_key)37    except Exception as e:38        print(f"Error instantiating agent: {e}")39        return f"Error initializing agent: {e}", None40    # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)41    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"42    print(agent_code)43 44    # 2. Fetch Questions45    print(f"Fetching questions from: {questions_url}")46    try:47        response = requests.get(questions_url, timeout=15)48        response.raise_for_status()49        questions_data = response.json()50        if not questions_data:51             print("Fetched questions list is empty.")52             return "Fetched questions list is empty or invalid format.", None53        print(f"Fetched {len(questions_data)} questions.")54    except requests.exceptions.RequestException as e:55        print(f"Error fetching questions: {e}")56        return f"Error fetching questions: {e}", None57    except requests.exceptions.JSONDecodeError as e:58         print(f"Error decoding JSON response from questions endpoint: {e}")59         print(f"Response text: {response.text[:500]}")60         return f"Error decoding server response for questions: {e}", None61    except Exception as e:62        print(f"An unexpected error occurred fetching questions: {e}")63        return f"An unexpected error occurred fetching questions: {e}", None64 65    # 3. Run your Agent66    results_log = []67    answers_payload = []68    print(f"Running agent on {len(questions_data)} questions...")69    for item in questions_data:70        task_id = item.get("task_id")71        question_text = item.get("question")72        if not task_id or question_text is None:73            print(f"Skipping item with missing task_id or question: {item}")74            continue75        try:76            submitted_answer = agent(question_text)77            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})78            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})79        except Exception as e:80             print(f"Error running agent on task {task_id}: {e}")81             results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})82 83    if not answers_payload:84        print("Agent did not produce any answers to submit.")85        return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)86 87    # 4. Prepare Submission 88    submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}89    status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."90    print(status_update)91 92    # 5. Submit93    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")94    try:95        response = requests.post(submit_url, json=submission_data, timeout=60)96        response.raise_for_status()97        result_data = response.json()98        final_status = (99            f"Submission Successful!\n"100            f"User: {result_data.get('username')}\n"101            f"Overall Score: {result_data.get('score', 'N/A')}% "102            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"103            f"Message: {result_data.get('message', 'No message received.')}"104        )105        print("Submission successful.")106        results_df = pd.DataFrame(results_log)107        return final_status, results_df108    except requests.exceptions.HTTPError as e:109        error_detail = f"Server responded with status {e.response.status_code}."110        try:111            error_json = e.response.json()112            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"113        except requests.exceptions.JSONDecodeError:114            error_detail += f" Response: {e.response.text[:500]}"115        status_message = f"Submission Failed: {error_detail}"116        print(status_message)117        results_df = pd.DataFrame(results_log)118        return status_message, results_df119    except requests.exceptions.Timeout:120        status_message = "Submission Failed: The request timed out."121        print(status_message)122        results_df = pd.DataFrame(results_log)123        return status_message, results_df124    except requests.exceptions.RequestException as e:125        status_message = f"Submission Failed: Network error - {e}"126        print(status_message)127        results_df = pd.DataFrame(results_log)128        return status_message, results_df129    except Exception as e:130        status_message = f"An unexpected error occurred during submission: {e}"131        print(status_message)132        results_df = pd.DataFrame(results_log)133        return status_message, results_df134 135 136# --- Build Gradio Interface using Blocks ---137with gr.Blocks() as demo:138    gr.Markdown("# Basic Agent Evaluation Runner")139    gr.Markdown(140        """141        **Instructions:**142 143        1.  Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...144        2.  Log in to your Hugging Face account using the button below. This uses your HF username for submission.145        3.  Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.146 147        ---148        **Disclaimers:**149        Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).150        This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.151        """152    )153 154    # Add API Key input field155    api_key_input = gr.Textbox(156        label="API Key",157        placeholder="Enter your OpenAI API key here (sk-...)",158        type="password"159    )160    161    gr.LoginButton()162 163    run_button = gr.Button("Run Evaluation & Submit All Answers")164 165    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)166    # Removed max_rows=10 from DataFrame constructor167    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)168 169    run_button.click(170        fn=run_and_submit_all,171        inputs=[api_key_input],172        outputs=[status_output, results_table]173    )174 175if __name__ == "__main__":176    print("\n" + "-"*30 + " App Starting " + "-"*30)177    # Check for SPACE_HOST and SPACE_ID at startup for information178    space_host_startup = os.getenv("SPACE_HOST")179    space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup180 181    if space_host_startup:182        print(f"✅ SPACE_HOST found: {space_host_startup}")183        print(f"   Runtime URL should be: https://{space_host_startup}.hf.space")184    else:185        print("ℹ️  SPACE_HOST environment variable not found (running locally?).")186 187    if space_id_startup: # Print repo URLs if SPACE_ID is found188        print(f"✅ SPACE_ID found: {space_id_startup}")189        print(f"   Repo URL: https://huggingface.co/spaces/{space_id_startup}")190        print(f"   Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")191    else:192        print("ℹ️  SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")193 194    print("-"*(60 + len(" App Starting ")) + "\n")195 196    print("Launching Gradio Interface for Basic Agent Evaluation...")197    demo.launch(debug=True, share=False)