PradeepBodhi commited on
Commit
3d5eae7
·
verified ·
1 Parent(s): d66ff9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -55
app.py CHANGED
@@ -1,52 +1,42 @@
 
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from huggingface_hub import InferenceClient
 
6
 
 
 
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
- MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Free inference endpoint
10
 
11
- # --- Enhanced BasicAgent ---
 
 
 
12
  class BasicAgent:
13
  def __init__(self):
14
- self.client = InferenceClient(model=MODEL_NAME)
15
- self.system_prompt = """You are a GAIA question answering agent. Follow these rules:
16
- 1. Answer EXACTLY as required - no extra text
17
- 2. Use correct pluralization and ordering
18
- 3. Never explain your answer
19
- 4. Format lists as comma-separated values
20
- 5. Use only facts from verified sources"""
21
-
22
  def __call__(self, question: str) -> str:
23
- try:
24
- response = self.client.chat(
25
- model=MODEL_NAME,
26
- messages=[{
27
- "role": "system",
28
- "content": self.system_prompt
29
- },{
30
- "role": "user",
31
- "content": question
32
- }],
33
- max_tokens=100,
34
- stop_sequences=["\n"]
35
- )
36
-
37
- # Clean and format response
38
- answer = response.content.split("Answer:")[-1].strip()
39
- answer = answer.replace('"', '').replace('.', '').strip()
40
- return answer
41
-
42
- except Exception as e:
43
- print(f"Error in agent: {str(e)}")
44
- return "Error generating answer"
45
 
46
- # --- Keep Original Submission Logic Intact ---
47
- def run_and_submit_all(profile: gr.OAuthProfile | None):
48
  if profile:
49
- username = f"{profile.username}"
50
  print(f"User logged in: {username}")
51
  else:
52
  print("User not logged in.")
@@ -56,62 +46,132 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
56
  questions_url = f"{api_url}/questions"
57
  submit_url = f"{api_url}/submit"
58
 
 
59
  try:
60
  agent = BasicAgent()
61
  except Exception as e:
 
62
  return f"Error initializing agent: {e}", None
 
 
 
63
 
64
- space_id = os.getenv("SPACE_ID")
65
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
66
-
67
  try:
68
  response = requests.get(questions_url, timeout=15)
69
  response.raise_for_status()
70
  questions_data = response.json()
71
- except Exception as e:
 
 
 
 
 
72
  return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
73
 
74
- answers_payload = []
75
  results_log = []
 
 
76
  for item in questions_data:
77
  task_id = item.get("task_id")
78
  question_text = item.get("question")
79
  if not task_id or question_text is None:
 
80
  continue
81
  try:
82
  submitted_answer = agent(question_text)
83
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
84
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
85
  except Exception as e:
86
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"Error: {e}"})
 
 
 
 
 
87
 
88
- submission_data = {
89
- "username": username.strip(),
90
- "agent_code": agent_code,
91
- "answers": answers_payload
92
- }
93
 
 
 
94
  try:
95
  response = requests.post(submit_url, json=submission_data, timeout=60)
96
  response.raise_for_status()
97
  result_data = response.json()
98
  final_status = (
99
  f"Submission Successful!\n"
100
- f"Score: {result_data.get('score', 'N/A')}% "
101
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)"
 
 
102
  )
103
- return final_status, pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  except Exception as e:
105
- return f"Submission failed: {e}", pd.DataFrame(results_log)
 
 
 
 
106
 
107
- # --- Keep Original Gradio Interface ---
108
  with gr.Blocks() as demo:
109
  gr.Markdown("# Basic Agent Evaluation Runner")
110
- gr.Markdown("""... (original markdown content) ...""")
111
-
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  gr.LoginButton()
 
113
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
114
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
115
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
116
 
117
  run_button.click(
@@ -120,4 +180,25 @@ with gr.Blocks() as demo:
120
  )
121
 
122
  if __name__ == "__main__":
123
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
11
+
12
+ # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
15
 
16
+ # --- Basic Agent Definition ---
17
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
+
19
+
20
  class BasicAgent:
21
  def __init__(self):
22
+ self.graph = build_graph()
23
+
 
 
 
 
 
 
24
  def __call__(self, question: str) -> str:
25
+ messages = [HumanMessage(content=question)]
26
+ response = self.graph.invoke({"messages": messages})
27
+ return response['messages'][-1].content.strip()
28
+
29
+
30
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
31
+ """
32
+ Fetches all questions, runs the BasicAgent on them, submits all answers,
33
+ and displays the results.
34
+ """
35
+ # --- Determine HF Space Runtime URL and Repo URL ---
36
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
 
 
 
 
 
 
 
 
 
 
37
 
 
 
38
  if profile:
39
+ username= f"{profile.username}"
40
  print(f"User logged in: {username}")
41
  else:
42
  print("User not logged in.")
 
46
  questions_url = f"{api_url}/questions"
47
  submit_url = f"{api_url}/submit"
48
 
49
+ # 1. Instantiate Agent ( modify this part to create your agent)
50
  try:
51
  agent = BasicAgent()
