Skip to content

Commit

Permalink
5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zenafey committed Mar 13, 2024
1 parent 463f7ea commit 51dcd09
Show file tree
Hide file tree
Showing 19 changed files with 1,187 additions and 1,271 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Description
prodiapy is an unofficial lightweight Python package for [Prodia Stable Diffusion API](https://docs.prodia.com/reference/getting-started)

# Pre-requirements
The minimal ver. of Python supported by this package is 3.8.10

# Installation
```commandline
pip install -U prodiapy
pip install prodiapy
```

# text2img example
Expand Down
21 changes: 10 additions & 11 deletions prodiapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from . import _exceptions
from . import resources
from .resources.logger import logger
from ._client import Prodia, AsyncProdia

__all__ = [
"_exceptions",
"resources",
"Prodia",
"AsyncProdia"
]
from . import _exceptions, resources, aio
from .resources.logger import logger
from ._client import Prodia

__all__ = [
"_exceptions",
"resources",
"Prodia",
"aio"
]
170 changes: 55 additions & 115 deletions prodiapy/_client.py
Original file line number Diff line number Diff line change
@@ -1,115 +1,55 @@
from prodiapy.resources.engine import SyncAPIClient, AsyncAPIClient
from prodiapy import resources
from prodiapy._exceptions import *
from typing_extensions import override

import os


class Prodia(SyncAPIClient):
sd: resources.StableDiffusion
sdxl: resources.StableDiffusionXL

api_key: str

def __init__(
self,
api_key: str | None = None,
base_url: str | None = None
) -> None:
"""Construct a new prodia client instance.
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `api_key` from `PRODIA_API_KEY`
"""
if api_key is None:
api_key = os.environ.get("PRODIA_API_KEY")
if api_key is None:
raise AuthenticationError(
"The api_key client option must be set either by passing api_key to the client or by setting the \
PRODIA_API_KEY environment variable"
)
self.api_key = api_key

if base_url is None:
base_url = f"https://api.prodia.com/v1"

super().__init__(
base_url=base_url,
headers=self.auth_headers
)

self.sd = resources.StableDiffusion(self)
self.sdxl = resources.StableDiffusionXL(self)

general = resources.General(self)
upscale = resources.Upscale(self)
faceswap = resources.FaceSwap(self)

self.faceswap = faceswap.faceswap
self.upscale = upscale.upscale
self.create = general.create
self.job = general.job
self.constant = general.constant
self.wait = general.wait


@property
@override
def auth_headers(self) -> dict[str, str]:
api_key = self.api_key
return {'X-Prodia-Key': api_key, 'Content-Type': 'application/json'}


class AsyncProdia(AsyncAPIClient):
sd: resources.AsyncStableDiffusion
sdxl: resources.AsyncStableDiffusionXL

api_key: str

def __init__(
self,
api_key: str | None = None,
base_url: str | None = None
) -> None:
"""Construct a new async prodia client instance.
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `api_key` from `PRODIA_API_KEY`
"""
if api_key is None:
api_key = os.environ.get("PRODIA_API_KEY")
if api_key is None:
raise AuthenticationError(
"The api_key client option must be set either by passing api_key to the client or by setting the \
PRODIA_API_KEY environment variable"
)
self.api_key = api_key

if base_url is None:
base_url = f"https://api.prodia.com/v1"

super().__init__(
base_url=base_url,
headers=self.auth_headers
)

self.sd = resources.AsyncStableDiffusion(self)
self.sdxl = resources.AsyncStableDiffusionXL(self)

general = resources.AsyncGeneral(self)
upscale = resources.AsyncUpscale(self)
faceswap = resources.AsyncFaceSwap(self)

self.faceswap = faceswap.faceswap
self.upscale = upscale.upscale
self.create = general.create
self.job = general.job
self.constant = general.constant
self.wait = general.wait

@property
@override
def auth_headers(self) -> dict[str, str]:
api_key = self.api_key
return {'X-Prodia-Key': api_key, 'Content-Type': 'application/json'}

from prodiapy.resources.engine import SyncAPIClient
from prodiapy import resources
from typing import Optional
from prodiapy._exceptions import *

import os


class Prodia(SyncAPIClient):
api_key: str
sd: resources.StableDiffusion
sdxl: resources.StableDiffusionXL
general: resources.General

def __init__(
self,
api_key: Optional[str] = os.getenv("PRODIA_API_KEY"),
base_url: str = os.getenv("PRODIA_API_BASE", "https://api.prodia.com/v1")
):
"""Construct a new prodia client instance.
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `api_key` from `PRODIA_API_KEY`
"""
if api_key is None:
raise AuthenticationError(
"The api_key client option must be set either by passing api_key to the client or by setting the \
PRODIA_API_KEY environment variable"
)
self.api_key = api_key

super().__init__(
base_url=base_url,
headers=self.auth_headers
)

self.sd = resources.StableDiffusion(self)
self.sdxl = resources.StableDiffusionXL(self)

general = resources.General(self)

self.faceswap = general.faceswap
self.upscale = general.upscale
self.create = general.create
self.job = general.job
self.constant = general.constants
self.wait = general.wait

@property
def auth_headers(self) -> dict:
api_key = self.api_key
return {'X-Prodia-Key': api_key, 'Content-Type': 'application/json'}


63 changes: 35 additions & 28 deletions prodiapy/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@


__all__ = [
"AuthenticationError",
"InvalidParameterError",
"UnknownError",
"FailedJobError"
]


class ProdiaError(Exception):
pass


class AuthenticationError(ProdiaError):
pass


class InvalidParameterError(ProdiaError):
pass


class UnknownError(ProdiaError):
pass


class FailedJobError(ProdiaError):
pass


__all__ = [
"AuthenticationError",
"InvalidParameterError",
"UnknownError",
"FailedJobError",
"exception_vocab"
]


class ProdiaError(Exception):
pass


class AuthenticationError(ProdiaError):
pass


class InvalidParameterError(ProdiaError):
pass


class UnknownError(ProdiaError):
pass


class FailedJobError(ProdiaError):
pass


exception_vocab = {
401 | 401: AuthenticationError,
400: InvalidParameterError
}
53 changes: 53 additions & 0 deletions prodiapy/aio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

from prodiapy.resources.engine import AsyncAPIClient
from prodiapy import resources
from typing import Optional
from prodiapy._exceptions import *

import os


class Prodia(AsyncAPIClient):
api_key: str
sd: resources.AsyncStableDiffusion
sdxl: resources.AsyncStableDiffusionXL
general: resources.AsyncGeneral

def __init__(
self,
api_key: Optional[str] = os.getenv("PRODIA_API_KEY"),
base_url: str = os.getenv("PRODIA_API_BASE", "https://api.prodia.com/v1")
) -> None:
"""Construct a new async prodia client instance.
This automatically infers the following arguments from their corresponding environment variables if they are not provided:
- `api_key` from `PRODIA_API_KEY`
"""
if api_key is None:
raise AuthenticationError(
"The api_key client option must be set either by passing api_key to the client or by setting the \
PRODIA_API_KEY environment variable"
)
self.api_key = api_key

super().__init__(
base_url=base_url,
headers=self.auth_headers
)

self.sd = resources.AsyncStableDiffusion(self)
self.sdxl = resources.AsyncStableDiffusionXL(self)

general = resources.AsyncGeneral(self)

self.faceswap = general.faceswap
self.upscale = general.upscale
self.create = general.create
self.job = general.job
self.constant = general.constants
self.wait = general.wait

@property
def auth_headers(self) -> dict:
api_key = self.api_key
return {'X-Prodia-Key': api_key, 'Content-Type': 'application/json'}
13 changes: 7 additions & 6 deletions prodiapy/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .logger import logger
from .stablediffusion import StableDiffusion, AsyncStableDiffusion
from .stablediffusionxl import StableDiffusionXL, AsyncStableDiffusionXL
from .upscale import Upscale, AsyncUpscale
from .faceswap import FaceSwap, AsyncFaceSwap
from .general import General, AsyncGeneral
from .logger import logger
from .stablediffusion import StableDiffusion, AsyncStableDiffusion
from .stablediffusionxl import StableDiffusionXL, AsyncStableDiffusionXL
from .upscale import Upscale, AsyncUpscale
from .faceswap import FaceSwap, AsyncFaceSwap
from .facerestore import FaceRestore, AsyncFaceRestore
from .general import General, AsyncGeneral
Loading

0 comments on commit 51dcd09

Please sign in to comment.