Qscar KIM commited on
Commit
bdc0645
ยท
1 Parent(s): 0544670

update codes

Browse files
Files changed (1) hide show
  1. app.py +10 -37
app.py CHANGED
@@ -10,44 +10,24 @@ from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpa
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
- # --- RPM Guard: ํ”„๋ฆฌ ํ‹ฐ์–ด ๋ฒ„์ŠคํŠธ ๋ฐฉ์–ดํ˜• ์ปค์Šคํ…€ ๋ชจ๋ธ ์–ด๋Œ‘ํ„ฐ ---
14
- class PacedGeminiModel(LiteLLMModel):
15
- def __init__(self, *args, **kwargs):
16
- super().__init__(*args, **kwargs)
17
- self.last_call_time = 0.0
18
- self.min_interval = 15 # 15 RPM ์ œํ•œ์„ ์•ˆ์ „ํ•˜๊ฒŒ ํšŒํ”ผํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ดˆ ๋‹จ์œ„ ๊ฐ„๊ฒฉ ๊ณ ์ •
19
-
20
- def __call__(self, *args, **kwargs):
21
- # ๋งˆ์ง€๋ง‰ API ํ˜ธ์ถœ ์‹œ์ ์œผ๋กœ๋ถ€ํ„ฐ ๊ฒฝ๊ณผํ•œ ์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ •๋ฐ€ ํŽ˜์ด์‹ฑ ๋ฝ(Lock) ๊ฐ€๋™
22
- now = time.time()
23
- elapsed = now - self.last_call_time
24
- if elapsed < self.min_interval:
25
- sleep_needed = self.min_interval - elapsed
26
- time.sleep(sleep_needed)
27
-
28
- try:
29
- response = super().__call__(*args, **kwargs)
30
- return response
31
- finally:
32
- # ์‹คํŒจํ•˜๋“  ์„ฑ๊ณตํ•˜๋“  ํƒ€์ž„์Šคํƒฌํ”„๋ฅผ ๊ฐฑ์‹ ํ•˜์—ฌ ์—ฐ์† ๋ฆฌํŠธ๋ผ์ด๋กœ ์ธํ•œ ๋ฒ„์ŠคํŠธ ํญ์ฆ ์ฐจ๋‹จ
33
- self.last_call_time = time.time()
34
-
35
  # --- Basic Agent Definition ---
36
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
37
  class BasicAgent:
38
  def __init__(self):
39
  gemini_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
40
 
41
- # ๊ธฐ๋ณธ LiteLLMModel ๋Œ€์‹  ์ •๋ฐ€ ๊ฐ์†๊ธฐ๊ฐ€ ๋‚ด์žฅ๋œ PacedGeminiModel๋กœ ๋ฐ”์ธ๋”ฉ
42
- self.model = PacedGeminiModel(
43
- model_id="gemini/gemini-2.5-flash",
44
- api_key=gemini_key
 
 
45
  )
46
 
47
  self.search_tool = DuckDuckGoSearchTool()
48
  self.visit_tool = VisitWebpageTool()
49
 
