| |
| import os |
| import requests |
| import pandas as pd |
| import gradio as gr |
| from typing import Union |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| |
| |
| class BasicAgent: |
| def __init__(self): |
| print("β
BasicAgent initialized.") |
| |
| def __call__(self, question: str) -> str: |
| |
| print(f"π€ Agent received question: {question[:50]}...") |
| |
| |
| fixed_answer = "This is a default answer." |
| |
| print(f"π€ Agent returns: {fixed_answer}") |
| return fixed_answer |
|
|
|
|
| |
| def run_and_submit_all(profile: Union[gr.OAuthProfile, None]): |
| """ |
| Core function that: |
| - Initializes the agent |
| - Fetches questions |
| - Generates answers |
| - Submits them to the scoring API |
| - Returns the final result and answers DataFrame |
| """ |
| space_id = os.getenv("SPACE_ID") |
|
|
| if profile: |
| username = profile.username |
| print(f"π€ Logged in user: {username}") |
| else: |
| print("β οΈ User not logged in.") |
| return "Please login using Hugging Face Login button.", None |
|
|
| api_url = DEFAULT_API_URL |
| questions_url = f"{api_url}/questions" |
| submit_url = f"{api_url}/submit" |
| agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Not available" |
|
|
| |
| try: |
| agent = BasicAgent() |
| except Exception as e: |
| return f"β Error initializing agent: {e}", None |
|
|
| |
| print(f"π₯ Fetching questions from: {questions_url}") |
| try: |
| response = requests.get(questions_url, timeout=15) |
| response.raise_for_status() |
| questions_data = response.json() |
| if not questions_data: |
| return "β Fetched questions list is empty or invalid.", None |
| print(f"β
{len(questions_data)} questions fetched.") |
| except Exception as e: |
| return f"β Error fetching questions: {e}", None |
|
|
| |
| results_log = [] |
| answers_payload = [] |
|
|
| print("π§ Running agent on questions...") |
| for item in questions_data: |
| task_id = item.get("task_id") |
| question_text = item.get("question") |
| if not task_id or question_text is None: |
| continue |
| try: |
| submitted_answer = agent(question_text) |
| answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) |
| results_log.append({ |
| "Task ID": task_id, |
| "Question": question_text, |
| "Submitted Answer": submitted_answer |
| }) |
| except Exception as e: |
| results_log.append({ |
| "Task ID": task_id, |
| "Question": question_text, |
| "Submitted Answer": f"AGENT ERROR: {e}" |
| }) |
|
|
| if not answers_payload: |
| return "β Agent did not produce any answers to submit.", pd.DataFrame(results_log) |
|
|
| |
| submission_data = { |
| "username": username, |
| "agent_code": agent_code, |
| "answers": answers_payload |
| } |
| print(f"π Submitting {len(answers_payload)} answers...") |
|
|
| |
| try: |
| response = requests.post(submit_url, json=submission_data, timeout=60) |
| response.raise_for_status() |
| result_data = response.json() |
|
|
| final_status = ( |
| f"β
Submission Successful!\n" |
| f"π€ User: {result_data.get('username')}\n" |
| f"π Score: {result_data.get('score')}% " |
| f"({result_data.get('correct_count')}/{result_data.get('total_attempted')} correct)\n" |
| f"π Message: {result_data.get('message', 'No message.')}" |
| ) |
| return final_status, pd.DataFrame(results_log) |
|
|
| except requests.exceptions.HTTPError as e: |
| return f"β Submission Failed (HTTP error): {e}", pd.DataFrame(results_log) |
| except requests.exceptions.Timeout: |
| return "β Submission Failed: Request timed out.", pd.DataFrame(results_log) |
| except Exception as e: |
| return f"β Submission Failed: {e}", pd.DataFrame(results_log) |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# π€ Basic Agent Evaluation Tool") |
| gr.Markdown(""" |
| ### π Instructions: |
| 1. Clone this Hugging Face Space. |
| 2. Implement your own logic in the `BasicAgent` class. |
| 3. Login with your Hugging Face account. |
| 4. Press the button to run all questions through your agent and submit. |
| |
| **Note:** It may take some time depending on the number of questions and agent logic. |
| """) |
|
|
| |
| gr.LoginButton() |
| run_button = gr.Button("βΆοΈ Run Evaluation & Submit All Answers") |
| status_output = gr.Textbox(label="π Submission Status", lines=5, interactive=False) |
| results_table = gr.DataFrame(label="π Agent Answers Log", wrap=True) |
|
|
| |
| run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table]) |
|
|
| |
| if __name__ == "__main__": |
| print("\n" + "-" * 30 + " π App Starting " + "-" * 30) |
| space_host = os.getenv("SPACE_HOST") |
| space_id = os.getenv("SPACE_ID") |
|
|
| if space_host: |
| print(f"β
SPACE_HOST: {space_host}") |
| print(f"π App URL: https://{space_host}.hf.space") |
| else: |
| print("βΉοΈ SPACE_HOST not found (running locally?)") |
|
|
| if space_id: |
| print(f"β
SPACE_ID: {space_id}") |
| print(f"π¦ Repo: https://huggingface.co/spaces/{space_id}") |
| else: |
| print("βΉοΈ SPACE_ID not set") |
|
|
| print("-" * 60) |
| print("π§ Launching Gradio app...") |
| demo.launch(debug=True, share=False) |
|
|