SantoshKumar1310 commited on
Commit
a0c3a50
Β·
verified Β·
1 Parent(s): 6bfe482

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -115
app.py CHANGED
@@ -1,129 +1,71 @@
1
- import os
2
- import gradio as gr
3
  import requests
4
- import pandas as pd
5
 
6
- # --- Constants ---
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
- print("βœ… BasicAgent initialized.")
 
15
 
16
- def __call__(self, question: str) -> str:
17
- print(f"Agent received question: {question[:50]}...")
18
- # For now, it returns a placeholder answer
19
- fixed_answer = "This is a default answer."
20
- print(f"Agent returning: {fixed_answer}")
21
- return fixed_answer
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
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "N/A"
46
- print(f"πŸ”— Code link: {agent_code}")
 
47
 
48
- # --- Fetch Questions ---
49
- print(f"πŸ“‘ Fetching from {questions_url}")
50
- try:
51
- response = requests.get(questions_url, timeout=15)
52
- response.raise_for_status()
53
- questions_data = response.json()
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
- # --- Run Agent ---
61
- results_log = []
62
- answers_payload = []
63
- print(f"πŸ€– Running agent on {len(questions_data)} questions...")
64
- for item in questions_data:
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
- if not answers_payload:
77
- return "No answers produced by the agent.", pd.DataFrame(results_log)
78
 
79
- # --- Prepare Submission ---
80
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
81
- print(f"πŸš€ Submitting {len(answers_payload)} answers...")
82
 
83
- # --- Submit ---
 
84
  try:
85
- response = requests.post(submit_url, json=submission_data, timeout=60)
86
- response.raise_for_status()
87
- result_data = response.json()
88
- final_status = (
89
- f"βœ… Submission Successful!\n"
90
- f"User: {result_data.get('username')}\n"
91
- f"Score: {result_data.get('score', 'N/A')}%\n"
92
- f"Correct: {result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')}\n"
93
- f"Message: {result_data.get('message', 'No message received.')}"
94
- )
95
- return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  except Exception as e:
97
- return f"Submission failed: {e}", pd.DataFrame(results_log)
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)}")