123Ayesha-Nawaz's picture
Update app.py
d5abeef verified
raw
history blame
2.55 kB
import os
import gradio as gr
import requests
import pandas as pd
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# --- Simple Working Agent ---
class BasicAgent:
def __init__(self):
print("Agent ready")
def __call__(self, question: str) -> str:
# Return a simple answer - you'll replace this with your logic
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
# Create agent
agent = BasicAgent()
# Get questions
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
# Answer questions
answers = []
results = []
for q in questions:
task_id = q.get("task_id")
question_text = q.get("question", "")
# Get answer from agent
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] + "..."
})
# Submit answers
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)
# --- Gradio Interface ---
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()