rakesh-dvg commited on
Commit
f0d1562
·
verified ·
1 Parent(s): fef27d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -92
app.py CHANGED
@@ -1,38 +1,13 @@
1
  """ Basic Agent Evaluation Runner"""
2
  import os
3
- import inspect
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
  from langchain_core.messages import HumanMessage
8
  from agent import build_graph
9
 
10
- from dotenv import load_dotenv
11
- import os
12
-
13
- load_dotenv()
14
-
15
- SUPABASE_URL = os.getenv("SUPABASE_URL")
16
- SUPABASE_KEY = os.getenv("SUPABASE_KEY")
17
-
18
- if not SUPABASE_KEY:
19
- raise ValueError("SUPABASE_KEY is missing!")
20
-
21
- # Now, import the agent module AFTER loading the env variables
22
- import agent
23
-
24
- # Pass the variables or client to agent functions/classes as needed
25
- agent.init_supabase_client(SUPABASE_URL, SUPABASE_KEY)
26
-
27
-
28
- # (Keep Constants as is)
29
- # --- Constants ---
30
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
31
 
32
- # --- Basic Agent Definition ---
33
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
34
-
35
-
36
  class BasicAgent:
37
  """A langgraph agent."""
38
  def __init__(self):
@@ -41,23 +16,18 @@ class BasicAgent:
41
 
42
  def __call__(self, question: str) -> str:
43
  print(f"Agent received question (first 50 chars): {question[:50]}...")
44
- # Wrap the question in a HumanMessage from langchain_core
45
  messages = [HumanMessage(content=question)]
46
  messages = self.graph.invoke({"messages": messages})
47
  answer = messages['messages'][-1].content
48
- return answer[14:]
 
49
 
50
 
51
- def run_and_submit_all( profile: gr.OAuthProfile | None):
52
- """
53
- Fetches all questions, runs the BasicAgent on them, submits all answers,
54
- and displays the results.
55
- """
56
- # --- Determine HF Space Runtime URL and Repo URL ---
57
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
58
 
59
  if profile:
60
- username= f"{profile.username}"
61
  print(f"User logged in: {username}")
62
  else:
63
  print("User not logged in.")
@@ -67,66 +37,44 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
67
  questions_url = f"{api_url}/questions"
68
  submit_url = f"{api_url}/submit"
69
 
70
- # 1. Instantiate Agent ( modify this part to create your agent)
71
  try:
72
  agent = BasicAgent()
73
  except Exception as e:
74
  print(f"Error instantiating agent: {e}")
75
  return f"Error initializing agent: {e}", None
76
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
77
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
78
- print(agent_code)
79
 
80
- # 2. Fetch Questions
81
- print(f"Fetching questions from: {questions_url}")
82
  try:
83
  response = requests.get(questions_url, timeout=15)
84
  response.raise_for_status()
85
  questions_data = response.json()
86
  if not questions_data:
87
- print("Fetched questions list is empty.")
88
- return "Fetched questions list is empty or invalid format.", None
89
- print(f"Fetched {len(questions_data)} questions.")
90
- except requests.exceptions.RequestException as e:
91
  print(f"Error fetching questions: {e}")
92
  return f"Error fetching questions: {e}", None
93
- except requests.exceptions.JSONDecodeError as e:
94
- print(f"Error decoding JSON response from questions endpoint: {e}")
95
- print(f"Response text: {response.text[:500]}")
96
- return f"Error decoding server response for questions: {e}", None
97
- except Exception as e:
98
- print(f"An unexpected error occurred fetching questions: {e}")
99
- return f"An unexpected error occurred fetching questions: {e}", None
100
 
101
- # 3. Run your Agent
102
  results_log = []
103
  answers_payload = []
104
- print(f"Running agent on {len(questions_data)} questions...")
105
  for item in questions_data:
106
  task_id = item.get("task_id")
107
  question_text = item.get("question")
108
  if not task_id or question_text is None:
109
- print(f"Skipping item with missing task_id or question: {item}")
110
  continue
111
  try:
112
  submitted_answer = agent(question_text)
