-
Notifications
You must be signed in to change notification settings - Fork 11
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
Set up nos bot to save training images #313
Changes from 1 commit
0d41589
1992adb
2068e95
02f8dbc
679e0af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,10 @@ | |
|
||
import nos | ||
from nos.client import InferenceClient, TaskType | ||
from nos.constants import NOS_TMP_DIR | ||
|
||
|
||
# Init nos server, wait for it to spin up then confirm its healthy: | ||
nos.init(runtime="gpu") | ||
nos_client = InferenceClient() | ||
nos_client.WaitForServer() | ||
if not nos_client.IsHealthy(): | ||
|
@@ -24,9 +24,13 @@ | |
# Create our bot: | ||
bot = commands.Bot(command_prefix="$", intents=intents) | ||
|
||
TRAINING_CHANNEL_NAME = "training" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
NOS_TRAINING_DIR = NOS_TMP_DIR / "train" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOS_TMP_DIR / "discord/{CHANNEL_NAME}/train" |
||
|
||
# Create a callback to read messages and generate images from prompt: | ||
@bot.command() | ||
async def generate(ctx, *, prompt): | ||
# pull the channel id so we know which model to run: | ||
response = nos_client.Run( | ||
TaskType.IMAGE_GENERATION, | ||
"stabilityai/stable-diffusion-2", | ||
|
@@ -44,6 +48,53 @@ async def generate(ctx, *, prompt): | |
await ctx.send(file=discord.File(image_bytes, filename="image.png")) | ||
|
||
|
||
@bot.command() | ||
async def train(ctx): | ||
# check that its in the training channel | ||
if ctx.channel.name != TRAINING_CHANNEL_NAME: | ||
print("not in training channel, returning!") | ||
return | ||
|
||
if not ctx.message.attachments: | ||
print("no attachments to train on, returning!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit |
||
return | ||
|
||
# create a thread for this training job: | ||
thread_name = str(ctx.message.id) | ||
thread = await ctx.channel.create_thread(name=thread_name, type=discord.ChannelType.public_thread) | ||
|
||
await thread.send(f"Created a new thread: {thread.name}") | ||
|
||
dirname = NOS_TRAINING_DIR / thread_name | ||
dirname.mkdir(parents=True, exist_ok=True) | ||
|
||
await thread.send("saving at dir: " + str(dirname)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit Use fstrings as much as possible `send(f"saving at dir: {dirname}") |
||
|
||
# save the attachments | ||
for attachment in ctx.message.attachments: | ||
print(f"got attachement: {attachment.filename}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
await attachment.save(os.path.join(dirname, attachment.filename)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Pathlib operators instead |
||
await thread.send(f"Image {attachment.filename} saved!") | ||
|
||
# Kick off a nos training run | ||
from nos.server._service import TrainingService | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to top of file |
||
|
||
svc = TrainingService() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to top of file, no need to instantiate it every time we run training |
||
job_id = svc.train( | ||
method="stable-diffusion-dreambooth-lora", | ||
training_inputs={ | ||
"model_name": "stabilityai/stable-diffusion-2-1", | ||
"instance_directory": dirname, | ||
}, | ||
metadata={ | ||
"name": "sdv21-dreambooth-lora-test-bench", | ||
}, | ||
) | ||
assert job_id is not None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. raise an error so that the discord bot user gets some internal server error |
||
|
||
thread.send(f"Started training job: {job_id}") | ||
|
||
|
||
# Pull API token out of environment and run the bot: | ||
bot_token = os.environ.get("BOT_TOKEN") | ||
if bot_token is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to a standalone Makefile under
examples/discord