Shivangsinha commited on
Commit
46df6c3
·
verified ·
1 Parent(s): 89fcb54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -69
app.py CHANGED
@@ -25,16 +25,14 @@ def get_current_date_time() -> str:
25
  from datetime import datetime
26
  return datetime.now().isoformat()
27
 
28
-
29
  class BasicAgent:
30
  def __init__(self):
31
  print("BasicAgent initialized.")
32
  hf_token = os.getenv("HF_TOKEN")
33
- # Use InferenceClientModel without specifying provider
34
- # so HF router picks the best available provider automatically
35
  model = InferenceClientModel(
36
  model_id="meta-llama/Llama-3.3-70B-Instruct",
37
  token=hf_token,
 
38
  )
39
  tools = [
40
  DuckDuckGoSearchTool(),
@@ -50,27 +48,22 @@ class BasicAgent:
50
  )
51
 
52
  def __call__(self, question: str) -> str:
53
- print(f"Agent received question (first 50 chars): {question[:50]}...")
54
  try:
55
  answer = self.agent.run(question)
56
- final_answer = str(answer)
 
57
  except Exception as e:
58
  print(f"Agent error: {e}")
59
- final_answer = f"Error: {e}"
60
- print(f"Agent returning answer: {final_answer}")
61
- return final_answer
62
 
63
 
64
- def run_and_submit_all( profile: gr.OAuthProfile | None):
65
- """
66
- Fetches all questions, runs the BasicAgent on them, submits all answers,
67
- and displays the results.
68
- """
69
- # --- Determine HF Space Runtime URL and Repo URL ---
70
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
71
 
72
  if profile:
73
- username= f"{profile.username}"
74
  print(f"User logged in: {username}")
75
  else:
76
  print("User not logged in.")
@@ -80,17 +73,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
80
  questions_url = f"{api_url}/questions"
81
  submit_url = f"{api_url}/submit"
82
 
83
- # 1. Instantiate Agent ( modify this part to create your agent)
84
  try:
85
  agent = BasicAgent()
86
  except Exception as e:
87
  print(f"Error instantiating agent: {e}")
88
  return f"Error initializing agent: {e}", None
89
- # 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)
90
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
91
  print(agent_code)
92
 
93
- # 2. Fetch Questions
94
  print(f"Fetching questions from: {questions_url}")
95
  try:
96
  response = requests.get(questions_url, timeout=15)
@@ -100,18 +91,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
100
  print("Fetched questions list is empty.")
101
  return "Fetched questions list is empty or invalid format.", None
102
  print(f"Fetched {len(questions_data)} questions.")
103
- except requests.exceptions.RequestException as e:
104
  print(f"Error fetching questions: {e}")
105
  return f"Error fetching questions: {e}", None
106
- except requests.exceptions.JSONDecodeError as e:
107
- print(f"Error decoding JSON response from questions endpoint: {e}")
108
- print(f"Response text: {response.text[:500]}")
109
- return f"Error decoding server response for questions: {e}", None
110
- except Exception as e:
111
- print(f"An unexpected error occurred fetching questions: {e}")
112
- return f"An unexpected error occurred fetching questions: {e}", None
113
 
114
- # 3. Run your Agent
115
  results_log = []
116
  answers_payload = []
117
  print(f"Running agent on {len(questions_data)} questions...")
@@ -133,12 +116,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
133
  print("Agent did not produce any answers to submit.")
134
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
135
 
136
- # 4. Prepare Submission
137
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
138
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
139
  print(status_update)
140
 
141
- # 5. Submit
142
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
143
  try:
144
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -147,36 +128,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
147
  final_status = (
148
  f"Submission Successful!\n"
149
  f"User: {result_data.get('username')}\n"
150
- f"Overall Score: {result_data.get('score', 'N/A')}% "
151
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
152
  f"Message: {result_data.get('message', 'No message received.')}"
153
  )
154
  print("Submission successful.")
155
  results_df = pd.DataFrame(results_log)
156
  return final_status, results_df
157
- except requests.exceptions.HTTPError as e:
158
- error_detail = f"Server responded with status {e.response.status_code}."
159
- try:
160
- error_json = e.response.json()
161
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
162
- except requests.exceptions.JSONDecodeError:
163
- error_detail += f" Response: {e.response.text[:500]}"
164
- status_message = f"Submission Failed: {error_detail}"
165
- print(status_message)
166
- results_df = pd.DataFrame(results_log)
167
- return status_message, results_df
168
- except requests.exceptions.Timeout:
169
- status_message = "Submission Failed: The request timed out."
170
- print(status_message)
171
- results_df = pd.DataFrame(results_log)
172
- return status_message, results_df
173
- except requests.exceptions.RequestException as e:
174
- status_message = f"Submission Failed: Network error - {e}"
175
- print(status_message)
176
- results_df = pd.DataFrame(results_log)
177
- return status_message, results_df
178
  except Exception as e:
179
- status_message = f"An unexpected error occurred during submission: {e}"
180
  print(status_message)
181
  results_df = pd.DataFrame(results_log)
182
  return status_message, results_df
@@ -188,51 +148,40 @@ with gr.Blocks() as demo:
188
  gr.Markdown(
189
  """
190
  **Instructions:**
191
-
192
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
193
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
194
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
195
-
196
  ---
197
  **Disclaimers:**
198
  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).
199
  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.
200
  """
201
  )
202
-
203
  gr.LoginButton()