113
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
114
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
115
  except Exception as e:
116
- print(f"Error running agent on task {task_id}: {e}")
117
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
118
 
119
  if not answers_payload:
120
- print("Agent did not produce any answers to submit.")
121
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
122
 
123
- # 4. Prepare Submission
124
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
125
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
126
- print(status_update)
127
-
128
- # 5. Submit
129
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
130
  try:
131
  response = requests.post(submit_url, json=submission_data, timeout=60)
132
  response.raise_for_status()
@@ -138,7 +86,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
138
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
139
  f"Message: {result_data.get('message', 'No message received.')}"
140
  )
141
- print("Submission successful.")
142
  results_df = pd.DataFrame(results_log)
143
  return final_status, results_df
144
  except requests.exceptions.HTTPError as e:
@@ -146,80 +93,65 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
146
  try:
147
  error_json = e.response.json()
148
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
149
- except requests.exceptions.JSONDecodeError:
150
  error_detail += f" Response: {e.response.text[:500]}"
151
  status_message = f"Submission Failed: {error_detail}"
152
- print(status_message)
153
  results_df = pd.DataFrame(results_log)
154
  return status_message, results_df
155
  except requests.exceptions.Timeout:
156
  status_message = "Submission Failed: The request timed out."
157
- print(status_message)
158
  results_df = pd.DataFrame(results_log)
159
  return status_message, results_df
160
  except requests.exceptions.RequestException as e:
161
  status_message = f"Submission Failed: Network error - {e}"
162
- print(status_message)
163
  results_df = pd.DataFrame(results_log)
164
  return status_message, results_df
165
  except Exception as e:
166
  status_message = f"An unexpected error occurred during submission: {e}"
167
- print(status_message)
168
  results_df = pd.DataFrame(results_log)
169
  return status_message, results_df
170
 
171
 
172
- # --- Build Gradio Interface using Blocks ---
173
  with gr.Blocks() as demo:
174
  gr.Markdown("# Basic Agent Evaluation Runner")
175
  gr.Markdown(
176
  """
177
  **Instructions:**
178
 
179
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
180
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
181
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
182
 
183
  ---
184
- **Disclaimers:**
185
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
186
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
187
  """
188
  )
189
 
190
  gr.LoginButton()
191
-
192
  run_button = gr.Button("Run Evaluation & Submit All Answers")
193
-
194
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
195
- # Removed max_rows=10 from DataFrame constructor
196
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
197
 
198
- run_button.click(
199
- fn=run_and_submit_all,
200
- outputs=[status_output, results_table]
201
- )
202
 
203
  if __name__ == "__main__":
204
  print("\n" + "-"*30 + " App Starting " + "-"*30)
205
- # Check for SPACE_HOST and SPACE_ID at startup for information
206
  space_host_startup = os.getenv("SPACE_HOST")
207
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
208
 
209
  if space_host_startup:
210
  print(f"✅ SPACE_HOST found: {space_host_startup}")
211
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
212
  else:
213
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
214
 
215
- if space_id_startup: # Print repo URLs if SPACE_ID is found
216
  print(f"✅ SPACE_ID found: {space_id_startup}")
217
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
218
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
219
  else:
220
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
221
-
222
- print("-"*(60 + len(" App Starting ")) + "\n")
223
 
 
224
  print("Launching Gradio Interface for Basic Agent Evaluation...")
225
  demo.launch(debug=True, share=False)
 
1
  """ Basic Agent Evaluation Runner"""
2
  import os
 
3
  import gradio as gr
4
  import requests
5
  import pandas as pd
6
  from langchain_core.messages import HumanMessage
7
  from agent import build_graph
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
 
 
 
11
  class BasicAgent:
12
  """A langgraph agent."""
13
  def __init__(self):
 
16
 
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
19
  messages = [HumanMessage(content=question)]
20
  messages = self.graph.invoke({"messages": messages})
21
  answer = messages['messages'][-1].content
22
+ print(f"Raw answer: {answer}")
23
+ return answer.strip()
24
 
25
 
