Aya1610 commited on
Commit
b643f2d
Β·
verified Β·
1 Parent(s): 02e4ef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -57
app.py CHANGED
@@ -1,23 +1,21 @@
1
  import os
2
- import inspect
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
 
 
10
 
11
- # (Keep Constants as is)
12
- # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
- # --- Basic Agent Definition ---
16
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
17
-
18
 
19
  class BasicAgent:
20
- """A langgraph agent."""
21
  def __init__(self):
22
  print("BasicAgent initialized.")
23
  self.graph = build_graph()
@@ -28,33 +26,23 @@ class BasicAgent:
28
  result = self.graph.invoke({"messages": messages})
29
  print(f"Graph result: {result}")
30
  if not isinstance(result, dict):
31
- return "Graph returned an unexpected result format."
32
 
33
- if 'messages' in result:
34
- answer = result['messages'][-1].content
35
- return answer
36
  else:
37
- return f"Graph returned: {result} (missing 'messages')"
38
- # answer = result['messages'][-1].content
39
- # return answer # kein [14:] mehr nΓΆtig!
40
- # result = self.graph.invoke({"messages": messages})
41
-
42
-
43
-
44
-
45
 
46
 
47
-
48
- def run_and_submit_all( profile: gr.OAuthProfile | None):
49
  """
50
  Fetches all questions, runs the BasicAgent on them, submits all answers,
51
  and displays the results.
52
  """
53
- # --- Determine HF Space Runtime URL and Repo URL ---
54
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
55
-
56
  if profile:
57
- username= f"{profile.username}"
58
  print(f"User logged in: {username}")
59
  else:
60
  print("User not logged in.")
@@ -64,13 +52,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
64
  questions_url = f"{api_url}/questions"
65
  submit_url = f"{api_url}/submit"
66
 
67
- # 1. Instantiate Agent ( modify this part to create your agent)
68
  try:
69
  agent = BasicAgent()
70
  except Exception as e:
71
  print(f"Error instantiating agent: {e}")
72
  return f"Error initializing agent: {e}", None
73
- # 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)
74
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
75
  print(agent_code)
76
 
@@ -81,21 +69,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
81
  response.raise_for_status()
82
  questions_data = response.json()
83
  if not questions_data:
84
- print("Fetched questions list is empty.")
85
- return "Fetched questions list is empty or invalid format.", None
86
  print(f"Fetched {len(questions_data)} questions.")
87
  except requests.exceptions.RequestException as e:
88
  print(f"Error fetching questions: {e}")
89
  return f"Error fetching questions: {e}", None
90
  except requests.exceptions.JSONDecodeError as e:
91
- print(f"Error decoding JSON response from questions endpoint: {e}")
92
- print(f"Response text: {response.text[:500]}")
93
- return f"Error decoding server response for questions: {e}", None
94
  except Exception as e:
95
  print(f"An unexpected error occurred fetching questions: {e}")
96
  return f"An unexpected error occurred fetching questions: {e}", None
97
 
98
- # 3. Run your Agent
99
  results_log = []
100
  answers_payload = []
101
  print(f"Running agent on {len(questions_data)} questions...")
@@ -108,18 +96,32 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
108
  try:
109
  submitted_answer = agent(question_text)
110
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
111
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
112
  except Exception as e:
113
- print(f"Error running agent on task {task_id}: {e}")
114
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
115
 
116
  if not answers_payload:
117
  print("Agent did not produce any answers to submit.")
118
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
119
 
120
- # 4. Prepare Submission
121
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
122
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
 
 
 
 
 
 
123
  print(status_update)
124
 
125
  # 5. Submit
@@ -132,7 +134,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
132
  f"Submission Successful!\n"
133
  f"User: {result_data.get('username')}\n"
134
  f"Overall Score: {result_data.get('score', 'N/A')}% "
135
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
 
136
  f"Message: {result_data.get('message', 'No message received.')}"
137
  )
138
  print("Submission successful.")
@@ -166,19 +169,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
166
  return status_message, results_df
167
 
168
 
169
- # --- Build Gradio Interface using Blocks ---
 
170
  with gr.Blocks() as demo:
171
  gr.Markdown("# Basic Agent Evaluation Runner")
172
  gr.Markdown(
173
  """
174
  **Instructions:**
175
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
176
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
177
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
178
- ---
179
- **Disclaimers:**
180
- 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).
181
- 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.
182
  """
183
  )
184
 
@@ -187,7 +187,6 @@ with gr.Blocks() as demo:
187
  run_button = gr.Button("Run Evaluation & Submit All Answers")
188
 
189
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
190
- # Removed max_rows=10 from DataFrame constructor
191
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
192
 
193
  run_button.click(
@@ -196,10 +195,9 @@ with gr.Blocks() as demo:
196
  )
197
 
198
  if __name__ == "__main__":
199
- print("\n" + "-"*30 + " App Starting " + "-"*30)
200
- # Check for SPACE_HOST and SPACE_ID at startup for information
201
  space_host_startup = os.getenv("SPACE_HOST")
202
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
203
 
204
  if space_host_startup:
205
  print(f"βœ… SPACE_HOST found: {space_host_startup}")
@@ -207,14 +205,13 @@ if __name__ == "__main__":
207
  else:
208
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
209
 
210
- if space_id_startup: # Print repo URLs if SPACE_ID is found
211
  print(f"βœ… SPACE_ID found: {space_id_startup}")
212
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
213
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
214
  else:
215
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
216
 
217
- print("-"*(60 + len(" App Starting ")) + "\n")
218
-
219
  print("Launching Gradio Interface for Basic Agent Evaluation...")