204
-
205
  run_button = gr.Button("Run Evaluation & Submit All Answers")
206
-
207
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
208
  # Removed max_rows=10 from DataFrame constructor
209
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
210
-
211
  run_button.click(
212
  fn=run_and_submit_all,
213
  outputs=[status_output, results_table]
214
  )
215
-
216
  if __name__ == "__main__":
217
  print("\n" + "-" * 30 + " App Starting " + "-" * 30)
218
  # Check for SPACE_HOST and SPACE_ID at startup for information
219
  space_host_startup = os.getenv("SPACE_HOST")
220
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
221
-
222
  if space_host_startup:
223
- print(f" SPACE_HOST found: {space_host_startup}")
224
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
225
  else:
226
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
227
-
228
  if space_id_startup: # Print repo URLs if SPACE_ID is found
229
- print(f" SPACE_ID found: {space_id_startup}")
230
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
231
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
232
  else:
233
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
234
-
235
  print("-" * (60 + len(" App Starting ")) + "\n")
236
-
237
  print("Launching Gradio Interface for Basic Agent Evaluation...")
238
- demo.launch(debug=True, share=False)
 
25
  from datetime import datetime
26
  return datetime.now().isoformat()
27
 
 
28
  class BasicAgent:
29
  def __init__(self):
30
  print("BasicAgent initialized.")
31
  hf_token = os.getenv("HF_TOKEN")
 
 
32
  model = InferenceClientModel(
33
  model_id="meta-llama/Llama-3.3-70B-Instruct",
34
  token=hf_token,
35
+ provider="cerebras",
36
  )
37
  tools = [
38
  DuckDuckGoSearchTool(),
 
48
  )
49
 
50
  def __call__(self, question: str) -> str:
51
+ print(f"Agent received question (first 50 chars): {question[:50]}")
52
  try:
53
  answer = self.agent.run(question)
54
+ print(f"Agent answer: {str(answer)[:100]}")
55
+ return str(answer)
56
  except Exception as e:
57
  print(f"Agent error: {e}")
58
+ return f"Error: {e}"
 
 
59
 
60
 
61
+ # --- run_and_submit_all function (Keep as is) ---
62
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
63
+ space_id = os.getenv("SPACE_ID")
 
 
 
 
64
 
65
  if profile:
66
+ username = f"{profile.username}"
67
  print(f"User logged in: {username}")
68
  else:
69
  print("User not logged in.")
 
73
  questions_url = f"{api_url}/questions"
74
  submit_url = f"{api_url}/submit"
75
 
 
76
  try:
77
  agent = BasicAgent()
78
  except Exception as e:
79
  print(f"Error instantiating agent: {e}")
80
  return f"Error initializing agent: {e}", None
81
+
82
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
83
  print(agent_code)
84
 
 
85
  print(f"Fetching questions from: {questions_url}")
86
  try:
87
  response = requests.get(questions_url, timeout=15)
 
91
  print("Fetched questions list is empty.")
92
  return "Fetched questions list is empty or invalid format.", None
93
  print(f"Fetched {len(questions_data)} questions.")
94
+ except Exception as e:
95
  print(f"Error fetching questions: {e}")
96
  return f"Error fetching questions: {e}", None
 
 
 
 
 
 
 
97
 
 
98
  results_log = []
99
  answers_payload = []
100
  print(f"Running agent on {len(questions_data)} questions...")
 
116
  print("Agent did not produce any answers to submit.")
117
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
118
 
 
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
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
124
  try:
125
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
128
  final_status = (
129
  f"Submission Successful!\n"
130
  f"User: {result_data.get('username')}\n"
131
+ f"Overall Score: {result_data.get('score', 'N/A')} "
132
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
133
  f"Message: {result_data.get('message', 'No message received.')}"
134
  )
135
  print("Submission successful.")
136
  results_df = pd.DataFrame(results_log)
137
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  except Exception as e:
139
+ status_message = f"Submission Failed: {e}"
140
  print(status_message)
141
  results_df = pd.DataFrame(results_log)
142
  return status_message, results_df
 
148
  gr.Markdown(
149
  """
150
  **Instructions:**
 
151
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
152
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
153
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
154
  ---
155
  **Disclaimers:**
156
  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).
157
  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.
158
  """
159
  )
 
160
  gr.LoginButton()
 
161
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
162
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
163
  # Removed max_rows=10 from DataFrame constructor
164
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
165
  run_button.click(
166
  fn=run_and_submit_all,
167
  outputs=[status_output, results_table]
168
  )
 
169
  if __name__ == "__main__":
170
  print("\n" + "-" * 30 + " App Starting " + "-" * 30)
171
  # Check for SPACE_HOST and SPACE_ID at startup for information
172
  space_host_startup = os.getenv("SPACE_HOST")
173
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
 
174
  if space_host_startup:
175
+ print(f"\u2705 SPACE_HOST found: {space_host_startup}")
176
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
177
  else:
178
+ print("\u2139\ufe0f SPACE_HOST environment variable not found (running locally?).")
 
179
  if space_id_startup: # Print repo URLs if SPACE_ID is found
180
+ print(f"\u2705 SPACE_ID found: {space_id_startup}")
181
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
182
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
183
  else:
184
+ print("\u2139\ufe0f SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
185
  print("-" * (60 + len(" App Starting ")) + "\n")
 
186
  print("Launching Gradio Interface for Basic Agent Evaluation...")
187
+ demo.launch(debug=True, share=False)