52
  except Exception as e:
53
+ print(f"Error instantiating agent: {e}")
54
  return f"Error initializing agent: {e}", None
55
+ # 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)
56
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
57
+ print(agent_code)
58
 
59
+ # 2. Fetch Questions
60
+ print(f"Fetching questions from: {questions_url}")
 
61
  try:
62
  response = requests.get(questions_url, timeout=15)
63
  response.raise_for_status()
64
  questions_data = response.json()
65
+ if not questions_data:
66
+ print("Fetched questions list is empty.")
67
+ return "Fetched questions list is empty or invalid format.", None
68
+ print(f"Fetched {len(questions_data)} questions.")
69
+ except requests.exceptions.RequestException as e:
70
+ print(f"Error fetching questions: {e}")
71
  return f"Error fetching questions: {e}", None
72
+ except requests.exceptions.JSONDecodeError as e:
73
+ print(f"Error decoding JSON response from questions endpoint: {e}")
74
+ print(f"Response text: {response.text[:500]}")
75
+ return f"Error decoding server response for questions: {e}", None
76
+ except Exception as e:
77
+ print(f"An unexpected error occurred fetching questions: {e}")
78
+ return f"An unexpected error occurred fetching questions: {e}", None
79
 
80
+ # 3. Run your Agent
81
  results_log = []
82
+ answers_payload = []
83
+ print(f"Running agent on {len(questions_data)} questions...")
84
  for item in questions_data:
85
  task_id = item.get("task_id")
86
  question_text = item.get("question")
87
  if not task_id or question_text is None:
88
+ print(f"Skipping item with missing task_id or question: {item}")
89
  continue
90
  try:
91
  submitted_answer = agent(question_text)
92
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
93
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
94
  except Exception as e:
95
+ print(f"Error running agent on task {task_id}: {e}")
96
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
97
+
98
+ if not answers_payload:
99
+ print("Agent did not produce any answers to submit.")
100
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
101
 
102
+ # 4. Prepare Submission
103
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
104
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
105
+ print(status_update)
 
106
 
107
+ # 5. Submit
108
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
109
  try:
110
  response = requests.post(submit_url, json=submission_data, timeout=60)
111
  response.raise_for_status()
112
  result_data = response.json()
113
  final_status = (
114
  f"Submission Successful!\n"
115
+ f"User: {result_data.get('username')}\n"
116
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
117
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
118
+ f"Message: {result_data.get('message', 'No message received.')}"
119
  )
120
+ print("Submission successful.")
121
+ results_df = pd.DataFrame(results_log)
122
+ return final_status, results_df
123
+ except requests.exceptions.HTTPError as e:
124
+ error_detail = f"Server responded with status {e.response.status_code}."
125
+ try:
126
+ error_json = e.response.json()
127
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
128
+ except requests.exceptions.JSONDecodeError:
129
+ error_detail += f" Response: {e.response.text[:500]}"
130
+ status_message = f"Submission Failed: {error_detail}"
131
+ print(status_message)
132
+ results_df = pd.DataFrame(results_log)
133
+ return status_message, results_df
134
+ except requests.exceptions.Timeout:
135
+ status_message = "Submission Failed: The request timed out."
136
+ print(status_message)
137
+ results_df = pd.DataFrame(results_log)
138
+ return status_message, results_df
139
+ except requests.exceptions.RequestException as e:
140
+ status_message = f"Submission Failed: Network error - {e}"
141
+ print(status_message)
142
+ results_df = pd.DataFrame(results_log)
143
+ return status_message, results_df
144
  except Exception as e:
145
+ status_message = f"An unexpected error occurred during submission: {e}"
146
+ print(status_message)
147
+ results_df = pd.DataFrame(results_log)
148
+ return status_message, results_df
149
+
150
 
151
+ # --- Build Gradio Interface using Blocks ---
152
  with gr.Blocks() as demo:
153
  gr.Markdown("# Basic Agent Evaluation Runner")
154
+ gr.Markdown(
155
+ """
156
+ **Instructions:**
157
+
158
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
159
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
160
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
161
+
162
+ ---
163
+ **Disclaimers:**
164
+ 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).
165
+ 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.
166
+ """
167
+ )
168
+
169
  gr.LoginButton()
170
+
171
  run_button = gr.Button("Run Evaluation & Submit All Answers")
172
+
173
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
174
+ # Removed max_rows=10 from DataFrame constructor
175
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
176
 
177
  run_button.click(
 
180
  )
181
 
182
  if __name__ == "__main__":
183
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
184
+ # Check for SPACE_HOST and SPACE_ID at startup for information
185
+ space_host_startup = os.getenv("SPACE_HOST")
186
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
187
+
188
+ if space_host_startup:
189
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
190
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
191
+ else:
192
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
193
+
194
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
195
+ print(f"✅ SPACE_ID found: {space_id_startup}")
196
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
197
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
198
+ else:
199
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
200
+
201
+ print("-"*(60 + len(" App Starting ")) + "\n")
202
+
203
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
204
+ demo.launch(debug=True, share=False)