inferencesh 0.3.1__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of inferencesh might be problematic. Click here for more details.
- {inferencesh-0.3.1/src/inferencesh.egg-info → inferencesh-0.4.0}/PKG-INFO +1 -1
- {inferencesh-0.3.1 → inferencesh-0.4.0}/pyproject.toml +1 -1
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/models/file.py +49 -3
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/utils/download.py +15 -7
- {inferencesh-0.3.1 → inferencesh-0.4.0/src/inferencesh.egg-info}/PKG-INFO +1 -1
- {inferencesh-0.3.1 → inferencesh-0.4.0}/LICENSE +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/README.md +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/setup.cfg +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/setup.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/__init__.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/client.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/models/__init__.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/models/base.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/models/llm.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/utils/__init__.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh/utils/storage.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh.egg-info/SOURCES.txt +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh.egg-info/dependency_links.txt +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh.egg-info/entry_points.txt +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh.egg-info/requires.txt +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/src/inferencesh.egg-info/top_level.txt +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/tests/test_client.py +0 -0
- {inferencesh-0.3.1 → inferencesh-0.4.0}/tests/test_sdk.py +0 -0
|
@@ -5,11 +5,45 @@ import os
|
|
|
5
5
|
import urllib.request
|
|
6
6
|
import urllib.parse
|
|
7
7
|
import tempfile
|
|
8
|
+
import hashlib
|
|
9
|
+
from pathlib import Path
|
|
8
10
|
from tqdm import tqdm
|
|
9
11
|
|
|
10
12
|
|
|
11
13
|
class File(BaseModel):
|
|
12
14
|
"""A class representing a file in the inference.sh ecosystem."""
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def get_cache_dir(cls) -> Path:
|
|
18
|
+
"""Get the cache directory path based on environment variables or default location."""
|
|
19
|
+
if cache_dir := os.environ.get("FILE_CACHE_DIR"):
|
|
20
|
+
path = Path(cache_dir)
|
|
21
|
+
else:
|
|
22
|
+
path = Path.home() / ".cache" / "inferencesh" / "files"
|
|
23
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
return path
|
|
25
|
+
|
|
26
|
+
def _get_cache_path(self, url: str) -> Path:
|
|
27
|
+
"""Get the cache path for a URL using a hash-based directory structure."""
|
|
28
|
+
# Parse URL components
|
|
29
|
+
parsed_url = urllib.parse.urlparse(url)
|
|
30
|
+
|
|
31
|
+
# Create hash from URL path and query parameters for uniqueness
|
|
32
|
+
url_components = parsed_url.netloc + parsed_url.path
|
|
33
|
+
if parsed_url.query:
|
|
34
|
+
url_components += '?' + parsed_url.query
|
|
35
|
+
url_hash = hashlib.sha256(url_components.encode()).hexdigest()[:12]
|
|
36
|
+
|
|
37
|
+
# Get filename from URL or use default
|
|
38
|
+
filename = os.path.basename(parsed_url.path)
|
|
39
|
+
if not filename:
|
|
40
|
+
filename = 'download'
|
|
41
|
+
|
|
42
|
+
# Create hash directory in cache
|
|
43
|
+
cache_dir = self.get_cache_dir() / url_hash
|
|
44
|
+
cache_dir.mkdir(exist_ok=True)
|
|
45
|
+
|
|
46
|
+
return cache_dir / filename
|
|
13
47
|
uri: Optional[str] = Field(default=None) # Original location (URL or file path)
|
|
14
48
|
path: Optional[str] = None # Resolved local file path
|
|
15
49
|
content_type: Optional[str] = None # MIME type of the file
|
|
@@ -74,11 +108,20 @@ class File(BaseModel):
|
|
|
74
108
|
return parsed.scheme in ('http', 'https')
|
|
75
109
|
|
|
76
110
|
def _download_url(self) -> None:
|
|
77
|
-
"""Download the URL to
|
|
111
|
+
"""Download the URL to the cache directory and update the path."""
|
|
78
112
|
original_url = self.uri
|
|
113
|
+
cache_path = self._get_cache_path(original_url)
|
|
114
|
+
|
|
115
|
+
# If file exists in cache, use it
|
|
116
|
+
if cache_path.exists():
|
|
117
|
+
print(f"Using cached file: {cache_path}")
|
|
118
|
+
self.path = str(cache_path)
|
|
119
|
+
return
|
|
120
|
+
|
|
121
|
+
print(f"Downloading URL: {original_url} to {cache_path}")
|
|
79
122
|
tmp_file = None
|
|
80
123
|
try:
|
|
81
|
-
#
|
|
124
|
+
# Download to temporary file first to avoid partial downloads in cache
|
|
82
125
|
suffix = os.path.splitext(urllib.parse.urlparse(original_url).path)[1]
|
|
83
126
|
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
|
84
127
|
self._tmp_path = tmp_file.name
|
|
@@ -133,7 +176,10 @@ class File(BaseModel):
|
|
|
133
176
|
# If we read the whole body at once, exit loop
|
|
134
177
|
break
|
|
135
178
|
|
|
136
|
-
|
|
179
|
+
# Move the temporary file to the cache location
|
|
180
|
+
os.replace(self._tmp_path, cache_path)
|
|
181
|
+
self._tmp_path = None # Prevent deletion in __del__
|
|
182
|
+
self.path = str(cache_path)
|
|
137
183
|
except (urllib.error.URLError, urllib.error.HTTPError) as e:
|
|
138
184
|
raise RuntimeError(f"Failed to download URL {original_url}: {str(e)}")
|
|
139
185
|
except IOError as e:
|
|
@@ -24,16 +24,24 @@ def download(url: str, directory: Union[str, Path, StorageDir]) -> str:
|
|
|
24
24
|
dir_path = Path(directory)
|
|
25
25
|
dir_path.mkdir(exist_ok=True)
|
|
26
26
|
|
|
27
|
-
#
|
|
28
|
-
|
|
29
|
-
hash_dir = dir_path / url_hash
|
|
30
|
-
hash_dir.mkdir(exist_ok=True)
|
|
27
|
+
# Parse URL components
|
|
28
|
+
parsed_url = urllib.parse.urlparse(url)
|
|
31
29
|
|
|
32
|
-
#
|
|
33
|
-
|
|
30
|
+
# Create hash from URL path and query parameters for uniqueness
|
|
31
|
+
url_components = parsed_url.netloc + parsed_url.path
|
|
32
|
+
if parsed_url.query:
|
|
33
|
+
url_components += '?' + parsed_url.query
|
|
34
|
+
url_hash = hashlib.sha256(url_components.encode()).hexdigest()[:12]
|
|
35
|
+
|
|
36
|
+
# Keep original filename or use a default
|
|
37
|
+
filename = os.path.basename(parsed_url.path)
|
|
34
38
|
if not filename:
|
|
35
39
|
filename = 'download'
|
|
36
|
-
|
|
40
|
+
|
|
41
|
+
# Create hash directory and store file
|
|
42
|
+
hash_dir = dir_path / url_hash
|
|
43
|
+
hash_dir.mkdir(exist_ok=True)
|
|
44
|
+
|
|
37
45
|
output_path = hash_dir / filename
|
|
38
46
|
|
|
39
47
|
# If file exists in directory and it's not a temp directory, return it
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|