yooke commited on
Commit
f9faf38
·
verified ·
1 Parent(s): d484553

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -84
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import os
3
  import gradio as gr
4
  import requests
@@ -13,7 +12,6 @@ load_dotenv()
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
-
17
  def run_and_submit_all(profile: gr.OAuthProfile | None):
18
  """
19
  Fetches all questions, runs the LangGraph Agent on them, submits all answers,
@@ -21,7 +19,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
21
  """
22
  # --- Determine HF Space Runtime URL and Repo URL ---
23
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
24
-
25
  if profile:
26
  username = f"{profile.username}"
27
  print(f"User logged in: {username}")
@@ -41,8 +38,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
41
  except Exception as e:
42
  print(f"Error instantiating agent graph: {e}")
43
  return f"Error initializing agent graph: {e}", None
44
- # 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)
45
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code link unavailable (SPACE_ID not set)" # Added a check for SPACE_ID
46
  print(f"Agent code link: {agent_code}")
47
 
48
  # 2. Fetch Questions
@@ -71,56 +68,33 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
71
  answers_payload = []
72
  print(f"Running agent on {len(questions_data)} questions...")
73
 
74
- # Removed the problematic print statement from here
75
-
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
-
80
  if not task_id or question_text is None:
81
  print(f"Skipping item with missing task_id or question: {item}")
82
  continue
83
 
84
- # Moved the print statement inside the loop, after task_id and question_text are assigned
85
  print(f"--- Starting processing Task ID: {task_id}, Question: {question_text[:100]}...")
86
-
87
  try:
88
- # Invoke the LangGraph agent
89
  result_state = agent_graph.invoke({"messages": [HumanMessage(content=question_text)]})
90
-
91
- # Extract the final answer from the last message
92
- submitted_answer = "Error: Agent did not provide a response." # Default in case extraction fails
93
-
94
  if result_state and "messages" in result_state and result_state["messages"]:
95
  last_message = result_state["messages"][-1]
96
- # The final content is typically in the content attribute of the last message
97
  if hasattr(last_message, 'content') and last_message.content:
98
  submitted_answer = last_message.content
99
- # else: Handle cases where the last message might be a tool message etc.,
100
- # for simplicity, we just use the default error message if content is missing.
101
-
102
- # Ensure submitted_answer is a string
103
  if not isinstance(submitted_answer, str):
104
  submitted_answer = str(submitted_answer)
105
-
106
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
107
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
108
-
109
- # Moved this print statement inside the loop as well
110
  print(f"--- Finished processing Task ID: {task_id}")
111
- # Moved this print statement inside the loop as well
112
  print(f"--- Extracted answer for Task ID: {task_id}: {submitted_answer[:100]}...")
113
-
114
-
115
  except Exception as e:
116
  print(f"Error running agent graph on task {task_id}: {e}")
117
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
118
- # Note: If an error occurs, the 'Finished' and 'Extracted answer' prints for this specific task won't happen,
119
- # which is reasonable behavior.
120
 
121
  if not answers_payload:
122
  print("Agent did not produce any answers to submit.")
123
- # Even if no answers, show the log of errors
124
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
125
 
126
  # 4. Prepare Submission
@@ -144,22 +118,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
144
  print("Submission successful.")
145
  results_df = pd.DataFrame(results_log)
146
  return final_status, results_df
147
- except requests.exceptions.HTTPError as e:
148
- error_detail = f"Server responded with status {e.response.status_code}."
149
- try:
150
- error_json = e.response.json()
151
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
152
- except requests.exceptions.JSONDecodeError:
153
- error_detail += f" Response: {e.response.text[:500]}"
154
- status_message = f"Submission Failed: {error_detail}"
155
- print(status_message)
156
- results_df = pd.DataFrame(results_log)
157
- return status_message, results_df
158
- except requests.exceptions.Timeout:
159
- status_message = "Submission Failed: The request timed out."
160
- print(status_message)
161
- results_df = pd.DataFrame(results_log)
162
- return status_message, results_df
163
  except requests.exceptions.RequestException as e:
164
  status_message = f"Submission Failed: Network error - {e}"
165
  print(status_message)
@@ -171,58 +129,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
171
  results_df = pd.DataFrame(results_log)
172
  return status_message, results_df
173
 
174
-
175
  # --- Build Gradio Interface using Blocks ---
176
  with gr.Blocks() as demo:
177
- gr.Markdown("# LangGraph Agent Evaluation Runner") # Updated title
178
  gr.Markdown(
179
  """
180
  **Instructions:**
181
 
182
- 1. Please clone this space, then modify the code in `agent.py` and `app.py` to define your agent's logic, the tools, the necessary packages, etc ...
183
- 2. **Make sure you have your `DEEPSEEK_API_KEY` set as a Space Secret.**
184
- 3. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
185
- 4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
186
 
187
- ---
188
- **Disclaimers:**
189
- 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).
190
- 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.
191
  """
192
  )
