Spaces:
Build error
Build error
import os | |
import gradio as gr | |
import requests | |
import pandas as pd | |
from rag_agent import BasicAgent | |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
def run_and_submit_all(profile: gr.OAuthProfile | None): | |
space_id = os.getenv("SPACE_ID") | |
username = profile.username if profile else None | |
if not username: | |
return "Please log in to Hugging Face.", None | |
agent = BasicAgent() | |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" | |
try: | |
response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15) | |
response.raise_for_status() | |
questions_data = response.json() | |
except Exception as e: | |
return f"Error fetching questions: {e}", None | |
answers_payload, results_log = [], [] | |
for item in questions_data: | |
task_id = item.get("task_id") | |
question = item.get("question") | |
if not task_id or not question: | |
continue | |
try: | |
answer = agent(question) | |
except Exception as e: | |
answer = f"ERROR: {e}" | |
answers_payload.append({"task_id": task_id, "submitted_answer": answer}) | |
results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer}) | |
submission_data = { | |
"username": username, | |
"agent_code": agent_code, | |
"answers": answers_payload | |
} | |
try: | |
res = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60) | |
res.raise_for_status() | |
result = res.json() | |
status = f"✅ Submitted! Score: {result.get('score')}% - {result.get('message')}" | |
return status, pd.DataFrame(results_log) | |
except Exception as e: | |
return f"Submission error: {e}", pd.DataFrame(results_log) | |
with gr.Blocks() as demo: | |
gr.Markdown("# 🧠 RAG Agent: Wikipedia + arXiv") | |
gr.Markdown("Login with Hugging Face and click below to evaluate your agent.") | |
gr.LoginButton() | |
run_button = gr.Button("Run Evaluation & Submit All Answers") | |
status = gr.Textbox(label="Status", lines=4) | |
table = gr.DataFrame(label="Results") | |
run_button.click(fn=run_and_submit_all, outputs=[status, table]) | |
if __name__ == "__main__": | |
demo.launch() | |