yooke commited on
Commit
3cffeac
·
verified ·
1 Parent(s): f7a1d84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -41
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  # --- START OF FILE app.py ---
2
 
3
  import os
@@ -5,28 +7,26 @@ import gradio as gr
5
  import requests
6
  import inspect
7
  import pandas as pd
8
- from dotenv import load_dotenv # Added
9
- from agent import build_graph # Added
10
- from langchain_core.messages import HumanMessage # Added
11
 
12
- load_dotenv() # Added
13
 
14
- # (Keep Constants as is)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
18
- # --- Basic Agent Definition --- REMOVED THIS PART
19
 
20
- def run_and_submit_all( profile: gr.OAuthProfile | None):
21
  """
22
  Fetches all questions, runs the LangGraph Agent on them, submits all answers,
23
  and displays the results.
24
  """
25
  # --- Determine HF Space Runtime URL and Repo URL ---
26
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
27
 
28
  if profile:
29
- username= f"{profile.username}"
30
  print(f"User logged in: {username}")
31
  else:
32
  print("User not logged in.")
@@ -36,92 +36,102 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
36
  questions_url = f"{api_url}/questions"
37
  submit_url = f"{api_url}/submit"
38
 
39
- # 1. Instantiate Agent ( modify this part to create your agent)
40
  try:
41
  # Use the build_graph function from agent.py
42
- agent_graph = build_graph() # Changed from BasicAgent()
43
  print("LangGraph agent initialized.")
44
  except Exception as e:
45
  print(f"Error instantiating agent graph: {e}")
46
  return f"Error initializing agent graph: {e}", None
47
  # 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)
48
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
- print(agent_code)
50
 
51
  # 2. Fetch Questions
52
- # ... (rest of fetching code is the same) ...
53
  print(f"Fetching questions from: {questions_url}")
54
  try:
55
  response = requests.get(questions_url, timeout=15)
56
  response.raise_for_status()
57
  questions_data = response.json()
58
  if not questions_data:
59
- print("Fetched questions list is empty.")
60
- return "Fetched questions list is empty or invalid format.", None
61
  print(f"Fetched {len(questions_data)} questions.")
62
  except requests.exceptions.RequestException as e:
63
  print(f"Error fetching questions: {e}")
64
  return f"Error fetching questions: {e}", None
65
  except requests.exceptions.JSONDecodeError as e:
66
- print(f"Error decoding JSON response from questions endpoint: {e}")
67
- print(f"Response text: {response.text[:500]}")
68
- return f"Error decoding server response for questions: {e}", None
69
  except Exception as e:
70
  print(f"An unexpected error occurred fetching questions: {e}")
71
  return f"An unexpected error occurred fetching questions: {e}", None
72
 
73
-
74
  # 3. Run your Agent
75
  results_log = []
76
  answers_payload = []
77
  print(f"Running agent on {len(questions_data)} questions...")
78
- print(f"--- Starting processing Task ID: {task_id}, Question: {question_text[:100]}...")
 
 
79
  for item in questions_data:
80
  task_id = item.get("task_id")
81
- print(f"--- Finished agent invoke for Task ID: {task_id}")
82
-
83
  question_text = item.get("question")
 
84
  if not task_id or question_text is None:
85
  print(f"Skipping item with missing task_id or question: {item}")
86
  continue
 
 
 
 
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
- print(f"--- Extracted answer for Task ID: {task_id}: {submitted_answer[:100]}...")
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
  except Exception as e:
109
- print(f"Error running agent graph on task {task_id}: {e}")
110
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
111
 
112
  if not answers_payload:
113
  print("Agent did not produce any answers to submit.")
114
  # Even if no answers, show the log of errors
115
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
116
 
117
-
118
  # 4. Prepare Submission
119
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
120
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
121
  print(status_update)
122
 
123
  # 5. Submit
124
- # ... (rest of submission code is the same) ...
125
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
126
  try:
127
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -167,7 +177,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
167
 
168
  # --- Build Gradio Interface using Blocks ---
169
  with gr.Blocks() as demo:
