Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
# CONFIGURATION
|
| 6 |
-
# ==============================
|
| 7 |
API_BASE = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
# ==============================
|
| 12 |
-
class BasicAgent:
|
| 13 |
-
def __init__(self):
|
| 14 |
-
pass
|
| 15 |
-
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
# Simple example agent — replace with your own logic
|
| 18 |
-
# You can later integrate a Hugging Face model or reasoning steps
|
| 19 |
-
if "capital" in question.lower():
|
| 20 |
-
return "Paris"
|
| 21 |
-
return "I need to research that."
|
| 22 |
-
|
| 23 |
-
# ==============================
|
| 24 |
-
# API INTERACTION FUNCTIONS
|
| 25 |
-
# ==============================
|
| 26 |
def fetch_questions():
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
def submit_answers(username, agent_code_link, answers):
|
| 35 |
-
payload = {
|
| 36 |
-
"username": username,
|
| 37 |
-
"agent_code": agent_code_link,
|
| 38 |
-
"answers": answers
|
| 39 |
-
}
|
| 40 |
-
res = requests.post(f"{API_BASE}/submit", json=payload)
|
| 41 |
-
return res.json()
|
| 42 |
-
|
| 43 |
-
# ==============================
|
| 44 |
-
# GRADIO UI LOGIC
|
| 45 |
-
# ==============================
|
| 46 |
-
def run_evaluation(username, agent_code_link):
|
| 47 |
-
agent = BasicAgent()
|
| 48 |
-
data = fetch_questions()
|
| 49 |
-
|
| 50 |
-
if "error" in data:
|
| 51 |
-
return f"❌ Error fetching questions: {data['error']}", None
|
| 52 |
-
|
| 53 |
-
results = []
|
| 54 |
-
for q in data:
|
| 55 |
-
task_id = q["task_id"]
|
| 56 |
-
question = q["question"]
|
| 57 |
-
answer = agent(question)
|
| 58 |
-
results.append({"task_id": task_id, "submitted_answer": answer})
|
| 59 |
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
# === CONFIG ===
|
|
|
|
|
|
|
| 6 |
API_BASE = "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
|
| 8 |
+
# === CORE FUNCTIONS ===
|
| 9 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def fetch_questions():
|
| 11 |
+
"""Fetch all available questions."""
|
| 12 |
try:
|
| 13 |
+
response = requests.get(f"{API_BASE}/questions")
|
| 14 |
+
response.raise_for_status()
|
| 15 |
+
questions = response.json()
|
| 16 |
+
return questions
|
| 17 |
except Exception as e:
|
| 18 |
+
return f"⚠️ Error fetching questions: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def fetch_random_question():
|
| 21 |
+
"""Fetch a random question."""
|
| 22 |
try:
|
| 23 |
+
response = requests.get(f"{API_BASE}/random-question")
|
| 24 |
+
response.raise_for_status()
|
| 25 |
+
q = response.json()
|
| 26 |
+
return f"**Task ID:** {q['task_id']}\n\n**Question:** {q['question']}"
|
| 27 |
except Exception as e:
|
| 28 |
+
return f"⚠️ Error fetching random question: {e}"
|
| 29 |
+
|
| 30 |
+
def submit_answers(username, code_link, answers_json):
|
| 31 |
+
"""Submit answers and return the score."""
|
| 32 |
+
try:
|
| 33 |
+
answers = json.loads(answers_json)
|
| 34 |
+
payload = {
|
| 35 |
+
"username": username,
|
| 36 |
+
"agent_code": code_link,
|
| 37 |
+
"answers": answers
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
response = requests.post(f"{API_BASE}/submit", json=payload)
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
result = response.json()
|
| 44 |
+
return json.dumps(result, indent=2)
|
| 45 |
+
|
| 46 |
+
except json.JSONDecodeError:
|
| 47 |
+
return "❌ Invalid JSON format for answers. Make sure it's a valid list of objects."
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"⚠️ Submission failed: {e}"
|
| 50 |
+
|
| 51 |
+
# === DEMO / UI ===
|
| 52 |
+
|
| 53 |
+
def auto_agent():
|
| 54 |
+
"""Simple rule-based agent for demo purposes."""
|
| 55 |
+
questions = fetch_questions()
|
| 56 |
+
if isinstance(questions, str):
|
| 57 |
+
return questions # Error message
|
| 58 |
+
|
| 59 |
+
answers = []
|
| 60 |
+
for q in questions[:5]: # just answer first 5 for example
|
| 61 |
+
answers.append({
|
| 62 |
+
"task_id": q["task_id"],
|
| 63 |
+
"submitted_answer": "42" # dummy placeholder
|
| 64 |
+
})
|
| 65 |
+
return json.dumps(answers, indent=2)
|
| 66 |
+
|
| 67 |
+
# === GRADIO UI ===
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
+
gr.Markdown("## 🤖 GAIA Agent Submission Portal")
|
| 71 |
+
gr.Markdown("This interface lets you test your agent, view questions, and submit your answers for leaderboard scoring.")
|
| 72 |
+
|
| 73 |
+
with gr.Tab("📋 Fetch Questions"):
|
| 74 |
+
fetch_all_btn = gr.Button("Fetch All Questions")
|
| 75 |
+
questions_output = gr.Textbox(label="All Questions", lines=10)
|
| 76 |
+
fetch_all_btn.click(fetch_questions, outputs=questions_output)
|
| 77 |
+
|
| 78 |
+
fetch_random_btn = gr.Button("Fetch Random Question")
|
| 79 |
+
random_output = gr.Markdown()
|
| 80 |
+
fetch_random_btn.click(fetch_random_question, outputs=random_output)
|
| 81 |
+
|
| 82 |
+
with gr.Tab("🧠 Agent Answers"):
|
| 83 |
+
gr.Markdown("Generate dummy answers to test the submission structure.")
|
| 84 |
+
gen_btn = gr.Button("Generate Sample Answers")
|
| 85 |
+
answers_box = gr.Textbox(label="Generated Answers (Editable)", lines=10)
|
| 86 |
+
gen_btn.click(auto_agent, outputs=answers_box)
|
| 87 |
+
|
| 88 |
+
with gr.Tab("🚀 Submit Answers"):
|
| 89 |
+
username = gr.Textbox(label="Your Hugging Face Username")
|
| 90 |
+
code_link = gr.Textbox(label="Public Space Code Link (e.g. https://huggingface.co/spaces/yourname/yourspace/tree/main)")
|
| 91 |
+
answers_json = gr.Textbox(label="Answers JSON", lines=10)
|
| 92 |
+
submit_btn = gr.Button("Submit to Leaderboard")
|
| 93 |
+
result_box = gr.Textbox(label="Result / Score", lines=10)
|
| 94 |
+
submit_btn.click(submit_answers, inputs=[username, code_link, answers_json], outputs=result_box)
|
| 95 |
+
|
| 96 |
+
gr.Markdown("---")
|
| 97 |
+
gr.Markdown("💡 **Tip:** Replace the dummy answers with your real agent’s output to get scored.")
|
| 98 |
+
|
| 99 |
+
# === RUN ===
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo.launch()
|