Skip to content

Commit

Permalink
Merge pull request #23 from LlmKira/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sudoskys authored Feb 13, 2024
2 parents 103a462 + 5a7371e commit 3543c5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions playground/generate_image_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ async def main():
try:
gen = GenerateImageInfer.build(
prompt=f"1girl, spring, jacket, sfw, angel, flower,{enhance}",
action=Action.GENERATE,
action=Action.IMG2IMG,
image=encoded,
add_original_image=True,
strength=0.99,
strength=0.5,
width=1088,
height=896
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "novelai-python"
version = "0.3.0"
version = "0.3.1"
description = "Novelai Python Binding With Pydantic"
authors = [
{ name = "sudoskys", email = "[email protected]" },
Expand Down
13 changes: 8 additions & 5 deletions src/novelai_python/sdk/ai/generate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import math
import random
from copy import deepcopy
from enum import Enum, IntEnum
from io import BytesIO
from typing import Optional, Union
Expand Down Expand Up @@ -249,7 +250,7 @@ def model_post_init(self, *args) -> None:
def validate_model(self):
if self.action == Action.INFILL and not self.parameters.mask:
logger.warning("Mask maybe required for infill mode.")
if self.action == Action.INFILL:
if self.action != Action.GENERATE:
self.parameters.extra_noise_seed = self.parameters.seed
return self

Expand Down Expand Up @@ -394,6 +395,7 @@ async def necessary_headers(self, request_data) -> dict:
"Sec-Fetch-Site": "same-site",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0"
}

async def request(self,
Expand Down Expand Up @@ -421,20 +423,21 @@ async def request(self,
session.headers.clear()
session.headers.update(override_headers)
try:
_log_data = request_data.copy()
_log_data = deepcopy(request_data)
if self.action == Action.GENERATE:
logger.debug(f"Request Data: {_log_data}")
else:
_log_data.get("parameters", {}).update({
"image": "base64 data" if self.parameters.image else "None",
"image": "base64 data" if self.parameters.image else None,
}
)
_log_data.get("parameters", {}).update(
{
"mask": "base64 data" if self.parameters.mask else "None",
"mask": "base64 data" if self.parameters.mask else None,
}
)
logger.debug(f"Request Data: {request_data}")
logger.debug(f"Request Data: {_log_data}")
del _log_data
except Exception as e:
logger.warning(f"Error when print log data: {e}")
try:
Expand Down
30 changes: 29 additions & 1 deletion src/novelai_python/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .credential import JwtCredential, SecretStr
from .sdk.ai.generate_image import GenerateImageInfer
from .sdk.ai.upscale import Upscale
from .sdk.user.information import Information
from .sdk.user.login import Login
from .sdk.user.subscription import Subscription
Expand Down Expand Up @@ -90,6 +91,33 @@ async def subscription(
return JSONResponse(status_code=500, content=e.__dict__)


@app.post("/ai/upscale")
async def upscale(
req: Upscale,
current_token: str = Depends(get_current_token)
):
"""
生成图片
:param current_token: Authorization
:param req: Upscale
:return:
"""
try:
_result = await req.request(session=get_session(current_token), remove_sign=True)
zip_file_bytes = io.BytesIO()
with zipfile.ZipFile(zip_file_bytes, mode="w", compression=zipfile.ZIP_DEFLATED) as zip_file:
file = _result.files # ONLY TUPLE
zip_file.writestr(zinfo_or_arcname=file[0], data=file[1])
# return the zip file
zip_file_bytes.seek(0)
return StreamingResponse(zip_file_bytes, media_type='application/zip', headers={
'Content-Disposition': 'attachment;filename=image.zip'
})
except Exception as e:
logger.exception(e)
return JSONResponse(status_code=500, content=e.__dict__)


@app.post("/ai/generate_image")
async def generate_image(
req: GenerateImageInfer,
Expand All @@ -110,7 +138,7 @@ async def generate_image(
# return the zip file
zip_file_bytes.seek(0)
return StreamingResponse(zip_file_bytes, media_type='application/zip', headers={
'Content-Disposition': 'attachment;filename=multiple_files.zip'
'Content-Disposition': 'attachment;filename=image.zip'
})
except Exception as e:
logger.exception(e)
Expand Down

0 comments on commit 3543c5f

Please sign in to comment.