Qscar KIM commited on
Commit
56ac7c7
·
1 Parent(s): 83d0b09

update codes

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -3,9 +3,13 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
6
  import random
7
- from smolagents import CodeAgent, InferenceClientModel, OpenAIModel, DuckDuckGoSearchTool
 
 
8
 
 
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
@@ -13,43 +17,32 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
- deepseek_key = os.getenv("DEEPSEEK_API_KEY")
17
- hf_token = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
 
 
 
18
 
19
- # 고성능 추론엔진인 DeepSeek API 우선 바인딩, 키 누락 시 무료 인퍼런스 서버리스로 폴백 조치
20
- if deepseek_key:
21
- model = OpenAIModel(
22
- model_id="deepseek-chat",
23
- api_base="https://api.deepseek.com",
24
- api_key=deepseek_key,
25
- )
26
- print("BasicAgent: Initialized with DeepSeek API Engine.")
27
- else:
28
- model = InferenceClientModel(
29
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
30
- token=hf_token
31
- )
32
- print("BasicAgent: DeepSeek Key not found. Falling back to Hugging Face Inference API.")
33
-
34
  search_tool = DuckDuckGoSearchTool()
35
-
36
- # 35점 베이스라인의 다단계 계획 수립(Planning Loop) 및 기본 인터프리터 툴 에코시스템 이식
37
  self.alfred = CodeAgent(
38
  tools=[search_tool],
39
  model=model,
40
- add_base_tools=True,
41
- planning_interval=3
42
  )
43
 
44
  def __call__(self, question: str) -> str:
45
- try:
46
- result = self.alfred.run(question)
47
- if result is None:
48
- return "unknown"
49
- return str(result).strip()
50
- except Exception as e:
51
- print(f"Error during agent runtime execution: {e}")
52
- return "unknown"
53
 
54
  def run_and_submit_all( profile: gr.OAuthProfile | None):
55
  """
@@ -171,6 +164,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
171
  results_df = pd.DataFrame(results_log)
172
  return status_message, results_df
173
 
 
174
  # --- Build Gradio Interface using Blocks ---
175
  with gr.Blocks() as demo:
176
  gr.Markdown("# Basic Agent Evaluation Runner")
@@ -180,6 +174,10 @@ with gr.Blocks() as demo:
180
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
181
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
182
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
 
 
 
183
  """
184
  )
185
 
@@ -188,6 +186,7 @@ with gr.Blocks() as demo:
188
  run_button = gr.Button("Run Evaluation & Submit All Answers")
189
 
190
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
191
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
192
 
193
  run_button.click(
@@ -197,8 +196,9 @@ with gr.Blocks() as demo:
197
 
198
  if __name__ == "__main__":
199
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
200
  space_host_startup = os.getenv("SPACE_HOST")
201
- space_id_startup = os.getenv("SPACE_ID")
202
 
203
  if space_host_startup:
204
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -206,7 +206,7 @@ if __name__ == "__main__":
206
  else:
207
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
208
 
209
- if space_id_startup:
210
  print(f"✅ SPACE_ID found: {space_id_startup}")
211
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
212
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+
7
  import random
8
+ from smolagents import CodeAgent, InferenceClientModel, TransformersModel, OpenAIModel
9
+ from smolagents import DuckDuckGoSearchTool
10
+
11
 
12
+ # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
 
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
  class BasicAgent:
19
  def __init__(self):
20
+ # Initialize the Hugging Face model
21
+ # hf_token = os.getenv("HF_TOKEN")
22
+
23
+ # model = InferenceClientModel(
24
+ # token=hf_token
25
+ # )
26
+
27
+ model = OpenAIModel(
28
+ model_id="deepseek-chat",
29
+ api_base="https://api.deepseek.com",
30
+ api_key=os.getenv("DEEPSEEK_API_KEY"),
31
+ )
32
 
33
+ # Initialize the web search tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  search_tool = DuckDuckGoSearchTool()
35
+
36
+ # Create Alfred with all the tools
37
  self.alfred = CodeAgent(
38
  tools=[search_tool],
39
  model=model,
40
+ add_base_tools=True, # Add any additional base tools
41
+ planning_interval=3 # Enable planning every 3 steps
42
  )
43
 
44
  def __call__(self, question: str) -> str:
45
+ return self.alfred.run(question)
 
 
 
 
 
 
 
46
 
47
  def run_and_submit_all( profile: gr.OAuthProfile | None):
48
  """
 
164
  results_df = pd.DataFrame(results_log)
165
  return status_message, results_df
166
 
167
+
168
  # --- Build Gradio Interface using Blocks ---
169
  with gr.Blocks() as demo:
170
  gr.Markdown("# Basic Agent Evaluation Runner")
 
174
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
175
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
176
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
177
+ ---
178
+ **Disclaimers:**
179
+ 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).
180
+ 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.
181
  """
182
  )
183
 
 
186
  run_button = gr.Button("Run Evaluation & Submit All Answers")
187
 
188
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
189
+ # Removed max_rows=10 from DataFrame constructor
190
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
191
 
192
  run_button.click(
 
196
 
197
  if __name__ == "__main__":
198
  print("\n" + "-"*30 + " App Starting " + "-"*30)
199
+ # Check for SPACE_HOST and SPACE_ID at startup for information
200
  space_host_startup = os.getenv("SPACE_HOST")
201
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
202
 
203
  if space_host_startup:
204
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
206
  else:
207
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
208
 
209
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
210
  print(f"✅ SPACE_ID found: {space_id_startup}")
211
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
212
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")