Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import requests | |
| import pandas as pd | |
| import time | |
| from langchain_core.messages import HumanMessage | |
| from agent import build_graph | |
| # Constants | |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
| class BasicAgent: | |
| def __init__(self): | |
| print("π€ BasicAgent initialized.") | |
| self.graph = build_graph() | |
| def __call__(self, question: str) -> str: | |
| print(f"π€ Processing: {question[:50]}...") | |
| try: | |
| messages = [HumanMessage(content=question)] | |
| result = self.graph.invoke({"messages": messages}) | |
| answer = result['messages'][-1].content | |
| # Simple cleaning | |
| answer = answer.strip() | |
| if answer.startswith("Assistant: "): | |
| answer = answer[11:] | |
| print(f"β Answer: {answer}") | |
| return answer | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| return f"Error: {str(e)}" | |
| def run_evaluation(profile): | |
| if not profile: | |
| return "β Please login with HuggingFace first!", None | |
| print(f"π Starting evaluation for user: {profile.username}") | |
| try: | |
| # Initialize agent | |
| agent = BasicAgent() | |
| print("β Agent initialized successfully") | |
| # Get questions | |
| questions_url = f"{DEFAULT_API_URL}/questions" | |
| print(f"π₯ Fetching questions from: {questions_url}") | |
| response = requests.get(questions_url, timeout=30) | |
| questions = response.json() | |
| print(f"π Got {len(questions)} questions") | |
| # Process questions | |
| answers = [] | |
| results = [] | |
| for i, q in enumerate(questions): # Run on all questions | |
| task_id = q.get("task_id") | |
| question_text = q.get("question") | |
| print(f"\nπ Question {i+1}/{len(questions)}: {task_id}") | |
| try: | |
| answer = agent(question_text) | |
| answers.append({"task_id": task_id, "submitted_answer": answer}) | |
| results.append({ | |
| "Task ID": task_id, | |
| "Question": question_text[:100] + "...", | |
| "Answer": answer | |
| }) | |
| time.sleep(5) # Increased delay | |
| except Exception as e: | |
| print(f"β Error on question {task_id}: {e}") | |
| results.append({ | |
| "Task ID": task_id, | |
| "Question": question_text[:100] + "...", | |
| "Answer": f"ERROR: {e}" | |
| }) | |
| # Submit answers | |
| space_id = os.getenv("SPACE_ID", "Supan23/gaia-agent") | |
| submit_data = { | |
| "username": profile.username, | |
| "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main", | |
| "answers": answers | |
| } | |
| submit_url = f"{DEFAULT_API_URL}/submit" | |
| print(f"π€ Submitting to: {submit_url}") | |
| response = requests.post(submit_url, json=submit_data, timeout=60) | |
| result = response.json() | |
| status = f"""π EVALUATION COMPLETE! | |
| π€ User: {result.get('username')} | |
| π Score: {result.get('score', 0)}% | |
| β Correct: {result.get('correct_count', 0)}/{result.get('total_attempted', 0)} | |
| π¬ Message: {result.get('message', 'No message')} | |
| {'π CERTIFICATE ELIGIBLE!' if result.get('score', 0) >= 30 else 'π Need 30% for certificate'} | |
| """ | |
| return status, pd.DataFrame(results) | |
| except Exception as e: | |
| error_msg = f"β Evaluation failed: {str(e)}" | |
| print(error_msg) | |
| return error_msg, None | |
| # Create the interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π GAIA Agent - Certificate Test") | |
| gr.Markdown("**Click Login, then click Run Test to get your certificate!**") | |
| gr.LoginButton() | |
| run_btn = gr.Button("π Run Certificate Test", variant="primary") | |
| status = gr.Textbox(label="π Results", lines=10) | |
| table = gr.DataFrame(label="π Questions & Answers") | |
| run_btn.click(run_evaluation, outputs=[status, table]) | |
| if __name__ == "__main__": | |
| demo.launch() | |