-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from LlmKira/dev
Update SDK
- Loading branch information
Showing
41 changed files
with
1,264 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
import asyncio | ||
import os | ||
import pathlib | ||
from dotenv import load_dotenv | ||
from pydantic import SecretStr | ||
|
||
from novelai_python import APIError, Login | ||
from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential | ||
from novelai_python.sdk.ai.generate_image import Action, Sampler | ||
from novelai_python.utils.useful import enum_to_list | ||
|
||
|
||
async def generate( | ||
prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres", | ||
image_path="static_refer.png" | ||
): | ||
jwt = os.getenv("NOVELAI_JWT", None) | ||
if jwt is None: | ||
raise ValueError("NOVELAI_JWT is not set in `.env` file, please create one and set it") | ||
credential = JwtCredential(jwt_token=SecretStr(jwt)) | ||
"""Or you can use the login credential to get the jwt token""" | ||
_login_credential = Login.build( | ||
user_name=os.getenv("NOVELAI_USER"), | ||
password=os.getenv("NOVELAI_PASS") | ||
) | ||
# await _login_credential.request() | ||
print(f"Action List:{enum_to_list(Action)}") | ||
try: | ||
if not os.path.exists(image_path): | ||
raise ValueError(f"Image not found: {image_path}") | ||
with open(image_path, "rb") as f: | ||
image = f.read() | ||
agent = GenerateImageInfer.build( | ||
prompt=prompt, | ||
action=Action.GENERATE, | ||
sampler=Sampler.K_DPMPP_SDE, | ||
reference_image=image, | ||
reference_strength=0.6, | ||
reference_information_extracted=1, | ||
add_original_image=True, # This Not affect the vibe generation | ||
qualityToggle=True, | ||
) | ||
print(f"charge: {agent.calculate_cost(is_opus=True)} if you are vip3") | ||
print(f"charge: {agent.calculate_cost(is_opus=False)} if you are not vip3") | ||
result = await agent.request( | ||
session=credential, remove_sign=True | ||
) | ||
except APIError as e: | ||
print(f"Error: {e.message}") | ||
return None | ||
else: | ||
print(f"Meta: {result.meta}") | ||
_res: ImageGenerateResp | ||
file = result.files[0] | ||
with open(f"{pathlib.Path(__file__).stem}.png", "wb") as f: | ||
f.write(file[1]) | ||
|
||
|
||
load_dotenv() | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(generate()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# -*- coding: utf-8 -*- | ||
import asyncio | ||
import os | ||
import pathlib | ||
from dotenv import load_dotenv | ||
from pydantic import SecretStr | ||
|
||
from novelai_python import APIError, Login | ||
from novelai_python import GenerateImageInfer, ImageGenerateResp, JwtCredential | ||
from novelai_python.sdk.ai.generate_image import Action, Sampler | ||
from novelai_python.utils.useful import enum_to_list | ||
|
||
|
||
async def generate( | ||
prompt="1girl, year 2023, dynamic angle, best quality, amazing quality, very aesthetic, absurdres", | ||
image_path="static_image.png", | ||
reference_image_path="static_refer.png" | ||
): | ||
jwt = os.getenv("NOVELAI_JWT", None) | ||
if jwt is None: | ||
raise ValueError("NOVELAI_JWT is not set in `.env` file, please create one and set it") | ||
credential = JwtCredential(jwt_token=SecretStr(jwt)) | ||
"""Or you can use the login credential to get the jwt token""" | ||
_login_credential = Login.build( | ||
user_name=os.getenv("NOVELAI_USER"), | ||
password=os.getenv("NOVELAI_PASS") | ||
) | ||
# await _login_credential.request() | ||
print(f"Action List:{enum_to_list(Action)}") | ||
try: | ||
if not os.path.exists(image_path): | ||
raise ValueError(f"Image not found: {image_path}") | ||
if not os.path.exists(reference_image_path): | ||
raise ValueError(f"Image not found: {reference_image_path}") | ||
with open(image_path, "rb") as f: | ||
image = f.read() | ||
with open(reference_image_path, "rb") as f: | ||
reference_image = f.read() | ||
agent = GenerateImageInfer.build( | ||
prompt=prompt, | ||
action=Action.IMG2IMG, | ||
sampler=Sampler.K_DPMPP_SDE, | ||
|
||
image=image, | ||
strength=0.6, | ||
|
||
reference_image=reference_image, | ||
reference_strength=0.6, | ||
reference_information_extracted=1, | ||
|
||
add_original_image=True, # This Not affect the vibe generation | ||
qualityToggle=True, | ||
) | ||
print(f"charge: {agent.calculate_cost(is_opus=True)} if you are vip3") | ||
print(f"charge: {agent.calculate_cost(is_opus=False)} if you are not vip3") | ||
result = await agent.request( | ||
session=credential, remove_sign=True | ||
) | ||
except APIError as e: | ||
print(f"Error: {e.message}") | ||
return None | ||
else: | ||
print(f"Meta: {result.meta}") | ||
_res: ImageGenerateResp | ||
file = result.files[0] | ||
with open(f"{pathlib.Path(__file__).stem}.png", "wb") as f: | ||
f.write(file[1]) | ||
|
||
|
||
load_dotenv() | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(generate()) |
Oops, something went wrong.