asaporta commited on
Commit
e0fb2b9
·
verified ·
1 Parent(s): d9c2706

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -3,19 +3,17 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
7
-
8
- # (Keep Constants and BasicAgent class as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
- # --- Basic Agent Definition ---
13
 
 
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
 
16
- #hf_token = os.environ.get("HF_API_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN")
17
- openai_key = os.environ.get("OPENAI_API_KEY")
18
- xai_key = os.environ.get("XAI_API_KEY")
19
  class BasicAgent:
20
  def __init__(self, openai_key):
21
  self.openai_key = openai_key
@@ -36,7 +34,7 @@ class BasicAgent:
36
  print(f"Agent returning fixed answer: {fixed_answer}")
37
  return fixed_answer
38
 
39
- def run_and_submit_all( profile: gr.OAuthProfile | None):
40
  """
41
  Fetches all questions, runs the BasicAgent on them, submits all answers,
42
  and displays the results.
@@ -57,7 +55,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
57
 
58
  # 1. Instantiate Agent ( modify this part to create your agent)
59
  try:
60
- agent = BasicAgent()
61
  except Exception as e:
62
  print(f"Error instantiating agent: {e}")
63
  return f"Error initializing agent: {e}", None
@@ -161,21 +159,31 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
161
  with gr.Blocks() as demo:
162
  gr.Markdown("# Basic Agent Evaluation Runner")
163
  gr.Markdown(
164
- "Please clone this space, then modify the code to define your agent's logic within the `BasicAgent` class. "
165
- "Log in to your Hugging Face account using the button below. This uses your HF username for submission. "
166
- "Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score."
 
 
 
 
 
 
 
 
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(
178
  fn=run_and_submit_all,
 
179
  outputs=[status_output, results_table]
180
  )
181
 
@@ -201,6 +209,4 @@ if __name__ == "__main__":
201
  print("-"*(60 + len(" App Starting ")) + "\n")
202
 
203
  print("Launching Gradio Interface for Basic Agent Evaluation...")
204
- demo.launch(debug=True, share=False)
205
-
206
-
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
7
+ # (Keep Constants as is)
 
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
11
 
12
+ # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
 
15
+ openai_key = os.getenv("SPACE_ID")
16
+
 
17
  class BasicAgent:
18
  def __init__(self, openai_key):
19
  self.openai_key = openai_key
 
34
  print(f"Agent returning fixed answer: {fixed_answer}")
35
  return fixed_answer
36
 
37
+ def run_and_submit_all(profile: gr.OAuthProfile | None, openai_key: str):
38
  """
39
  Fetches all questions, runs the BasicAgent on them, submits all answers,
40
  and displays the results.
 
55
 
56
  # 1. Instantiate Agent ( modify this part to create your agent)
57
  try:
58
+ agent = BasicAgent(openai_key)
59
  except Exception as e:
60
  print(f"Error instantiating agent: {e}")
61
  return f"Error initializing agent: {e}", None
 
159
  with gr.Blocks() as demo:
160
  gr.Markdown("# Basic Agent Evaluation Runner")
161
  gr.Markdown(
162
+ """
163
+ **Instructions:**
164
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
165
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
166
+ 3. Enter your OpenAI key below (if required by your agent).
167
+ 4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
168
+ ---
169
+ **Disclaimers:**
170
+ 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).
171
+ 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.
172
+ """
173
  )
174
 
175
  gr.LoginButton()
176
 
177
+ openai_key_box = gr.Textbox(label="OpenAI API Key", type="password", placeholder="sk-...", lines=1)
178
+
179
  run_button = gr.Button("Run Evaluation & Submit All Answers")
180
 
181
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
182
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
183
 
184
  run_button.click(
185
  fn=run_and_submit_all,
186
+ inputs=[openai_key_box],
187
  outputs=[status_output, results_table]
188
  )
189
 
 
209
  print("-"*(60 + len(" App Starting ")) + "\n")
210
 
211
  print("Launching Gradio Interface for Basic Agent Evaluation...")
212
+ demo.launch(debug=True, share=False)