commit
a479e8c23c
@ -1 +1,3 @@
|
||||
__version__ = "1.0.3"
|
||||
from pkg_resources import parse_version
|
||||
|
||||
__version__ = parse_version("1.0.4")
|
||||
|
@ -1,6 +1,125 @@
|
||||
import textwrap
|
||||
|
||||
from aiohttp.client_exceptions import ClientResponseError
|
||||
|
||||
|
||||
class YouNeedToUseAContextManager(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
class ThisFunctionRequiresAPrompt(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
class StabilityAiError(Exception):
|
||||
retryable: bool = False
|
||||
|
||||
|
||||
class YouAreHoldingItWrong(StabilityAiError):
|
||||
pass
|
||||
|
||||
|
||||
class ApiKeyIsMissingOrInvalid(StabilityAiError):
|
||||
pass
|
||||
|
||||
|
||||
class ImAfraidICantLetYouDoThat(StabilityAiError):
|
||||
pass
|
||||
|
||||
|
||||
class YouBrokeTheirServeres(StabilityAiError):
|
||||
retryable: bool = True
|
||||
|
||||
|
||||
class ResourceNotFound(StabilityAiError):
|
||||
pass
|
||||
|
||||
|
||||
class RateLimitOrServerError(StabilityAiError):
|
||||
retryable: bool = True
|
||||
|
||||
|
||||
def figure_out_exception(e: ClientResponseError) -> StabilityAiError:
|
||||
if e.status == 400:
|
||||
return YouAreHoldingItWrong(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
Either you specificed some combination of options that their
|
||||
server doesn't like or it's a bug in this library. If you
|
||||
think it's the latter open up a bug report on
|
||||
|
||||
https://github.com/estheruary/stabilityai
|
||||
"""
|
||||
)
|
||||
)
|
||||
elif e.status == 401:
|
||||
return ApiKeyIsMissingOrInvalid(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
This could be one of a few problems. If you don't have an API
|
||||
key you should generate one at
|
||||
|
||||
https://beta.dreamstudio.ai/account
|
||||
|
||||
If you have an API key there are two ways to provide it to this
|
||||
library. The first and most common is via the environment. You
|
||||
to set STABILITY_API_KEY.
|
||||
|
||||
The second method is to provide it to the client constructor
|
||||
directly.
|
||||
|
||||
async with AsyncStabilityClient(api_key="your-key") as stability:
|
||||
...
|
||||
"""
|
||||
)
|
||||
)
|
||||
elif e.status == 403:
|
||||
return ImAfraidICantLetYouDoThat(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
At the time of writing (Jun 2023) Stability doesn't have any
|
||||
permissions on their API so if you get this it's likely
|
||||
a bug in this library or a problem on their servers. If you
|
||||
think it's the former report it at
|
||||
|
||||
https://github.com/estheruary/stabilityai
|
||||
"""
|
||||
)
|
||||
)
|
||||
elif e.status == 429:
|
||||
return RateLimitOrServerError(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
Stability's rate limit is 150 req / 10 sec. If you get this
|
||||
error on *every* request then it's a bug on their servers.
|
||||
You need to generate a new API key and delete the old one.
|
||||
"""
|
||||
)
|
||||
)
|
||||
elif e.status == 404:
|
||||
return ResourceNotFound(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
This usually happens when you specify models that don't exist
|
||||
*or* a combination of model/sampler that doesn't work together.
|
||||
"""
|
||||
)
|
||||
)
|
||||
elif e.status == 500:
|
||||
return YouBrokeTheirServeres(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
Impressive. This is not an error you can do anything about
|
||||
except retry. Stability might be having an outage or a
|
||||
problem on their servers.
|
||||
"""
|
||||
)
|
||||
)
|
||||
else:
|
||||
return StabilityAiError(
|
||||
textwrap.dedent(
|
||||
"""\
|
||||
Sorry, I don't know what's up with this error.
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
@ -1,6 +1,38 @@
|
||||
from typing import Dict, Mapping, TypeVar
|
||||
from numbers import Number
|
||||
from typing import Mapping, Sequence
|
||||
|
||||
T, U = TypeVar("T"), TypeVar("U")
|
||||
|
||||
def omit_none(m: Mapping[T, U]) -> Dict[T, U]:
|
||||
return {k: v for k, v in m.items() if v is not None}
|
||||
def serialize_for_form(data, path="", res={}):
|
||||
print(data, path, res)
|
||||
if isinstance(data, Mapping):
|
||||
for key, val in data.items():
|
||||
if val is not None:
|
||||
xxx = f"{path}[{key}]" if path else key
|
||||
serialize_for_form(val, xxx, res)
|
||||
elif isinstance(data, Sequence) and not isinstance(data, str):
|
||||
for i, elem in enumerate(data):
|
||||
serialize_for_form(elem, f"{path}[{i}]", res)
|
||||
elif isinstance(data, Number):
|
||||
res[path] = str(data)
|
||||
else:
|
||||
res[path] = data
|
||||
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
dat = {
|
||||
"text_prompts": [{"text": "Birthday party at a gothic church", "weight": None}],
|
||||
"init_image": None,
|
||||
"init_image_mode": None,
|
||||
"image_strength": 0.3,
|
||||
"step_schedule_start": None,
|
||||
"step_schedule_end": None,
|
||||
"cfg_scale": None,
|
||||
"clip_guidance_preset": None,
|
||||
"sampler": None,
|
||||
"sample_extras": None,
|
||||
}
|
||||
s = serialize_for_form(dat)
|
||||
|
||||
print(s)
|
||||
|
Loading…
Reference in New Issue