50
- # ๊ณ„ํš ์ฃผ๊ธฐ ๊ฐ€๋™ ๋ฐ ํˆด ๊ฐ€๋ฐฉ ๋ฐ”์ธ๋”ฉ
51
  self.alfred = CodeAgent(
52
  tools=[self.search_tool, self.visit_tool],
53
  model=self.model,
@@ -57,8 +37,6 @@ class BasicAgent:
57
 
58
  def __call__(self, question: str) -> str:
59
  try:
60
- # ์—ฐ์†๋œ ๋ฌธ์ œ ํ’€์ด ์‚ฌ์ด์—๋„ ํ”„๋ฆฌ ํ‹ฐ์–ด ๊ฒŒ์ดํŠธ์›จ์ด๊ฐ€ ํœด์‹ํ•  ์ˆ˜ ์žˆ๋„๋ก 5์ดˆ ์ „์ฒ˜๋ฆฌ ๋Œ€๊ธฐ
61
- time.sleep(5.0)
62
  result = self.alfred.run(question)
63
  if result is None:
64
  return "unknown"
@@ -72,8 +50,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
72
  Fetches all questions, runs the BasicAgent on them, submits all answers,
73
  and displays the results.
74
  """
75
- # --- Determine HF Space Runtime URL and Repo URL ---
76
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
77
  if profile:
78
  username= f"{profile.username}"
79
  print(f"User logged in: {username}")
@@ -85,17 +63,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
85
  questions_url = f"{api_url}/questions"
86
  submit_url = f"{api_url}/submit"
87
 
88
- # 1. Instantiate Agent ( modify this part to create your agent)
89
  try:
90
  agent = BasicAgent()
91
  except Exception as e:
92
  print(f"Error instantiating agent: {e}")
93
  return f"Error initializing agent: {e}", None
94
- # 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)
95
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
96
  print(agent_code)
97
 
98
- # 2. Fetch Questions
99
  print(f"Fetching questions from: {questions_url}")
100
  try:
101
  response = requests.get(questions_url, timeout=15)
@@ -116,7 +92,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
116
  print(f"An unexpected error occurred fetching questions: {e}")
117
  return f"An unexpected error occurred fetching questions: {e}", None
118
 
119
- # 3. Run your Agent
120
  results_log = []
121
  answers_payload = []
122
  print(f"Running agent on {len(questions_data)} questions...")
@@ -138,12 +113,10 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
138
  print("Agent did not produce any answers to submit.")
139
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
140
 
141
- # 4. Prepare Submission
142
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
143
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
144
  print(status_update)
145
 
146
- # 5. Submit
147
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
148
  try:
149
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
  class BasicAgent:
16
  def __init__(self):
17
  gemini_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
18
 
19
+ # ์ผ์ผ ์ฟผํ„ฐ๊ฐ€ 1,500ํšŒ๋กœ ๊ฐ€์žฅ ๋„‰๋„‰ํ•œ 2.0-flash ๋ฐฐ์ •
20
+ # requests_per_minute ์ธ์ž๋ฅผ ํ†ตํ•ด ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์•Œ์•„์„œ ์•ˆ์ „ ๊ฐ€๋“œ๋ ˆ์ผ์„ ์žก๋„๋ก ์„ค์ •
21
+ self.model = LiteLLMModel(
22
+ model_id="gemini/gemini-2.0-flash",
23
+ api_key=gemini_key,
24
+ requests_per_minute=6
25
  )
26
 
27
  self.search_tool = DuckDuckGoSearchTool()
28
  self.visit_tool = VisitWebpageTool()
29
 
30
+ # 45์  ๋ฒ ์ด์Šค๋ผ์ธ์˜ ๋‹ค๋‹จ๊ณ„ ๊ณ„ํš ์ˆ˜๋ฆฝ ๋ฐ ๋‚ด์žฅ ์ธํ„ฐํ”„๋ฆฌํ„ฐ ํŒŒ๋ผ๋ฏธํ„ฐ ์™„์ „ ๊ณ ์ •
31
  self.alfred = CodeAgent(
32
  tools=[self.search_tool, self.visit_tool],
33
  model=self.model,
 
37
 
38
  def __call__(self, question: str) -> str:
39
  try:
 
 
40
  result = self.alfred.run(question)
41
  if result is None:
42
  return "unknown"
 
50
  Fetches all questions, runs the BasicAgent on them, submits all answers,
51
  and displays the results.
52
  """
53
+ space_id = os.getenv("SPACE_ID")
54
+
55
  if profile:
56
  username= f"{profile.username}"
57
  print(f"User logged in: {username}")
 
63
  questions_url = f"{api_url}/questions"
64
  submit_url = f"{api_url}/submit"
65
 
 
66
  try:
67
  agent = BasicAgent()
68
  except Exception as e:
69
  print(f"Error instantiating agent: {e}")
70
  return f"Error initializing agent: {e}", None
71
+
72
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
73
  print(agent_code)
74
 
 
75
  print(f"Fetching questions from: {questions_url}")
76
  try:
77
  response = requests.get(questions_url, timeout=15)
 
92
  print(f"An unexpected error occurred fetching questions: {e}")
93
  return f"An unexpected error occurred fetching questions: {e}", None
94
 
 
95
  results_log = []
96
  answers_payload = []
97
  print(f"Running agent on {len(questions_data)} questions...")
 
113
  print("Agent did not produce any answers to submit.")
114
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
115
 
 
116
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
117
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
118
  print(status_update)
119
 
 
120
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
121
  try:
122
  response = requests.post(submit_url, json=submission_data, timeout=60)