193
-
194
  gr.LoginButton()
195
-
196
  run_button = gr.Button("Run Evaluation & Submit All Answers")
197
-
198
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
199
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
200
-
201
- run_button.click(
202
- fn=run_and_submit_all,
203
- outputs=[status_output, results_table]
204
- )
205
 
206
  if __name__ == "__main__":
207
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
208
- # Check for SPACE_HOST and SPACE_ID at startup for information
209
- space_host_startup = os.getenv("SPACE_HOST")
210
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
211
-
212
- if space_host_startup:
213
- print(f"✅ SPACE_HOST found: {space_host_startup}")
214
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
215
- else:
216
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
217
-
218
- if space_id_startup: # Print repo URLs if SPACE_ID is found
219
- print(f"✅ SPACE_ID found: {space_id_startup}")
220
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
221
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
222
- else:
223
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
224
-
225
- print("-" * (60 + len(" App Starting ")) + "\n")
226
-
227
- print("Launching Gradio Interface for LangGraph Agent Evaluation...") # Updated message
228
  demo.launch(debug=True, share=False, auth=None)
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
 
15
  def run_and_submit_all(profile: gr.OAuthProfile | None):
16
  """
17
  Fetches all questions, runs the LangGraph Agent on them, submits all answers,
 
19
  """
20
  # --- Determine HF Space Runtime URL and Repo URL ---
21
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
 
22
  if profile:
23
  username = f"{profile.username}"
24
  print(f"User logged in: {username}")
 
38
  except Exception as e:
39
  print(f"Error instantiating agent graph: {e}")
40
  return f"Error initializing agent graph: {e}", None
41
+
42
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code link unavailable (SPACE_ID not set)"
43
  print(f"Agent code link: {agent_code}")
44
 
45
  # 2. Fetch Questions
 
68
  answers_payload = []
69
  print(f"Running agent on {len(questions_data)} questions...")
70
 
 
 
71
  for item in questions_data:
72
  task_id = item.get("task_id")
73
  question_text = item.get("question")
 
74
  if not task_id or question_text is None:
75
  print(f"Skipping item with missing task_id or question: {item}")
76
  continue
77
 
 
78
  print(f"--- Starting processing Task ID: {task_id}, Question: {question_text[:100]}...")
 
79
  try:
 
80
  result_state = agent_graph.invoke({"messages": [HumanMessage(content=question_text)]})
81
+ submitted_answer = "Error: Agent did not provide a response."
 
 
 
82
  if result_state and "messages" in result_state and result_state["messages"]:
83
  last_message = result_state["messages"][-1]
 
84
  if hasattr(last_message, 'content') and last_message.content:
85
  submitted_answer = last_message.content
 
 
 
 
86
  if not isinstance(submitted_answer, str):
87
  submitted_answer = str(submitted_answer)
 
88
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
89
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
90
  print(f"--- Finished processing Task ID: {task_id}")
 
91
  print(f"--- Extracted answer for Task ID: {task_id}: {submitted_answer[:100]}...")
 
 
92
  except Exception as e:
93
  print(f"Error running agent graph on task {task_id}: {e}")
94
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
95
 
96
  if not answers_payload:
97
  print("Agent did not produce any answers to submit.")
 
98
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
99
 
100
  # 4. Prepare Submission
 
118
  print("Submission successful.")
119
  results_df = pd.DataFrame(results_log)
120
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  except requests.exceptions.RequestException as e:
122
  status_message = f"Submission Failed: Network error - {e}"
123
  print(status_message)
 
129
  results_df = pd.DataFrame(results_log)
130
  return status_message, results_df
131
 
 
132
  # --- Build Gradio Interface using Blocks ---
133
  with gr.Blocks() as demo:
134
+ gr.Markdown("# LangGraph Agent Evaluation Runner")
135
  gr.Markdown(
136
  """
137
  **Instructions:**
138
 
139
+ 1. Clone this space, modify the code in `agent.py` and `app.py` to define your agent's logic.
140
+ 2. Ensure `DEEPSEEK_API_KEY` is set as a Space Secret.
141
+ 3. Log in to Hugging Face using the button below.
142
+ 4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, and submit answers.
143
 
144
+ **Disclaimer**: Submissions may take time to process.
 
 
 
145
  """
146
  )
 
147
  gr.LoginButton()
 
148
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
149
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
150
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
151
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
152
 
153
  if __name__ == "__main__":
154
+ print("Launching Gradio Interface for LangGraph Agent Evaluation...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  demo.launch(debug=True, share=False, auth=None)