SantoshKumar1310 commited on
Commit
a07c528
Β·
verified Β·
1 Parent(s): 7e6b81d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -55
app.py CHANGED
@@ -1,71 +1,92 @@
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)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()