26
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
27
+ space_id = os.getenv("SPACE_ID")
 
 
 
 
 
28
 
29
  if profile:
30
+ username = profile.username
31
  print(f"User logged in: {username}")
32
  else:
33
  print("User not logged in.")
 
37
  questions_url = f"{api_url}/questions"
38
  submit_url = f"{api_url}/submit"
39
 
 
40
  try:
41
  agent = BasicAgent()
42
  except Exception as e:
43
  print(f"Error instantiating agent: {e}")
44
  return f"Error initializing agent: {e}", None
 
 
 
45
 
46
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available."
47
+
48
  try:
49
  response = requests.get(questions_url, timeout=15)
50
  response.raise_for_status()
51
  questions_data = response.json()
52
  if not questions_data:
53
+ return "Fetched questions list is empty or invalid format.", None
54
+ except Exception as e:
 
 
55
  print(f"Error fetching questions: {e}")
56
  return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
57
 
 
58
  results_log = []
59
  answers_payload = []
 
60
  for item in questions_data:
61
  task_id = item.get("task_id")
62
  question_text = item.get("question")
63
  if not task_id or question_text is None:
 
64
  continue
65
  try:
66
  submitted_answer = agent(question_text)
67
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
68
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
69
  except Exception as e:
70
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
71
 
72
  if not answers_payload:
 
73
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
74
 
 
75
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
76
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
77
+
78
  try:
79
  response = requests.post(submit_url, json=submission_data, timeout=60)
80
  response.raise_for_status()
 
86
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
87
  f"Message: {result_data.get('message', 'No message received.')}"
88
  )
 
89
  results_df = pd.DataFrame(results_log)
90
  return final_status, results_df
91
  except requests.exceptions.HTTPError as e:
 
93
  try:
94
  error_json = e.response.json()
95
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
96
+ except Exception:
97
  error_detail += f" Response: {e.response.text[:500]}"
98
  status_message = f"Submission Failed: {error_detail}"
 
99
  results_df = pd.DataFrame(results_log)
100
  return status_message, results_df
101
  except requests.exceptions.Timeout:
102
  status_message = "Submission Failed: The request timed out."
 
103
  results_df = pd.DataFrame(results_log)
104
  return status_message, results_df
105
  except requests.exceptions.RequestException as e:
106
  status_message = f"Submission Failed: Network error - {e}"
 
107
  results_df = pd.DataFrame(results_log)
108
  return status_message, results_df
109
  except Exception as e:
110
  status_message = f"An unexpected error occurred during submission: {e}"
 
111
  results_df = pd.DataFrame(results_log)
112
  return status_message, results_df
113
 
114
 
 
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# Basic Agent Evaluation Runner")
117
  gr.Markdown(
118
  """
119
  **Instructions:**
120
 
121
+ 1. Clone this space and modify the code to define your agent.
122
+ 2. Log in with Hugging Face account using the button below.
123
+ 3. Click 'Run Evaluation & Submit All Answers' to evaluate your agent and submit answers.
124
 
125
  ---
126
+ **Note:**
127
+ This is a simple baseline. Consider improving with caching or async processing.
 
128
  """
129
  )
130
 
131
  gr.LoginButton()
 
132
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
133
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
134
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
135
 
136
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
137
 
138
  if __name__ == "__main__":
139
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
140
  space_host_startup = os.getenv("SPACE_HOST")
141
+ space_id_startup = os.getenv("SPACE_ID")
142
 
143
  if space_host_startup:
144
  print(f"✅ SPACE_HOST found: {space_host_startup}")
145
+ print(f" Runtime URL: https://{space_host_startup}.hf.space")
146
  else:
147
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
148
 
149
+ if space_id_startup:
150
  print(f"✅ SPACE_ID found: {space_id_startup}")
151
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
 
152
  else:
153
+ print("ℹ️ SPACE_ID environment variable not found (running locally?).")
 
 
154
 
155
+ print("-" * (60 + len(" App Starting ")) + "\n")
156
  print("Launching Gradio Interface for Basic Agent Evaluation...")
157
  demo.launch(debug=True, share=False)