170
- gr.Markdown("# LangGraph Agent Evaluation Runner") # Updated title
171
  gr.Markdown(
172
  """
173
  **Instructions:**
@@ -189,7 +199,6 @@ with gr.Blocks() as demo:
189
  run_button = gr.Button("Run Evaluation & Submit All Answers")
190
 
191
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
192
- # Removed max_rows=10 from DataFrame constructor
193
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
194
 
195
  run_button.click(
@@ -198,10 +207,10 @@ with gr.Blocks() as demo:
198
  )
199
 
200
  if __name__ == "__main__":
201
- print("\n" + "-"*30 + " App Starting " + "-"*30)
202
  # Check for SPACE_HOST and SPACE_ID at startup for information
203
  space_host_startup = os.getenv("SPACE_HOST")
204
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
205
 
206
  if space_host_startup:
207
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -209,15 +218,14 @@ if __name__ == "__main__":
209
  else:
210
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
211
 
212
- if space_id_startup: # Print repo URLs if SPACE_ID is found
213
  print(f"✅ SPACE_ID found: {space_id_startup}")
214
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
215
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
216
  else:
217
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
218
 
219
- print("-"*(60 + len(" App Starting ")) + "\n")
220
 
221
- print("Launching Gradio Interface for LangGraph Agent Evaluation...") # Updated message
222
- # demo.launch(debug=True, share=False)
223
- demo.launch(debug=True, share=False, auth=None) # Keep auth=None for public space or remove for gated
 
1
+ --- START OF FILE app.py ---
2
+
3
  # --- START OF FILE app.py ---
4
 
5
  import os
 
7
  import requests
8
  import inspect
9
  import pandas as pd
10
+ from dotenv import load_dotenv
11
+ from agent import build_graph
12
+ from langchain_core.messages import HumanMessage
13
 
14
+ load_dotenv()
15
 
 
16
  # --- Constants ---
17
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
18
 
 
19
 
20
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
21
  """
22
  Fetches all questions, runs the LangGraph Agent on them, submits all answers,
23
  and displays the results.
24
  """
25
  # --- Determine HF Space Runtime URL and Repo URL ---
26
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
27
 
28
  if profile:
29
+ username = f"{profile.username}"
30
  print(f"User logged in: {username}")
31
  else:
32
  print("User not logged in.")
 
36
  questions_url = f"{api_url}/questions"
37
  submit_url = f"{api_url}/submit"
38
 
39
+ # 1. Instantiate Agent
40
  try:
41
  # Use the build_graph function from agent.py
42
+ agent_graph = build_graph()
43
  print("LangGraph agent initialized.")
44
  except Exception as e:
45
  print(f"Error instantiating agent graph: {e}")
46
  return f"Error initializing agent graph: {e}", None
47
  # 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)
48
+ 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
49
+ print(f"Agent code link: {agent_code}")
50
 
51
  # 2. Fetch Questions
 
52
  print(f"Fetching questions from: {questions_url}")
53
  try:
54
  response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
56
  questions_data = response.json()
57
  if not questions_data:
58
+ print("Fetched questions list is empty.")
59
+ return "Fetched questions list is empty or invalid format.", None
60
  print(f"Fetched {len(questions_data)} questions.")
61
  except requests.exceptions.RequestException as e:
62
  print(f"Error fetching questions: {e}")
63
  return f"Error fetching questions: {e}", None
64
  except requests.exceptions.JSONDecodeError as e:
65
+ print(f"Error decoding JSON response from questions endpoint: {e}")
66
+ print(f"Response text: {response.text[:500]}")
67
+ return f"Error decoding server response for questions: {e}", None
68
  except Exception as e:
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
 
72
  # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []
75
  print(f"Running agent on {len(questions_data)} questions...")
76
+
77
+ # Removed the problematic print statement from here
78
+
79
  for item in questions_data:
80
  task_id = item.get("task_id")
 
 
81
  question_text = item.get("question")
82
+
83
  if not task_id or question_text is None:
84
  print(f"Skipping item with missing task_id or question: {item}")
85
  continue
86
+
87
+ # Moved the print statement inside the loop, after task_id and question_text are assigned
88
+ print(f"--- Starting processing Task ID: {task_id}, Question: {question_text[:100]}...")
89
+
90
  try:
91
  # Invoke the LangGraph agent
92
  result_state = agent_graph.invoke({"messages": [HumanMessage(content=question_text)]})
93
 
94
  # Extract the final answer from the last message
95
+ submitted_answer = "Error: Agent did not provide a response." # Default in case extraction fails
96
+
97
  if result_state and "messages" in result_state and result_state["messages"]:
98
  last_message = result_state["messages"][-1]
99
+ # The final content is typically in the content attribute of the last message
100
  if hasattr(last_message, 'content') and last_message.content:
101
  submitted_answer = last_message.content
102
  # else: Handle cases where the last message might be a tool message etc.,
103
  # for simplicity, we just use the default error message if content is missing.
104
 
105
+ # Ensure submitted_answer is a string
106
  if not isinstance(submitted_answer, str):
107
+ submitted_answer = str(submitted_answer)
108
 
109
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
110
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
111
+
112
+ # Moved this print statement inside the loop as well
113
+ print(f"--- Finished processing Task ID: {task_id}")
114
+ # Moved this print statement inside the loop as well
115
+ print(f"--- Extracted answer for Task ID: {task_id}: {submitted_answer[:100]}...")
116
+
117
+
118
  except Exception as e:
119
+ print(f"Error running agent graph on task {task_id}: {e}")
120
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
121
+ # Note: If an error occurs, the 'Finished' and 'Extracted answer' prints for this specific task won't happen,
122
+ # which is reasonable behavior.
123
 
124
  if not answers_payload:
125
  print("Agent did not produce any answers to submit.")
126
  # Even if no answers, show the log of errors
127
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
128
 
 
129
  # 4. Prepare Submission
130
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
131
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
132
  print(status_update)
133
 
134
  # 5. Submit
 
135
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
136
  try:
137
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
177
 
178
  # --- Build Gradio Interface using Blocks ---
179
  with gr.Blocks() as demo:
180
+ gr.Markdown("# LangGraph Agent Evaluation Runner") # Updated title
181
  gr.Markdown(
182
  """
183
  **Instructions:**
 
199
  run_button = gr.Button("Run Evaluation & Submit All Answers")
200
 
201
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
202
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
203
 
204
  run_button.click(
 
207
  )
208
 
209
  if __name__ == "__main__":
210
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
211
  # Check for SPACE_HOST and SPACE_ID at startup for information
212
  space_host_startup = os.getenv("SPACE_HOST")
213
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
214
 
215
  if space_host_startup:
216
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
218
  else:
219
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
220
 
221
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
222
  print(f"✅ SPACE_ID found: {space_id_startup}")
223
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
224
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
225
  else:
226
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
227
 
228
+ print("-" * (60 + len(" App Starting ")) + "\n")
229
 
230
+ print("Launching Gradio Interface for LangGraph Agent Evaluation...") # Updated message
231
+ demo.launch(debug=True, share=False, auth=None)