220
- demo.launch(debug=True, share=False)
 
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from dotenv import load_dotenv
6
+
7
  from langchain_core.messages import HumanMessage
 
8
 
9
+ from agent import build_graph # <-- our modified agent
10
 
11
+ load_dotenv()
12
 
13
+ # -------------------------- Constants --------------------------
 
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
 
 
 
16
 
17
  class BasicAgent:
18
+ """A langgraph agent that uses our `find_answer` graph."""
19
  def __init__(self):
20
  print("BasicAgent initialized.")
21
  self.graph = build_graph()
 
26
  result = self.graph.invoke({"messages": messages})
27
  print(f"Graph result: {result}")
28
  if not isinstance(result, dict):
29
+ return "Graph returned an unexpected result format."
30
 
31
+ if "messages" in result:
32
+ answer = result["messages"][-1].content
33
+ return answer
34
  else:
35
+ return f"Graph returned: {result} (missing 'messages')"
 
 
 
 
 
 
 
36
 
37
 
38
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
 
39
  """
40
  Fetches all questions, runs the BasicAgent on them, submits all answers,
41
  and displays the results.
42
  """
43
+ space_id = os.getenv("SPACE_ID")
 
 
44
  if profile:
45
+ username = f"{profile.username}"
46
  print(f"User logged in: {username}")
47
  else:
48
  print("User not logged in.")
 
52
  questions_url = f"{api_url}/questions"
53
  submit_url = f"{api_url}/submit"
54
 
55
+ # 1. Instantiate Agent
56
  try:
57
  agent = BasicAgent()
58
  except Exception as e:
59
  print(f"Error instantiating agent: {e}")
60
  return f"Error initializing agent: {e}", None
61
+
62
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
63
  print(agent_code)
64
 
 
69
  response.raise_for_status()
70
  questions_data = response.json()
71
  if not questions_data:
72
+ print("Fetched questions list is empty.")
73
+ return "Fetched questions list is empty or invalid format.", None
74
  print(f"Fetched {len(questions_data)} questions.")
75
  except requests.exceptions.RequestException as e:
76
  print(f"Error fetching questions: {e}")
77
  return f"Error fetching questions: {e}", None
78
  except requests.exceptions.JSONDecodeError as e:
79
+ print(f"Error decoding JSON response from questions endpoint: {e}")
80
+ print(f"Response text: {response.text[:500]}")
81
+ return f"Error decoding server response for questions: {e}", None
82
  except Exception as e:
83
  print(f"An unexpected error occurred fetching questions: {e}")
84
  return f"An unexpected error occurred fetching questions: {e}", None
85
 
86
+ # 3. Run your Agent on each question
87
  results_log = []
88
  answers_payload = []
89
  print(f"Running agent on {len(questions_data)} questions...")
 
96
  try:
97
  submitted_answer = agent(question_text)
98
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
99
+ results_log.append({
100
+ "Task ID": task_id,
101
+ "Question": question_text,
102
+ "Submitted Answer": submitted_answer
103
+ })
104
  except Exception as e:
105
+ print(f"Error running agent on task {task_id}: {e}")
106
+ results_log.append({
107
+ "Task ID": task_id,
108
+ "Question": question_text,
109
+ "Submitted Answer": f"AGENT ERROR: {e}"
110
+ })
111
 
112
  if not answers_payload:
113
  print("Agent did not produce any answers to submit.")
114
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
115
 
116
+ # 4. Prepare Submission
117
+ submission_data = {
118
+ "username": username.strip(),
119
+ "agent_code": agent_code,
120
+ "answers": answers_payload
121
+ }
122
+ status_update = (
123
+ f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
124
+ )
125
  print(status_update)
126
 
127
  # 5. Submit
 
134
  f"Submission Successful!\n"
135
  f"User: {result_data.get('username')}\n"
136
  f"Overall Score: {result_data.get('score', 'N/A')}% "
137
+ f"({result_data.get('correct_count', '?')}/"
138
+ f"{result_data.get('total_attempted', '?')} correct)\n"
139
  f"Message: {result_data.get('message', 'No message received.')}"
140
  )
141
  print("Submission successful.")
 
169
  return status_message, results_df
170
 
171
 
172
+ # ─── Build Gradio Interface ───────────────────────────────────────────────────
173
+
174
  with gr.Blocks() as demo:
175
  gr.Markdown("# Basic Agent Evaluation Runner")
176
  gr.Markdown(
177
  """
178
  **Instructions:**
179
+ 1. Clone this Space, then modify the code to define your agent logic, tools, etc.
180
+ 2. Log in to your Hugging Face account using the button below.
181
+ 3. Click 'Run Evaluation & Submit All Answers'.
 
 
 
 
182
  """
183
  )
184
 
 
187
  run_button = gr.Button("Run Evaluation & Submit All Answers")
188
 
189
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
190
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
191
 
192
  run_button.click(
 
195
  )
196
 
197
  if __name__ == "__main__":
198
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
 
199
  space_host_startup = os.getenv("SPACE_HOST")
200
+ space_id_startup = os.getenv("SPACE_ID")
201
 
202
  if space_host_startup:
203
  print(f"βœ… SPACE_HOST found: {space_host_startup}")
 
205
  else:
206
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
207
 
208
+ if space_id_startup:
209
  print(f"βœ… SPACE_ID found: {space_id_startup}")
210
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
211
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
212
  else:
213
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
214
 
215
+ print("-" * (60 + len(" App Starting ")) + "\n")
 
216
  print("Launching Gradio Interface for Basic Agent Evaluation...")
217
+ demo.launch(debug=True, share=False)