inferencesh 0.1.10__py3-none-any.whl → 0.1.12__py3-none-any.whl

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/sdk.py CHANGED
@@ -6,10 +6,81 @@ import urllib.request
6
6
  import urllib.parse
7
7
  import tempfile
8
8
 
9
- class BaseAppInput(BaseModel):
9
+ from typing import Any, Dict, List
10
+ import inspect
11
+ import ast
12
+ import textwrap
13
+ from collections import OrderedDict
14
+
15
+
16
+ # inspired by https://github.com/pydantic/pydantic/issues/7580
17
+ class OrderedSchemaModel(BaseModel):
18
+ """A base model that ensures the JSON schema properties and required fields are in the order of field definition."""
19
+
20
+ @classmethod
21
+ def model_json_schema(cls, by_alias: bool = True, **kwargs: Any) -> Dict[str, Any]:
22
+ schema = super().model_json_schema(by_alias=by_alias, **kwargs)
23
+
24
+ field_order = cls._get_field_order()
25
+
26
+ if field_order:
27
+ # Order properties
28
+ ordered_properties = OrderedDict()
29
+ for field_name in field_order:
30
+ if field_name in schema['properties']:
31
+ ordered_properties[field_name] = schema['properties'][field_name]
32
+
33
+ # Add any remaining properties that weren't in field_order
34
+ for field_name, field_schema in schema['properties'].items():
35
+ if field_name not in ordered_properties:
36
+ ordered_properties[field_name] = field_schema
37
+
38
+ schema['properties'] = ordered_properties
39
+
40
+ # Order required fields
41
+ if 'required' in schema:
42
+ ordered_required = [field for field in field_order if field in schema['required']]
43
+ # Add any remaining required fields that weren't in field_order
44
+ ordered_required.extend([field for field in schema['required'] if field not in ordered_required])
45
+ schema['required'] = ordered_required
46
+
47
+ return schema
48
+
49
+ @classmethod
50
+ def _get_field_order(cls) -> List[str]:
51
+ """Get the order of fields as they were defined in the class."""
52
+ source = inspect.getsource(cls)
53
+
54
+ # Unindent the entire source code
55
+ source = textwrap.dedent(source)
56
+
57
+ try:
58
+ module = ast.parse(source)
59
+ except IndentationError:
60
+ # If we still get an IndentationError, wrap the class in a dummy module
61
+ source = f"class DummyModule:\n{textwrap.indent(source, ' ')}"
62
+ module = ast.parse(source)
63
+ # Adjust to look at the first class def inside DummyModule
64
+ # noinspection PyUnresolvedReferences
65
+ class_def = module.body[0].body[0]
66
+ else:
67
+ # Find the class definition
68
+ class_def = next(
69
+ node for node in module.body if isinstance(node, ast.ClassDef) and node.name == cls.__name__
70
+ )
71
+
72
+ # Extract field names in the order they were defined
73
+ field_order = []
74
+ for node in class_def.body:
75
+ if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
76
+ field_order.append(node.target.id)
77
+
78
+ return field_order
79
+
80
+ class BaseAppInput(OrderedSchemaModel):
10
81
  pass
11
82
 
12
- class BaseAppOutput(BaseModel):
83
+ class BaseAppOutput(OrderedSchemaModel):
13
84
  pass
14
85
 
15
86
  class BaseApp(BaseModel):
@@ -48,30 +119,44 @@ class File(BaseModel):
48
119
  """Check if the path is a URL."""
49
120
  parsed = urllib.parse.urlparse(path)
50
121
  return parsed.scheme in ('http', 'https')
51
-
52
122
  def _download_url(self) -> None:
53
123
  """Download the URL to a temporary file and update the path."""
54
124
  original_url = self.path
