Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,13 @@ import logging
|
|
4 |
import torch
|
5 |
from txagent import TxAgent
|
6 |
import gradio as gr
|
7 |
-
from huggingface_hub import
|
8 |
from tooluniverse import ToolUniverse
|
9 |
-
from tqdm import tqdm
|
10 |
import time
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Configuration
|
13 |
CONFIG = {
|
@@ -18,14 +21,32 @@ CONFIG = {
|
|
18 |
"tool_files": {
|
19 |
"new_tool": "./data/new_tool.json"
|
20 |
},
|
21 |
-
"download_timeout": 300, #
|
22 |
-
"max_retries": 3
|
|
|
23 |
}
|
24 |
|
25 |
# Logging setup
|
26 |
logging.basicConfig(level=logging.INFO)
|
27 |
logger = logging.getLogger(__name__)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def prepare_tool_files():
|
30 |
os.makedirs("./data", exist_ok=True)
|
31 |
if not os.path.exists(CONFIG["tool_files"]["new_tool"]):
|
@@ -38,6 +59,8 @@ def prepare_tool_files():
|
|
38 |
|
39 |
def download_with_retry(repo_id, local_dir):
|
40 |
retry_count = 0
|
|
|
|
|
41 |
while retry_count < CONFIG["max_retries"]:
|
42 |
try:
|
43 |
snapshot_download(
|
@@ -45,14 +68,15 @@ def download_with_retry(repo_id, local_dir):
|
|
45 |
local_dir=local_dir,
|
46 |
resume_download=True,
|
47 |
local_dir_use_symlinks=False,
|
48 |
-
|
|
|
49 |
)
|
50 |
return True
|
51 |
except Exception as e:
|
52 |
retry_count += 1
|
53 |
logger.error(f"Attempt {retry_count} failed for {repo_id}: {str(e)}")
|
54 |
if retry_count < CONFIG["max_retries"]:
|
55 |
-
wait_time =
|
56 |
logger.info(f"Waiting {wait_time} seconds before retry...")
|
57 |
time.sleep(wait_time)
|
58 |
return False
|
|
|
4 |
import torch
|
5 |
from txagent import TxAgent
|
6 |
import gradio as gr
|
7 |
+
from huggingface_hub import snapshot_download
|
8 |
from tooluniverse import ToolUniverse
|
|
|
9 |
import time
|
10 |
+
from functools import partial
|
11 |
+
from requests.adapters import HTTPAdapter
|
12 |
+
from requests import Session
|
13 |
+
from urllib3.util.retry import Retry
|
14 |
|
15 |
# Configuration
|
16 |
CONFIG = {
|
|
|
21 |
"tool_files": {
|
22 |
"new_tool": "./data/new_tool.json"
|
23 |
},
|
24 |
+
"download_timeout": 300, # 5 minutes timeout
|
25 |
+
"max_retries": 3,
|
26 |
+
"retry_delay": 10 # seconds between retries
|
27 |
}
|
28 |
|
29 |
# Logging setup
|
30 |
logging.basicConfig(level=logging.INFO)
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
33 |
+
def create_custom_session():
|
34 |
+
"""Create a session with custom timeout and retry settings"""
|
35 |
+
session = Session()
|
36 |
+
retries = Retry(
|
37 |
+
total=CONFIG["max_retries"],
|
38 |
+
backoff_factor=1,
|
39 |
+
status_forcelist=[500, 502, 503, 504]
|
40 |
+
)
|
41 |
+
adapter = HTTPAdapter(
|
42 |
+
max_retries=retries,
|
43 |
+
pool_connections=10,
|
44 |
+
pool_maxsize=10
|
45 |
+
)
|
46 |
+
session.mount("http://", adapter)
|
47 |
+
session.mount("https://", adapter)
|
48 |
+
return session
|
49 |
+
|
50 |
def prepare_tool_files():
|
51 |
os.makedirs("./data", exist_ok=True)
|
52 |
if not os.path.exists(CONFIG["tool_files"]["new_tool"]):
|
|
|
59 |
|
60 |
def download_with_retry(repo_id, local_dir):
|
61 |
retry_count = 0
|
62 |
+
custom_session = create_custom_session()
|
63 |
+
|
64 |
while retry_count < CONFIG["max_retries"]:
|
65 |
try:
|
66 |
snapshot_download(
|
|
|
68 |
local_dir=local_dir,
|
69 |
resume_download=True,
|
70 |
local_dir_use_symlinks=False,
|
71 |
+
use_auth_token=True,
|
72 |
+
session=custom_session
|
73 |
)
|
74 |
return True
|
75 |
except Exception as e:
|
76 |
retry_count += 1
|
77 |
logger.error(f"Attempt {retry_count} failed for {repo_id}: {str(e)}")
|
78 |
if retry_count < CONFIG["max_retries"]:
|
79 |
+
wait_time = CONFIG["retry_delay"] * retry_count
|
80 |
logger.info(f"Waiting {wait_time} seconds before retry...")
|
81 |
time.sleep(wait_time)
|
82 |
return False
|