Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
-
from smolagents import GradioUI, CodeAgent,
|
| 4 |
-
|
| 5 |
-
# Import our custom tools from their modules
|
| 6 |
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 7 |
from retriever import load_guest_dataset
|
| 8 |
import os
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
search_tool = DuckDuckGoSearchTool()
|
| 26 |
-
|
| 27 |
-
# Initialize the weather tool
|
| 28 |
weather_info_tool = WeatherInfoTool()
|
| 29 |
-
|
| 30 |
-
# Initialize the Hub stats tool
|
| 31 |
hub_stats_tool = HubStatsTool()
|
| 32 |
-
|
| 33 |
-
# Load the guest dataset and initialize the guest info tool
|
| 34 |
guest_info_tool = load_guest_dataset()
|
| 35 |
|
| 36 |
-
#
|
| 37 |
alfred = CodeAgent(
|
| 38 |
-
tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
|
| 39 |
model=model,
|
| 40 |
-
add_base_tools=True,
|
| 41 |
-
planning_interval=3
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
|
|
|
| 45 |
GradioUI(alfred).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
+
from smolagents import GradioUI, CodeAgent, LiteLLMModel
|
|
|
|
|
|
|
| 4 |
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 5 |
from retriever import load_guest_dataset
|
| 6 |
import os
|
| 7 |
+
import logging
|
| 8 |
+
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
| 9 |
+
import litellm
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
class RetryLiteLLMModel(LiteLLMModel):
|
| 15 |
+
@retry(
|
| 16 |
+
stop=stop_after_attempt(3), # Максимум 3 попытки
|
| 17 |
+
wait=wait_exponential(multiplier=1, min=1, max=10), # Задержка: 1с, 2с, 4с (макс. 10с)
|
| 18 |
+
retry=retry_if_exception_type(litellm.InternalServerError), # Повторять только для InternalServerError
|
| 19 |
+
before=lambda _: logger.info("Retrying due to 503 error..."),
|
| 20 |
+
after=lambda retry_state: logger.info(f"Retry attempt {retry_state.attempt_number} completed")
|
| 21 |
+
)
|
| 22 |
+
def generate(self, *args, **kwargs):
|
| 23 |
+
"""Переопределение метода generate с повторными попытками"""
|
| 24 |
+
return super().generate(*args, **kwargs)
|
| 25 |
+
|
| 26 |
+
# Model wrapper
|
| 27 |
+
try:
|
| 28 |
+
model = RetryLiteLLMModel(
|
| 29 |
+
model_id="gemini/gemini-2.0-flash-lite",
|
| 30 |
+
api_key=os.environ['GEMINI_API_TOKEN']
|
| 31 |
+
)
|
| 32 |
+
logger.info("Successfully initialized LiteLLM model")
|
| 33 |
+
except KeyError as e:
|
| 34 |
+
logger.error("GEMINI_API_TOKEN not set in environment variables")
|
| 35 |
+
raise
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logger.error(f"Failed to initialize model: {str(e)}")
|
| 38 |
+
raise
|
| 39 |
+
|
| 40 |
+
# Tools initialization
|
| 41 |
search_tool = DuckDuckGoSearchTool()
|
|
|
|
|
|
|
| 42 |
weather_info_tool = WeatherInfoTool()
|
|
|
|
|
|
|
| 43 |
hub_stats_tool = HubStatsTool()
|
|
|
|
|
|
|
| 44 |
guest_info_tool = load_guest_dataset()
|
| 45 |
|
| 46 |
+
# Создание агента
|
| 47 |
alfred = CodeAgent(
|
| 48 |
+
tools=[guest_info_tool, weather_info_tool, hub_stats_tool, search_tool],
|
| 49 |
model=model,
|
| 50 |
+
add_base_tools=True,
|
| 51 |
+
planning_interval=3
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
| 55 |
+
logger.info("Launching Gradio UI...")
|
| 56 |
GradioUI(alfred).launch()
|