Spaces:
Sleeping
Sleeping
Qscar KIM commited on
Commit ยท
3037214
1
Parent(s): f09272b
update codes
Browse files
app.py
CHANGED
|
@@ -4,28 +4,52 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import random
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
gemini_key = os.getenv("GEMINI_API_KEY") or os.getenv("GOOGLE_API_KEY")
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
self.model =
|
| 20 |
model_id="gemini/gemini-2.5-flash",
|
| 21 |
api_key=gemini_key
|
| 22 |
)
|
| 23 |
|
| 24 |
self.search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
self.alfred = CodeAgent(
|
| 28 |
-
tools=[self.search_tool],
|
| 29 |
model=self.model,
|
| 30 |
add_base_tools=True,
|
| 31 |
planning_interval=3
|
|
@@ -33,6 +57,8 @@ class BasicAgent:
|
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
try:
|
|
|
|
|
|
|
| 36 |
result = self.alfred.run(question)
|
| 37 |
if result is None:
|
| 38 |
return "unknown"
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
import random
|
| 7 |
+
import time
|
| 8 |
+
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool, VisitWebpageTool
|
| 9 |
|
| 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 = 5 # 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,
|
| 54 |
add_base_tools=True,
|
| 55 |
planning_interval=3
|
|
|
|
| 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"
|