Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom api url, proxy and some constant tweaks #30

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions imaginepy/async_imagine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@


class DeviantArt(Enum):
ID = 23185
SECRET = "fae0145a0736611056a5196a122c0d36"
ID = da_ID
SECRET = da_SECRET


class AsyncImagine:

def __init__(self, restricted: bool = True):
def __init__(self, restricted: bool = True, api: str = og_ENDPOINT, proxies: dict = None):
self.restricted = restricted
self.api = "https://inferenceengine.vyro.ai"
self.cdn = "https://1966211409.rsc.cdn77.org/appStuff/imagine-fncisndcubnsduigfuds"
self.api = api
self.proxy = proxies
self.cdn = og_CDN
self.version = 1
self.client = httpx.AsyncClient()
self.client = httpx.AsyncClient(proxies=self.proxy)

async def close(self):
await self.client.aclose()
Expand Down Expand Up @@ -58,7 +59,7 @@ async def _request(self, **kwargs) -> Response:
data = multi.read()

try:
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(proxies=self.proxy) as client:
r = await client.request(
method=kwargs.get("method", "GET"),
url=kwargs.get("url"),
Expand All @@ -71,14 +72,14 @@ async def _request(self, **kwargs) -> Response:
raise Exception(f"Request failed: {e}")

signature = hashlib.md5(r.content).hexdigest()
if signature == "d8b21a024d6267f3014d874d8372f7c8":
if signature == BANNED_SIGNATURE:
raise BannedContent("Violation of community guidelines.")
return r

async def thumb(self, item: Union[Model, Style, Inspiration, Mode]) -> bytes:
href = item.value[2 if isinstance(
item, Model) or isinstance(item, Style) else 1]
async with httpx.AsyncClient() as client:
async with httpx.AsyncClient(proxies=self.proxy) as client:
response = await client.get(f"{self.cdn}/{href}")
response.raise_for_status()
return bytes2png(response.content)
Expand Down Expand Up @@ -234,8 +235,8 @@ async def sdimg(
"negative_prompt": negative,
"seed": seed,
"cfg": get_cfg(cfg),
"image": ("temp_646.912234613557.jpg", content, "image/jpg"),
"mask": ("temp_646.912234613557.jpg", mask, "image/jpg"),
"image": (sd_temp_name, content, "image/jpg"),
"mask": (sd_temp_name, mask, "image/jpg"),
"priority": int(priority)
}
)
Expand Down Expand Up @@ -273,7 +274,7 @@ async def controlnet(
"control": mode.value[0],
"style_id": style.value[0] if style else model.value[0],
"seed": seed,
"image": ("temp_314.1353898439128.jpg", content, "image/jpg")
"image": (remix_temp_name, content, "image/jpg")
}
)
if asbase64 == True:
Expand Down
14 changes: 14 additions & 0 deletions imaginepy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

# Banned
BANNED_WORDS = ['xxx']
BANNED_SIGNATURE = "d8b21a024d6267f3014d874d8372f7c8"

# The official API urls, here to avoid repeating it twice
og_ENDPOINT = "https://inferenceengine.vyro.ai"
og_CDN = "https://1966211409.rsc.cdn77.org/appStuff/imagine-fncisndcubnsduigfuds"

# DevianArt credentials
da_ID = 23185
da_SECRET = "fae0145a0736611056a5196a122c0d36"

# sdimg have some repetitive image names, so here is better to simplify it a little
sd_temp_name = "temp_646.912234613557.jpg"
# Same for remix / controlnet function
remix_temp_name = "temp_314.1353898439128.jpg"


# KEY = (parameter, images, thumbnail)
Expand Down
23 changes: 12 additions & 11 deletions imaginepy/sync_imagine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@


class DeviantArt(Enum):
ID = 23185
SECRET = "fae0145a0736611056a5196a122c0d36"
ID = da_ID
SECRET = da_SECRET


class Imagine:

def __init__(self, restricted: bool = True):
def __init__(self, restricted: bool = True, api: str = og_ENDPOINT, proxies: dict = None):
self.restricted = restricted
self.api = "https://inferenceengine.vyro.ai"
self.cdn = "https://1966211409.rsc.cdn77.org/appStuff/imagine-fncisndcubnsduigfuds"
self.api = api
self.proxy = proxies
self.cdn = og_CDN
self.version = 1

def _request(self, **kwargs) -> Response:
Expand Down Expand Up @@ -54,7 +55,7 @@ def _request(self, **kwargs) -> Response:
data = multi.read()

try:
with httpx.Client() as client:
with httpx.Client(proxies=self.proxy) as client:
r = client.request(
method=kwargs.get("method", "GET"),
url=kwargs.get("url"),
Expand All @@ -67,14 +68,14 @@ def _request(self, **kwargs) -> Response:
raise Exception(f"Request failed: {e}")

signature = hashlib.md5(r.content).hexdigest()
if signature == "d8b21a024d6267f3014d874d8372f7c8":
if signature == BANNED_SIGNATURE:
raise BannedContent("Violation of community guidelines.")
return r

def thumb(self, item: Union[Model, Style, Inspiration, Mode]) -> bytes:
href = item.value[2 if isinstance(
item, Model) or isinstance(item, Style) else 1]
with httpx.Client() as client:
with httpx.Client(proxies=self.proxy) as client:
response = client.get(f"{self.cdn}/{href}")
response.raise_for_status()
return bytes2png(response.content)
Expand Down Expand Up @@ -230,8 +231,8 @@ def sdimg(
"negative_prompt": negative,
"seed": seed,
"cfg": get_cfg(cfg),
"image": ("temp_646.912234613557.jpg", content, "image/jpg"),
"mask": ("temp_646.912234613557.jpg", mask, "image/jpg"),
"image": (sd_temp_name, content, "image/jpg"),
"mask": (sd_temp_name, mask, "image/jpg"),
"priority": int(priority)
}
)
Expand Down Expand Up @@ -269,7 +270,7 @@ def controlnet(
"control": mode.value[0],
"style_id": style.value[0] if style else model.value[0],
"seed": seed,
"image": ("temp_314.1353898439128.jpg", content, "image/jpg")
"image": (remix_temp_name, content, "image/jpg")
}
)
if asbase64 == True:
Expand Down