hasanain9's picture
test3
ae8739e verified
raw
history blame
3.63 kB
import os
import gradio as gr
import requests
import pandas as pd
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# --- Agent Definition (replace with your own logic) ---
class BasicAgent:
def __init__(self):
print("βœ… BasicAgent initialized.")
def __call__(self, question: str) -> str:
print(f"πŸ“₯ Question received: {question[:60]}...")
return "Paris" if "capital of France" in question else "42"
# --- Evaluation Function ---
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID") or "your-username/your-space" # fallback
if not profile:
return "⚠️ Please log in to Hugging Face to submit.", None
username = profile.username
print(f"πŸ” Logged in as: {username}")
questions_url = f"{DEFAULT_API_URL}/questions"
submit_url = f"{DEFAULT_API_URL}/submit"
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
# Instantiate your agent
agent = BasicAgent()
# Fetch questions
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions = response.json()
print(f"πŸ“¦ {len(questions)} questions fetched.")
except Exception as e:
return f"❌ Failed to fetch questions: {e}", None
# Process answers
answers_payload = []
logs = []
for q in questions:
task_id = q.get("task_id")
question_text = q.get("question")
if not task_id or not question_text:
continue
try:
answer = agent(question_text)
except Exception as e:
answer = f"AGENT ERROR: {e}"
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
logs.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
print(f"βœ… Task ID: {task_id} | Answer: {answer}")
# Submit answers
submission = {
"username": username,
"agent_code": agent_code,
"answers": answers_payload,
}
try:
res = requests.post(submit_url, json=submission, timeout=60)
res.raise_for_status()
result = res.json()
status = (
f"πŸŽ‰ Submission Successful!\n"
f"User: {result.get('username', 'N/A')}\n"
f"Score: {result.get('score', 0)}%\n"
f"Correct: {result.get('correct_count', 0)}/{result.get('total_attempted', 0)}\n"
f"Message: {result.get('message', '')}"
)
except Exception as e:
status = f"❌ Submission failed: {e}"
return status, pd.DataFrame(logs)
# --- Gradio UI ---
with gr.Blocks() as demo:
gr.Markdown("# πŸ€– GAIA Evaluation Agent")
gr.Markdown("""
1. Log in with your Hugging Face account below.
2. Click the button to evaluate and submit answers.
3. Your score and submission details will appear below.
""")
profile_input = gr.OAuthProfile()
run_button = gr.Button("πŸš€ Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Submission Result", lines=6, interactive=False)
results_table = gr.DataFrame(label="Answer Log")
run_button.click(
fn=run_and_submit_all,
inputs=[profile_input],
outputs=[status_output, results_table]
)
if __name__ == "__main__":
print("\n================== GAIA Agent App Starting ==================")
if os.getenv("SPACE_ID"):
print(f"πŸ“¦ SPACE_ID = {os.getenv('SPACE_ID')}")
else:
print("ℹ️ No SPACE_ID found. Using fallback link.")
demo.launch(debug=True)