CoolFace
Apppublic

csr-01/Final_Assignment_Template

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py217 linesDownload Raw Back to root
1import os2import gradio as gr3import requests4import inspect5import pandas as pd6import yaml7from smolagents import CodeAgent,DuckDuckGoSearchTool, LiteLLMModel,load_tool,tool, GoogleSearchTool8 9# (Keep Constants as is)10# --- Constants ---11DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"12 13# --- Basic Agent Definition ---14# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------15 16model = LiteLLMModel(17    model_id="gemini/gemini-2.0-flash", # you can see other model names here: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models. It is important to prefix the name with "gemini/"18    api_key=os.getenv("GOOGLE_API_KEY"),19    max_tokens=819220)21 22 23# agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model)24agent = CodeAgent(tools=[GoogleSearchTool("serper")], model=model)25 26class BasicAgent:27    def __init__(self):28        print("BasicAgent initialized.")29    def __call__(self, question: str) -> str:30        print(f"Agent received question (first 50 chars): {question[:50]}...")31        32        fixed_answer = agent.run(f"""33        You are a helpful assistant tasked with answering questions using a set of tools. Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: 34        FINAL ANSWER: [YOUR FINAL ANSWER]. 35        YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.36        Your answer should only start with "FINAL ANSWER: ", then follows with the answer.37        38        {question}39        """)40        print(f"Agent returning fixed answer: {fixed_answer}")41        return fixed_answer42 43def run_and_submit_all( profile: gr.OAuthProfile | None):44    """45    Fetches all questions, runs the BasicAgent on them, submits all answers,46    and displays the results.47    """48    # --- Determine HF Space Runtime URL and Repo URL ---49    space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code50 51    if profile:52        username= f"{profile.username}"53        print(f"User logged in: {username}")54    else:55        print("User not logged in.")56        return "Please Login to Hugging Face with the button.", None57 58    api_url = DEFAULT_API_URL59    questions_url = f"{api_url}/questions"60    submit_url = f"{api_url}/submit"61 62    # 1. Instantiate Agent ( modify this part to create your agent)63    try:64        agent = BasicAgent()65    except Exception as e:66        print(f"Error instantiating agent: {e}")67        return f"Error initializing agent: {e}", None68    # 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)69    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"70    print(agent_code)71 72    # 2. Fetch Questions73    print(f"Fetching questions from: {questions_url}")74    try:75        response = requests.get(questions_url, timeout=15)76        response.raise_for_status()77        questions_data = response.json()78        if not questions_data:79             print("Fetched questions list is empty.")80             return "Fetched questions list is empty or invalid format.", None81        print(f"Fetched {len(questions_data)} questions.")82    except requests.exceptions.RequestException as e:83        print(f"Error fetching questions: {e}")84        return f"Error fetching questions: {e}", None85    except requests.exceptions.JSONDecodeError as e:86         print(f"Error decoding JSON response from questions endpoint: {e}")87         print(f"Response text: {response.text[:500]}")88         return f"Error decoding server response for questions: {e}", None89    except Exception as e:90        print(f"An unexpected error occurred fetching questions: {e}")91        return f"An unexpected error occurred fetching questions: {e}", None92 93    # 3. Run your Agent94    results_log = []95    answers_payload = []96    print(f"Running agent on {len(questions_data)} questions...")97    for item in questions_data:98        task_id = item.get("task_id")99        question_text = item.get("question")100        if not task_id or question_text is None:101            print(f"Skipping item with missing task_id or question: {item}")102            continue103        try:104            submitted_answer = agent(question_text)105            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})106            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})107        except Exception as e:108             print(f"Error running agent on task {task_id}: {e}")109             results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})110 111    if not answers_payload:112        print("Agent did not produce any answers to submit.")113        return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)114 115    # 4. Prepare Submission 116    submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}117    status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."118    print(status_update)119 120    # 5. Submit121    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")122    try:123        response = requests.post(submit_url, json=submission_data, timeout=60)124        response.raise_for_status()125        result_data = response.json()126        final_status = (127            f"Submission Successful!\n"128            f"User: {result_data.get('username')}\n"129            f"Overall Score: {result_data.get('score', 'N/A')}% "130            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"131            f"Message: {result_data.get('message', 'No message received.')}"132        )133        print("Submission successful.")134        results_df = pd.DataFrame(results_log)135        return final_status, results_df136    except requests.exceptions.HTTPError as e:137        error_detail = f"Server responded with status {e.response.status_code}."138        try:139            error_json = e.response.json()140            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"141        except requests.exceptions.JSONDecodeError:142            error_detail += f" Response: {e.response.text[:500]}"143        status_message = f"Submission Failed: {error_detail}"144        print(status_message)145        results_df = pd.DataFrame(results_log)146        return status_message, results_df147    except requests.exceptions.Timeout:148        status_message = "Submission Failed: The request timed out."149        print(status_message)150        results_df = pd.DataFrame(results_log)151        return status_message, results_df152    except requests.exceptions.RequestException as e:153        status_message = f"Submission Failed: Network error - {e}"154        print(status_message)155        results_df = pd.DataFrame(results_log)156        return status_message, results_df157    except Exception as e:158        status_message = f"An unexpected error occurred during submission: {e}"159        print(status_message)160        results_df = pd.DataFrame(results_log)161        return status_message, results_df162 163 164# --- Build Gradio Interface using Blocks ---165with gr.Blocks() as demo:166    gr.Markdown("# Basic Agent Evaluation Runner")167    gr.Markdown(168        """169        **Instructions:**170 171        1.  Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...172        2.  Log in to your Hugging Face account using the button below. This uses your HF username for submission.173        3.  Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.174 175        ---176        **Disclaimers:**177        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).178        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.179        """180    )181 182    gr.LoginButton()183 184    run_button = gr.Button("Run Evaluation & Submit All Answers")185 186    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)187    # Removed max_rows=10 from DataFrame constructor188    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)189 190    run_button.click(191        fn=run_and_submit_all,192        outputs=[status_output, results_table]193    )194 195if __name__ == "__main__":196    print("\n" + "-"*30 + " App Starting " + "-"*30)197    # Check for SPACE_HOST and SPACE_ID at startup for information198    space_host_startup = os.getenv("SPACE_HOST")199    space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup200 201    if space_host_startup:202        print(f"✅ SPACE_HOST found: {space_host_startup}")203        print(f"   Runtime URL should be: https://{space_host_startup}.hf.space")204    else:205        print("ℹ️  SPACE_HOST environment variable not found (running locally?).")206 207    if space_id_startup: # Print repo URLs if SPACE_ID is found208        print(f"✅ SPACE_ID found: {space_id_startup}")209        print(f"   Repo URL: https://huggingface.co/spaces/{space_id_startup}")210        print(f"   Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")211    else:212        print("ℹ️  SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")213 214    print("-"*(60 + len(" App Starting ")) + "\n")215 216    print("Launching Gradio Interface for Basic Agent Evaluation...")217    demo.launch(debug=True, share=False)