Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,112 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import
|
| 4 |
-
from agent import run_agent_on_question
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
else:
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
}
|
| 22 |
-
response = requests.post(f"{API_BASE}/submit", json=payload)
|
| 23 |
-
if response.status_code == 200:
|
| 24 |
-
return response.json()
|
| 25 |
-
else:
|
| 26 |
-
return {"message": "Submission failed.", "score": 0}
|
| 27 |
-
|
| 28 |
-
def run_and_submit():
|
| 29 |
-
print("Fetching questions...")
|
| 30 |
-
questions = fetch_questions()
|
| 31 |
-
print(f"Fetched {len(questions)} questions")
|
| 32 |
-
|
| 33 |
-
answers = []
|
| 34 |
-
for q in questions:
|
| 35 |
-
print(f"Running agent on task {q['task_id']}")
|
| 36 |
-
answer = run_agent_on_question(q)
|
| 37 |
-
answers.append({
|
| 38 |
-
"task_id": q["task_id"],
|
| 39 |
-
"submitted_answer": answer
|
| 40 |
-
})
|
| 41 |
-
|
| 42 |
-
username = get_hf_username()
|
| 43 |
-
code_link = get_code_link()
|
| 44 |
-
print(f"Submitting answers as {username} with code link {code_link}")
|
| 45 |
-
|
| 46 |
-
result = submit_answers(answers, username, code_link)
|
| 47 |
-
print(result)
|
| 48 |
-
return f"Score: {result.get('score', 0)}\nMessage: {result.get('message', 'No message')}"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from agent import run_agent_on_question # β
Import from your custom agent logic
|
| 6 |
+
|
| 7 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
|
| 9 |
+
class BasicAgent:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
print("β
Custom Agent initialized.")
|
| 12 |
+
def __call__(self, question: str) -> str:
|
| 13 |
+
print(f"π§ Agent received question: {question[:50]}...")
|
| 14 |
+
return run_agent_on_question({"question": question})
|
| 15 |
|
| 16 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 17 |
+
space_id = os.getenv("SPACE_ID")
|
| 18 |
+
|
| 19 |
+
if profile:
|
| 20 |
+
username = f"{profile.username}"
|
| 21 |
+
print(f"π User logged in: {username}")
|
| 22 |
else:
|
| 23 |
+
print("β User not logged in.")
|
| 24 |
+
return "Please Login to Hugging Face with the button.", None
|
| 25 |
+
|
| 26 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 27 |
+
print(f"π Agent code URL: {agent_code}")
|
| 28 |
+
|
| 29 |
+
# Instantiate agent
|
| 30 |
+
try:
|
| 31 |
+
agent = BasicAgent()
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"β Error initializing agent: {e}", None
|
| 34 |
|
| 35 |
+
# Fetch questions
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
questions_data = response.json()
|
| 40 |
+
if not questions_data:
|
| 41 |
+
return "β οΈ No questions fetched or invalid format.", None
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"β Error fetching questions: {e}", None
|
| 44 |
+
|
| 45 |
+
# Run agent
|
| 46 |
+
results_log = []
|
| 47 |
+
answers_payload = []
|
| 48 |
+
for item in questions_data:
|
| 49 |
+
task_id = item.get("task_id")
|
| 50 |
+
question_text = item.get("question")
|
| 51 |
+
if not task_id or not question_text:
|
| 52 |
+
continue
|
| 53 |
+
try:
|
| 54 |
+
answer = agent(question_text)
|
| 55 |
+
answers_payload.append({
|
| 56 |
+
"task_id": task_id,
|
| 57 |
+
"submitted_answer": answer
|
| 58 |
+
})
|
| 59 |
+
results_log.append({
|
| 60 |
+
"Task ID": task_id,
|
| 61 |
+
"Question": question_text,
|
| 62 |
+
"Submitted Answer": answer
|
| 63 |
+
})
|
| 64 |
+
except Exception as e:
|
| 65 |
+
results_log.append({
|
| 66 |
+
"Task ID": task_id,
|
| 67 |
+
"Question": question_text,
|
| 68 |
+
"Submitted Answer": f"AGENT ERROR: {e}"
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
if not answers_payload:
|
| 72 |
+
return "β No answers submitted.", pd.DataFrame(results_log)
|
| 73 |
+
|
| 74 |
+
submission_data = {
|
| 75 |
+
"username": username.strip(),
|
| 76 |
+
"agent_code": agent_code,
|
| 77 |
+
"answers": answers_payload
|
| 78 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
try:
|
| 81 |
+
response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
|
| 82 |
+
response.raise_for_status()
|
| 83 |
+
result_data = response.json()
|
| 84 |
+
final_status = (
|
| 85 |
+
f"β
Submission Successful!\n"
|
| 86 |
+
f"User: {result_data.get('username')}\n"
|
| 87 |
+
f"Score: {result_data.get('score')}% "
|
| 88 |
+
f"({result_data.get('correct_count')}/{result_data.get('total_attempted')} correct)\n"
|
| 89 |
+
f"Message: {result_data.get('message')}"
|
| 90 |
+
)
|
| 91 |
+
return final_status, pd.DataFrame(results_log)
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return f"β Submission Failed: {e}", pd.DataFrame(results_log)
|
| 94 |
+
|
| 95 |
+
# ---- Gradio UI ----
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 98 |
+
gr.Markdown("""
|
| 99 |
+
**Instructions:**
|
| 100 |
+
1. Log in to Hugging Face.
|
| 101 |
+
2. Click the button to evaluate your agent and submit answers.
|
| 102 |
+
---
|
| 103 |
+
""")
|
| 104 |
+
gr.LoginButton()
|
| 105 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 106 |
+
status_output = gr.Textbox(label="Submission Result", lines=5)
|
| 107 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 108 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 109 |
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
print("π Launching Agent Space...")
|
| 112 |
+
demo.launch(debug=True)
|