| import os |
| import gradio as gr |
| import requests |
| import pandas as pd |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| |
| class BasicAgent: |
| def __init__(self): |
| print("Agent ready") |
| |
| def __call__(self, question: str) -> str: |
| |
| return "This is a placeholder answer. Replace with your actual answer logic." |
|
|
| def run_and_submit_all(profile: gr.OAuthProfile | None): |
| """Run agent on all questions and submit answers""" |
| |
| if not profile: |
| return "Please login first", None |
| |
| username = profile.username |
| space_id = os.getenv("SPACE_ID", "local") |
| api_url = DEFAULT_API_URL |
| |
| |
| agent = BasicAgent() |
| |
| |
| try: |
| response = requests.get(f"{api_url}/questions", timeout=10) |
| questions = response.json() |
| except Exception as e: |
| return f"Failed to get questions: {e}", None |
| |
| |
| answers = [] |
| results = [] |
| |
| for q in questions: |
| task_id = q.get("task_id") |
| question_text = q.get("question", "") |
| |
| |
| answer = agent(question_text) |
| |
| answers.append({ |
| "task_id": task_id, |
| "submitted_answer": answer |
| }) |
| |
| results.append({ |
| "Task ID": task_id, |
| "Question": question_text[:50] + "...", |
| "Answer": answer[:50] + "..." |
| }) |
| |
| |
| try: |
| response = requests.post( |
| f"{api_url}/submit", |
| json={ |
| "username": username, |
| "agent_code": f"https://huggingface.co/spaces/{space_id}", |
| "answers": answers |
| }, |
| timeout=30 |
| ) |
| |
| result = response.json() |
| status = f"Score: {result.get('score', 0)}% ({result.get('correct_count', 0)}/20 correct)" |
| |
| return status, pd.DataFrame(results) |
| |
| except Exception as e: |
| return f"Submission failed: {e}", pd.DataFrame(results) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# GIAIA 20 Questions Evaluation") |
| |
| gr.LoginButton() |
| btn = gr.Button("Run on 20 Questions") |
| status = gr.Textbox(label="Result") |
| table = gr.DataFrame(label="Answers Preview") |
| |
| btn.click(run_and_submit_all, outputs=[status, table]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |