Update app.py
Browse files
app.py
CHANGED
|
@@ -1,129 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# β
correct backend API base URL
|
| 8 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
-
# --- Basic Agent Definition ---
|
| 11 |
-
# π customize this class to make your own agent smarter
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
def
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# --- Evaluation Logic ---
|
| 25 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 26 |
-
"""Fetches all questions, runs agent, submits answers, shows results."""
|
| 27 |
-
space_id = os.getenv("SPACE_ID") # for linking to code repo
|
| 28 |
-
|
| 29 |
-
if profile:
|
| 30 |
-
username = f"{profile.username}"
|
| 31 |
-
print(f"π€ Logged in as: {username}")
|
| 32 |
-
else:
|
| 33 |
-
return "Please log in with your Hugging Face account.", None
|
| 34 |
-
|
| 35 |
-
api_url = DEFAULT_API_URL
|
| 36 |
-
questions_url = f"{api_url}/questions"
|
| 37 |
-
submit_url = f"{api_url}/submit"
|
| 38 |
-
|
| 39 |
-
# --- Instantiate your agent ---
|
| 40 |
-
try:
|
| 41 |
-
agent = BasicAgent()
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"Error initializing agent: {e}", None
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if not questions_data:
|
| 55 |
-
return "No questions fetched.", None
|
| 56 |
-
print(f"β
{len(questions_data)} questions retrieved.")
|
| 57 |
-
except Exception as e:
|
| 58 |
-
return f"Error fetching questions: {e}", None
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
task_id = item.get("task_id")
|
| 66 |
-
question_text = item.get("question")
|
| 67 |
-
if not task_id or question_text is None:
|
| 68 |
-
continue
|
| 69 |
-
try:
|
| 70 |
-
submitted_answer = agent(question_text)
|
| 71 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 72 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Answer": submitted_answer})
|
| 73 |
-
except Exception as e:
|
| 74 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Answer": f"ERROR: {e}"})
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
print(f"π Submitting {len(answers_payload)} answers...")
|
| 82 |
|
| 83 |
-
|
|
|
|
| 84 |
try:
|
| 85 |
-
response = requests.
|
| 86 |
-
response.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
f"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# --- Build Gradio Interface ---
|
| 101 |
-
with gr.Blocks() as demo:
|
| 102 |
-
gr.Markdown("# π§ Basic Agent Evaluation Runner")
|
| 103 |
-
gr.Markdown(
|
| 104 |
-
"""
|
| 105 |
-
### Instructions
|
| 106 |
-
1οΈβ£ Clone this space on your Hugging Face profile.
|
| 107 |
-
2οΈβ£ Modify the `BasicAgent` class to add your logic.
|
| 108 |
-
3οΈβ£ Log in below, then click **Run Evaluation & Submit All Answers**.
|
| 109 |
-
|
| 110 |
-
---
|
| 111 |
-
The process might take a few minutes while the agent runs all questions.
|
| 112 |
-
You can enhance your agent with reasoning, web tools, or retrieval modules.
|
| 113 |
-
"""
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
gr.LoginButton()
|
| 117 |
-
run_button = gr.Button("π Run Evaluation & Submit All Answers")
|
| 118 |
-
|
| 119 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=6, interactive=False)
|
| 120 |
-
results_table = gr.DataFrame(label="π§Ύ Questions and Agent Answers")
|
| 121 |
-
|
| 122 |
-
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 123 |
-
|
| 124 |
-
# --- Run ---
|
| 125 |
-
if __name__ == "__main__":
|
| 126 |
-
print("\n" + "-" * 40)
|
| 127 |
-
print("π App Starting")
|
| 128 |
-
print("-" * 40)
|
| 129 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
+
BASE_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
class BasicAgent:
|
| 7 |
def __init__(self):
|
| 8 |
+
# You can initialize any tools, memory, or logic here
|
| 9 |
+
pass
|
| 10 |
|
| 11 |
+
def answer_question(self, question: str) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Implement your custom logic here.
|
| 14 |
+
For now, this just echoes the question.
|
| 15 |
+
"""
|
| 16 |
+
return f"This is my answer to: {question}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Initialize Streamlit app
|
| 19 |
+
st.set_page_config(page_title="Basic Agent Evaluation Runner", page_icon="π€", layout="centered")
|
| 20 |
+
st.title("π€ Basic Agent Evaluation Runner")
|
| 21 |
|
| 22 |
+
st.markdown("""
|
| 23 |
+
### Instructions:
|
| 24 |
+
1οΈβ£ Clone this space on your Hugging Face profile.
|
| 25 |
+
2οΈβ£ Modify the `BasicAgent` class with your logic.
|
| 26 |
+
3οΈβ£ Log in below and run evaluation.
|
| 27 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# --- UI for Hugging Face login ---
|
| 30 |
+
with st.expander("π Login"):
|
| 31 |
+
hf_token = st.text_input("Enter your Hugging Face token", type="password")
|
| 32 |
+
if hf_token:
|
| 33 |
+
st.success("β
Token saved!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# --- Initialize agent ---
|
| 36 |
+
agent = BasicAgent()
|
| 37 |
|
| 38 |
+
# --- Fetch questions ---
|
| 39 |
+
st.subheader("π Questions and Answers")
|
|
|
|
| 40 |
|
| 41 |
+
if st.button("π Run Evaluation & Submit All Answers"):
|
| 42 |
+
st.info("Fetching questions...")
|
| 43 |
try:
|
| 44 |
+
response = requests.get(f"{BASE_URL}/questions") # Correct endpoint
|
| 45 |
+
if response.status_code != 200:
|
| 46 |
+
st.error(f"Failed to fetch questions: {response.status_code} {response.reason}")
|
| 47 |
+
else:
|
| 48 |
+
data = response.json()
|
| 49 |
+
st.success(f"Fetched {len(data)} questions.")
|
| 50 |
+
answers = []
|
| 51 |
+
|
| 52 |
+
for q in data:
|
| 53 |
+
question = q.get("question", "")
|
| 54 |
+
task_id = q.get("task_id", "")
|
| 55 |
+
answer = agent.answer_question(question)
|
| 56 |
+
answers.append({
|
| 57 |
+
"task_id": task_id,
|
| 58 |
+
"answer": answer
|
| 59 |
+
})
|
| 60 |
+
st.write(f"**Q:** {question}")
|
| 61 |
+
st.write(f"**A:** {answer}")
|
| 62 |
+
|
| 63 |
+
# Submit answers
|
| 64 |
+
st.info("Submitting answers...")
|
| 65 |
+
submit_res = requests.post(f"{BASE_URL}/submit", json={"answers": answers})
|
| 66 |
+
if submit_res.status_code == 200:
|
| 67 |
+
st.success("β
Submission complete! Check leaderboard or logs.")
|
| 68 |
+
else:
|
| 69 |
+
st.error(f"Submission failed: {submit_res.status_code}")
|
| 70 |
except Exception as e:
|
| 71 |
+
st.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|