cgoncalves commited on
Commit
f3aa1ae
·
verified ·
1 Parent(s): 480a5f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -31
app.py CHANGED
@@ -119,7 +119,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str, google_
119
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
120
 
121
  if profile:
122
- username= f"{profile.username}"
123
  print(f"User logged in: {username}")
124
  else:
125
  print("User not logged in.")
@@ -131,11 +131,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str, google_
131
 
132
  # 1. Instantiate Agent ( modify this part to create your agent)
133
  try:
134
- agent_instance = build_supervisor_agent(openai_key, google_key)
135
- agent = Agent(agent_instance)
136
  except Exception as e:
137
  print(f"Error instantiating agent: {e}")
138
  return f"Error initializing agent: {e}", None
 
139
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
140
  print(agent_code)
141
 
@@ -167,17 +167,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str, google_
167
  for item in questions_data:
168
  task_id = item.get("task_id")
169
  question_text = item.get("question")
170
- file_name = item.get("file_name", "")
171
  if not task_id or question_text is None:
172
  print(f"Skipping item with missing task_id or question: {item}")
173
  continue
174
  try:
175
- submitted_answer = agent(question_text, file_name, task_id)
176
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
177
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
178
  except Exception as e:
179
- print(f"Error running agent on task {task_id}: {e}")
180
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
181
 
182
  if not answers_payload:
183
  print("Agent did not produce any answers to submit.")
@@ -238,16 +237,14 @@ with gr.Blocks() as demo:
238
  gr.Markdown(
239
  """
240
  **Instructions:**
241
-
242
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
243
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
244
- 3. Enter your OpenAI and Google API keys below (these are required for the agent to work).
245
  4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
246
-
247
  ---
248
  **Disclaimers:**
249
- 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).
250
- 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.
251
  """
252
  )
253
 
@@ -255,33 +252,15 @@ with gr.Blocks() as demo:
255
 
256
  openai_key_box = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
257
  google_key_box = gr.Textbox(label="Google API Key", type="password", placeholder="AIza...", lines=1)
258
- set_keys_btn = gr.Button("Set API Keys")
259
- status_api_keys = gr.Textbox(label="API Key Status", lines=1, interactive=False)
260
-
261
- def set_api_keys(openai_key, google_key):
262
- if openai_key:
263
- os.environ["OPENAI_API_KEY"] = openai_key
264
- if google_key:
265
- os.environ["GOOGLE_API_KEY"] = google_key
266
- if openai_key or google_key:
267
- return "API keys set for this session."
268
- return "No API keys provided."
269
-
270
- set_keys_btn.click(
271
- fn=set_api_keys,
272
- inputs=[openai_key_box, google_key_box],
273
- outputs=status_api_keys
274
- )
275
 
276
  run_button = gr.Button("Run Evaluation & Submit All Answers")
277
 
278
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
279
- # Removed max_rows=10 from DataFrame constructor
280
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
281
 
282
  run_button.click(
283
  fn=run_and_submit_all,
284
- inputs=[gr.OAuthProfile(), openai_key_box, google_key_box],
285
  outputs=[status_output, results_table]
286
  )
287
 
 
119
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
120
 
121
  if profile:
122
+ username = f"{profile.username}"
123
  print(f"User logged in: {username}")
124
  else:
125
  print("User not logged in.")
 
131
 
132
  # 1. Instantiate Agent ( modify this part to create your agent)
133
  try:
134
+ agent = Agent(build_supervisor_agent(openai_key, google_key))
 
135
  except Exception as e:
136
  print(f"Error instantiating agent: {e}")
137
  return f"Error initializing agent: {e}", None
138
+ # In the case of an app running as a hugging Face space, this link points toward your codebase ( useful for others so please keep it public)
139
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
140
  print(agent_code)
141
 
 
167
  for item in questions_data:
168
  task_id = item.get("task_id")
169
  question_text = item.get("question")
 
170
  if not task_id or question_text is None:
171
  print(f"Skipping item with missing task_id or question: {item}")
172
  continue
173
  try:
174
+ submitted_answer = agent(question_text)
175
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
176
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
177
  except Exception as e:
178
+ print(f"Error running agent on task {task_id}: {e}")
179
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
180
 
181
  if not answers_payload:
182
  print("Agent did not produce any answers to submit.")
 
237
  gr.Markdown(
238
  """
239
  **Instructions:**
 
240
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
241
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
242
+ 3. Enter your OpenAI and Google API keys below (if required by your agent).
243
  4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
244
  ---
245
  **Disclaimers:**
246
+ 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).
247
+ 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 separate action or even to answer the questions in async.
248
  """
249
  )
250
 
 
252
 
253
  openai_key_box = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
254
  google_key_box = gr.Textbox(label="Google API Key", type="password", placeholder="AIza...", lines=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  run_button = gr.Button("Run Evaluation & Submit All Answers")
257
 
258
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
259
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
260
 
261
  run_button.click(
262
  fn=run_and_submit_all,
263
+ inputs=[openai_key_box, google_key_box],
264
  outputs=[status_output, results_table]
265
  )
266