55
- # Create a temporary file with a suffix based on the URL path
56
- suffix = os.path.splitext(urllib.parse.urlparse(original_url).path)[1]
57
- tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
58
- self._tmp_path = tmp_file.name
59
-
60
- # Set up request with user agent
61
- headers = {
62
- 'User-Agent': (
63
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
64
- 'AppleWebKit/537.36 (KHTML, like Gecko) '
65
- 'Chrome/91.0.4472.124 Safari/537.36'
66
- )
67
- }
68
- req = urllib.request.Request(original_url, headers=headers)
69
-
70
- # Download the file
71
- print(f"Downloading URL: {original_url} to {self._tmp_path}")
72
- with urllib.request.urlopen(req) as response, open(self._tmp_path, 'wb') as out_file:
73
- out_file.write(response.read())
74
- self.path = self._tmp_path
125
+ tmp_file = None
126
+ try:
127
+ # Create a temporary file with a suffix based on the URL path
128
+ suffix = os.path.splitext(urllib.parse.urlparse(original_url).path)[1]
129
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
130
+ self._tmp_path = tmp_file.name
131
+
132
+ # Set up request with user agent
133
+ headers = {
134
+ 'User-Agent': (
135
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
136
+ 'AppleWebKit/537.36 (KHTML, like Gecko) '
137
+ 'Chrome/91.0.4472.124 Safari/537.36'
138
+ )
139
+ }
140
+ req = urllib.request.Request(original_url, headers=headers)
141
+
142
+ # Download the file
143
+ print(f"Downloading URL: {original_url} to {self._tmp_path}")
144
+ try:
145
+ with urllib.request.urlopen(req) as response, open(self._tmp_path, 'wb') as out_file:
146
+ out_file.write(response.read())
147
+ self.path = self._tmp_path
148
+ except (urllib.error.URLError, urllib.error.HTTPError) as e:
149
+ raise RuntimeError(f"Failed to download URL {original_url}: {str(e)}")
150
+ except IOError as e:
151
+ raise RuntimeError(f"Failed to write downloaded file to {self._tmp_path}: {str(e)}")
152
+ except Exception as e:
153
+ # Clean up temp file if something went wrong
154
+ if tmp_file is not None and hasattr(self, '_tmp_path'):
155
+ try:
156
+ os.unlink(self._tmp_path)
157
+ except:
158
+ pass
159
+ raise RuntimeError(f"Error downloading URL {original_url}: {str(e)}")
75
160
 
76
161
  def __del__(self):
77
162
  """Cleanup temporary file if it exists."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: inferencesh
3
- Version: 0.1.10
3
+ Version: 0.1.12
4
4
  Summary: inference.sh Python SDK
5
5
  Author: Inference Shell Inc.
6
6
  Author-email: "Inference Shell Inc." <hello@inference.sh>
@@ -0,0 +1,8 @@
1
+ inferencesh/__init__.py,sha256=pR0MXSJe41LgJkjGK-jhZR7LjqCFdRZtNTV6qcjYSTI,123
2
+ inferencesh/sdk.py,sha256=OJsbJlaQFewO6ncikfgGMnxs7TBLhxw_icNoHsqeIqw,7717
3
+ inferencesh-0.1.12.dist-info/LICENSE,sha256=OsgqEWIh2el_QMj0y8O1A5Q5Dl-dxqqYbFE6fszuR4s,1086
4
+ inferencesh-0.1.12.dist-info/METADATA,sha256=J39n1MJJ6ZlQr5uokw_1rhryoeofKaiVIyvTxsEUaF8,2584
5
+ inferencesh-0.1.12.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
6
+ inferencesh-0.1.12.dist-info/entry_points.txt,sha256=6IC-fyozAqW3ljsMLGCXxJ0_ui2Jb-2fLHtoH1RTnEE,45
7
+ inferencesh-0.1.12.dist-info/top_level.txt,sha256=TSMHg3T1ThMl1HGAWmzBClwOYH1ump5neof9BfHIwaA,12
8
+ inferencesh-0.1.12.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- inferencesh/__init__.py,sha256=pR0MXSJe41LgJkjGK-jhZR7LjqCFdRZtNTV6qcjYSTI,123
2
- inferencesh/sdk.py,sha256=N1GubtBeFkvqnpDisU8DUm7376HEkug1AwgoTV4Irv0,4081
3
- inferencesh-0.1.10.dist-info/LICENSE,sha256=OsgqEWIh2el_QMj0y8O1A5Q5Dl-dxqqYbFE6fszuR4s,1086
4
- inferencesh-0.1.10.dist-info/METADATA,sha256=Xx4mNEwqLMfhZfdZenwPhA-XWMbgvvWEQ-WzMxyD5Tc,2584
5
- inferencesh-0.1.10.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
6
- inferencesh-0.1.10.dist-info/entry_points.txt,sha256=6IC-fyozAqW3ljsMLGCXxJ0_ui2Jb-2fLHtoH1RTnEE,45
7
- inferencesh-0.1.10.dist-info/top_level.txt,sha256=TSMHg3T1ThMl1HGAWmzBClwOYH1ump5neof9BfHIwaA,12
8
- inferencesh-0.1.10.dist-info/RECORD,,