Skip to content

Commit 660bd45

Browse files
authored
fix(python): make option check uniform across backends (#6314)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent c27da0a commit 660bd45

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

backend/python/diffusers/backend.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,20 @@ def sc(self, clip_input, images): return images, [False for i in images]
6666
)
6767

6868
def is_float(s):
69+
"""Check if a string can be converted to float."""
6970
try:
7071
float(s)
7172
return True
7273
except ValueError:
7374
return False
75+
def is_int(s):
76+
"""Check if a string can be converted to int."""
77+
try:
78+
int(s)
79+
return True
80+
except ValueError:
81+
return False
82+
7483

7584
# The scheduler list mapping was taken from here: https://github.com/neggles/animatediff-cli/blob/6f336f5f4b5e38e85d7f06f1744ef42d0a45f2a7/src/animatediff/schedulers.py#L39
7685
# Credits to https://github.com/neggles
@@ -177,10 +186,11 @@ def LoadModel(self, request, context):
177186
key, value = opt.split(":")
178187
# if value is a number, convert it to the appropriate type
179188
if is_float(value):
180-
if float(value).is_integer():
181-
value = int(value)
182-
else:
183-
value = float(value)
189+
value = float(value)
190+
elif is_int(value):
191+
value = int(value)
192+
elif value.lower() in ["true", "false"]:
193+
value = value.lower() == "true"
184194
self.options[key] = value
185195

186196
# From options, extract if present "torch_dtype" and set it to the appropriate type

backend/python/mlx-audio/backend.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
import numpy as np
2121
import uuid
2222

23+
def is_float(s):
24+
"""Check if a string can be converted to float."""
25+
try:
26+
float(s)
27+
return True
28+
except ValueError:
29+
return False
30+
def is_int(s):
31+
"""Check if a string can be converted to int."""
32+
try:
33+
int(s)
34+
return True
35+
except ValueError:
36+
return False
37+
2338
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
2439

2540
# If MAX_WORKERS are specified in the environment use it, otherwise default to 1
@@ -32,14 +47,6 @@ class BackendServicer(backend_pb2_grpc.BackendServicer):
3247
This backend provides TTS (Text-to-Speech) functionality using MLX-Audio.
3348
"""
3449

35-
def _is_float(self, s):
36-
"""Check if a string can be converted to float."""
37-
try:
38-
float(s)
39-
return True
40-
except ValueError:
41-
return False
42-
4350
def Health(self, request, context):
4451
"""
4552
Returns a health check message.
@@ -80,11 +87,10 @@ async def LoadModel(self, request, context):
8087
key, value = opt.split(":", 1) # Split only on first colon to handle values with colons
8188

8289
# Convert numeric values to appropriate types
83-
if self._is_float(value):
84-
if float(value).is_integer():
85-
value = int(value)
86-
else:
87-
value = float(value)
90+
if is_float(value):
91+
value = float(value)
92+
elif is_int(value):
93+
value = int(value)
8894
elif value.lower() in ["true", "false"]:
8995
value = value.lower() == "true"
9096

backend/python/mlx-vlm/backend.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
from PIL import Image
2222
import tempfile
2323

24+
def is_float(s):
25+
"""Check if a string can be converted to float."""
26+
try:
27+
float(s)
28+
return True
29+
except ValueError:
30+
return False
31+
def is_int(s):
32+
"""Check if a string can be converted to int."""
33+
try:
34+
int(s)
35+
return True
36+
except ValueError:
37+
return False
38+
2439
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
2540

2641
# If MAX_WORKERS are specified in the environment use it, otherwise default to 1
@@ -32,14 +47,6 @@ class BackendServicer(backend_pb2_grpc.BackendServicer):
3247
A gRPC servicer that implements the Backend service defined in backend.proto.
3348
"""
3449

35-
def _is_float(self, s):
36-
"""Check if a string can be converted to float."""
37-
try:
38-
float(s)
39-
return True
40-
except ValueError:
41-
return False
42-
4350
def Health(self, request, context):
4451
"""
4552
Returns a health check message.
@@ -79,12 +86,10 @@ async def LoadModel(self, request, context):
7986
continue
8087
key, value = opt.split(":", 1) # Split only on first colon to handle values with colons
8188

82-
# Convert numeric values to appropriate types
83-
if self._is_float(value):
84-
if float(value).is_integer():
85-
value = int(value)
86-
else:
87-
value = float(value)
89+
if is_float(value):
90+
value = float(value)
91+
elif is_int(value):
92+
value = int(value)
8893
elif value.lower() in ["true", "false"]:
8994
value = value.lower() == "true"
9095

backend/python/mlx/backend.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@
2424
# If MAX_WORKERS are specified in the environment use it, otherwise default to 1
2525
MAX_WORKERS = int(os.environ.get('PYTHON_GRPC_MAX_WORKERS', '1'))
2626

27+
def is_float(s):
28+
"""Check if a string can be converted to float."""
29+
try:
30+
float(s)
31+
return True
32+
except ValueError:
33+
return False
34+
def is_int(s):
35+
"""Check if a string can be converted to int."""
36+
try:
37+
int(s)
38+
return True
39+
except ValueError:
40+
return False
41+
2742
# Implement the BackendServicer class with the service methods
2843
class BackendServicer(backend_pb2_grpc.BackendServicer):
2944
"""
3045
A gRPC servicer that implements the Backend service defined in backend.proto.
3146
"""
3247

33-
def _is_float(self, s):
34-
"""Check if a string can be converted to float."""
35-
try:
36-
float(s)
37-
return True
38-
except ValueError:
39-
return False
40-
4148
def Health(self, request, context):
4249
"""
4350
Returns a health check message.
@@ -78,11 +85,10 @@ async def LoadModel(self, request, context):
7885
key, value = opt.split(":", 1) # Split only on first colon to handle values with colons
7986

8087
# Convert numeric values to appropriate types
81-
if self._is_float(value):
82-
if float(value).is_integer():
83-
value = int(value)
84-
else:
85-
value = float(value)
88+
if is_float(value):
89+
value = float(value)
90+
elif is_int(value):
91+
value = int(value)
8692
elif value.lower() in ["true", "false"]:
8793
value = value.lower() == "true"
8894

0 commit comments

Comments
 (0)