Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,92 @@
|
|
| 1 |
-
import
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
class BasicAgent:
|
| 7 |
def __init__(self):
|
| 8 |
-
# You can initialize any tools, memory, or logic here
|
| 9 |
pass
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
if st.button("π Run Evaluation & Submit All Answers"):
|
| 42 |
-
st.info("Fetching questions...")
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
else:
|
| 48 |
-
data = response.json()
|
| 49 |
-
st.success(f"Fetched {len(data)} questions.")
|
| 50 |
-
answers = []
|
| 51 |
|
| 52 |
-
|
| 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 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# BASIC AGENT CLASS
|
| 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 |
+
res = requests.get(f"{API_BASE}/questions")
|
| 29 |
+
res.raise_for_status()
|
| 30 |
+
return res.json()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return {"error": str(e)}
|
| 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 |
+
result = submit_answers(username, agent_code_link, results)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
result = {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
return result, results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# ==============================
|
| 68 |
+
# GRADIO INTERFACE
|
| 69 |
+
# ==============================
|
| 70 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 71 |
+
gr.Markdown("# π Basic Agent Evaluation Runner")
|
| 72 |
+
gr.Markdown("""
|
| 73 |
+
1οΈβ£ **Clone this Space** on your Hugging Face profile.
|
| 74 |
+
2οΈβ£ **Modify the `BasicAgent`** class with your logic.
|
| 75 |
+
3οΈβ£ **Log in** and run evaluation to test your agent.
|
| 76 |
+
""")
|
| 77 |
+
|
| 78 |
+
username = gr.Textbox(label="Your Hugging Face Username")
|
| 79 |
+
agent_code_link = gr.Textbox(label="Public Hugging Face Space Code Link", placeholder="https://huggingface.co/spaces/YourName/YourSpace/tree/main")
|
| 80 |
+
|
| 81 |
+
run_btn = gr.Button("Run Evaluation & Submit All Answers π")
|
| 82 |
+
|
| 83 |
+
output_status = gr.JSON(label="Run Status / Submission Result")
|
| 84 |
+
output_table = gr.JSON(label="Questions and Agent Answers")
|
| 85 |
+
|
| 86 |
+
run_btn.click(
|
| 87 |
+
fn=run_evaluation,
|
| 88 |
+
inputs=[username, agent_code_link],
|
| 89 |
+
outputs=[output_status, output_table]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
demo.launch()
|