Skip to content

Commit

Permalink
fixed chapters crashing, using hf tokens instead of a Google Drive li…
Browse files Browse the repository at this point in the history
…nk, made readme easier to understand
  • Loading branch information
peterwilli committed Sep 15, 2022
1 parent d3c8651 commit 214367d
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 43 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ What you need:

- Follow the tutorial to install JCloud: https://docs.jina.ai/fundamentals/jcloud
- Create a directory where we will set up our bot
- Download [the following](/flow_server.yml) file in the directory
- Download [the following](https://raw.githubusercontent.com/peterwilli/Thingy/main/flow_server.yml) file in the directory
- In the directory, run `jc deploy flow_server.yml` to launch your own bot server! This may take a while
- You should get a URL similar to `xxxxx.wolf.jina.ai`, make note of that URL as you need it in the bot configuration!
- Download [this file](/config.example.yml) and name it `config.yml`, put it into the bot directory.
- Download [this file](https://raw.githubusercontent.com/peterwilli/Thingy/main/config.example.yml) and name it `config.yml`, put it into the bot directory.
- Once done, follow "Setting up the bot" and then go back here
- Download the bot interface (communicates with the AI) [from here](https://github.com/peterwilli/Thingy/releases/tag/v2-alpha-1) and put it in the bot directory
- In a terminal in the bot directory, now type `java -jar DiscordArtBot-1.0-SNAPSHOT-all.jar`
- Download the bot interface (communicates with the AI) [from here](https://github.com/peterwilli/Thingy/releases/tag/v2-alpha-2) and put it in the bot directory
- In a terminal in the bot directory, now type `java -jar Thingy-2.0-all.jar`
- In a bit, it should be up and running. Congrats, you can use `/stable_diffusion` and other peculiarities!

## Setting up the bot
Expand All @@ -60,9 +60,13 @@ What you need:
- Make a discord bot by clicking on the Bot menu, and click "Add Bot". Name it as you like
- Click "Reset Token" to reveal the bot token. Make note of this token, you need it in the next step
- In the config.yml, change the bot name and token to your bot token
- Go to [Hugginface Tokens](https://huggingface.co/settings/tokens) (make an account if you don't have one yet) and create a token
- Replace `token_from_hf` in config.yml with the token you just created
- This token allows you access to the Stable Diffusion model!
- Assuming you run the Jina Flow server as well as the Discord bot on the same machine, you don't need to do anything
- If your Jina server is remote, you have to change `host` and `port` under `grpcServer`
- If you run on JCLoud, make sure to set `port` to `443` and `plainText` to `false`!
- Make sure `host` does **not** include `grpcs://`!
- Save the config
- Go to the OAuth2 menu, and click the URL Generator submenu. Check off the Bot checkbox
- In bot permissions, you only need the following:
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ plugins {
id("com.github.johnrengelman.shadow") version "7.1.2"
}

group = "org.example"
version = "1.0-SNAPSHOT"
group = "org.thingy"
version = "2.0"

repositories {
mavenCentral()
Expand Down
1 change: 1 addition & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bot:
name: "My AI bot!"
token: "my_bot_token_keep_secret"
hfToken: "token_from_hf"
ownerId: "580050773522513920"
maxEntriesPerOwner: 1
hostConstraints:
Expand Down
6 changes: 3 additions & 3 deletions flow_server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ monitoring: true
executors:
# Stable Diffusion
- name: stable_diffusion_txt2img
uses: jinahub+docker://Txt2ImgExecutor/0.1.2
uses: jinahub+docker://Txt2ImgExecutor/0.1.3
jcloud:
resources:
gpu: 1
cpu: 1
- name: stable_diffusion_img2img
uses: jinahub+docker://Img2ImgExecutor/0.1.1
uses: jinahub+docker://Img2ImgExecutor/0.1.3
jcloud:
resources:
gpu: 1
cpu: 1
cpu: 1
4 changes: 1 addition & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@

rootProject.name = "DiscordArtBot"

rootProject.name = "Thingy"
3 changes: 2 additions & 1 deletion src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ data class GRPCServer(
data class Bot(
val name: String,
val ownerId: String,
val token: String
val token: String,
val hfToken: String
)

data class Timeout(
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fun initCommands(jda: JDA) {
slash("stable_diffusion", "Making things with Stable Diffusion!") {
option<String>("prompt", "Prompt to make i.e 'Monkey holding a beer'", required = true)
option<String>("ar", "aspect ratio (i.e 16:9)", required = false)
option<String>("init_image", "A link to an image you wish to use as start!", required = false)
option<Double>("guidance_scale", "How much guidance to the prompt?", required = false)
option<Int>(
"seed",
"Entropy for the random number generator, use the same seed to replicate results!",
Expand All @@ -90,7 +90,7 @@ fun initCommands(jda: JDA) {
required = false
)
option<Double>("strength", "How strong the change needs to be?", required = false)
option<Double>("guidance_scale", "How much guidance of the original image?", required = false)
option<Double>("guidance_scale", "How much guidance to the prompt?", required = false)
option<Int>("steps", "How much steps from the original image?", required = false)
}
slash("link2img", "Make an existing image into your prompt!") {
Expand All @@ -103,7 +103,7 @@ fun initCommands(jda: JDA) {
required = false
)
option<Double>("strength", "How strong the change needs to be?", required = false)
option<Double>("guidance_scale", "How much guidance of the original image?", required = false)
option<Double>("guidance_scale", "How much guidance to the prompt?", required = false)
option<Int>("steps", "How much steps from the original image?", required = false)
}
slash("update", "[Admin only] Update mode: Prevents new images from being created for updating the bot") {
Expand All @@ -128,7 +128,7 @@ fun initCommands(jda: JDA) {
}
slash("stats", "See server and bot stats!") {
}
slash("set_background", "Set profile background!") {
slash("edit_profile", "Set profile background!") {
}
}.queue()
}
20 changes: 15 additions & 5 deletions src/main/kotlin/commands/chapters/ListChaptersCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import gson
import miniManual
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.interactions.components.buttons.ButtonStyle
import net.dv8tion.jda.api.utils.messages.MessageEditData
import replyPaginator
import ui.ImageSliderEntry
import ui.sendImageSlider
Expand All @@ -26,6 +27,7 @@ fun listChaptersCommand(jda: JDA) {
.setEphemeral(true).queue()
return@onCommand
}
event.deferReply(true).queue()
val possibleChapters =
chapterDao.queryBuilder().orderBy("creationTimestamp", false).selectColumns().where()
.eq("userID", user.id).query()
Expand All @@ -49,23 +51,30 @@ fun listChaptersCommand(jda: JDA) {
user = event.user
) {
val chapter = possibleChapters[slider.getIndex()]
val parameters = gson.fromJson(chapter.getLatestEntry().parameters, Array<DiffusionParameters>::class.java)
val parameters =
gson.fromJson(chapter.getLatestEntry().parameters, Array<DiffusionParameters>::class.java)

val updateBuilder = userDao.updateBuilder()
updateBuilder.where().eq("id", chapter.userID)
updateBuilder.updateColumnValue("currentChapterId", chapter.id)
updateBuilder.update()

it.reply_("${parameters.first().getPrompt()} is now your current chapter! You can use editing commands such as `/upscale`, `/variate` to edit it! Enjoy!").setEphemeral(true).queue()
it.reply_(
"${
parameters.first().getPrompt()
} is now your current chapter! You can use editing commands such as `/upscale`, `/variate` to edit it! Enjoy!"
).setEphemeral(true).queue()
}, jda.button(
label = "\uD83D\uDDD1",
style = ButtonStyle.DANGER,
user = event.user
) { deleteEvent ->
val chapter = possibleChapters[slider.getIndex()]
val parameters = gson.fromJson(chapter.getLatestEntry().parameters, Array<DiffusionParameters>::class.java)
val parameters =
gson.fromJson(chapter.getLatestEntry().parameters, Array<DiffusionParameters>::class.java)

deleteEvent.reply_("**Are you sure to delete this chapter?** *${parameters.first().getPrompt()}*").setEphemeral(true).addActionRow(listOf(
deleteEvent.reply_("**Are you sure to delete this chapter?** *${parameters.first().getPrompt()}*")
.setEphemeral(true).addActionRow(listOf(
jda.button(
label = "Delete!",
style = ButtonStyle.DANGER,
Expand All @@ -83,7 +92,8 @@ fun listChaptersCommand(jda: JDA) {
}
)).queue()
})
event.replyPaginator(slider).setEphemeral(true).queue()
event.hook.editOriginal(MessageEditData.fromCreateData(slider.also { jda.addEventListener(it) }.pages[0]))
.setComponents(slider.getControls()).queue()
} catch (e: Exception) {
e.printStackTrace()
event.reply_("**Error!** $e").setEphemeral(true).queue()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/commands/make/FairQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ data class FairQueueEntry(
return stringBuilder.toString()
}

fun progressUpdate(message: String) {
progressHook.editOriginal(message).queue()
suspend fun progressUpdate(message: String): Message {
return progressHook.editOriginal(message).await()
}

suspend fun progressUpdate(message: String, fileBytes: ByteArray, fileName: String): Message {
Expand Down
19 changes: 5 additions & 14 deletions src/main/kotlin/commands/make/QueueDispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,12 @@ class QueueDispatcher(private val jda: JDA) {
dispatch(entry)
} catch (e: StatusException) {
e.printStackTrace()
entry.progressDelete()
entry.getChannel()
.sendMessage("${entry.getMember().asMention} Connection to the AI server has failed, it's likely that the bot is offline, we're sorry, please try again later!")
.queue()
val finishMsg = entry.progressUpdate("*Failed* :(")
finishMsg.reply_("${entry.getMember().asMention} Connection to the AI server has failed, it's likely that the bot is offline, we're sorry, please try again later!").queue()
} catch (e: Exception) {
try {
e.printStackTrace()
entry.progressDelete()
entry.getChannel()
.sendMessage("${entry.getMember().asMention} There's an error in the queue dispatcher: $e")
.queue()
} catch (e: Exception) {
println("Error here means the bot most likely tries to post in a message it doesn't have access to!")
e.printStackTrace()
}
e.printStackTrace()
val finishMsg = entry.progressUpdate("*Failed* :(")
finishMsg.reply_("${entry.getMember().asMention} There's an error in the queue dispatcher: $e").queue()
}
}
delay(1000)
Expand Down
15 changes: 13 additions & 2 deletions src/main/kotlin/commands/make/StableDiffusionParameters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class StableDiffusionParameters(
var initImage: URI? = null,
var ratio: Ratio = Ratio(),
var steps: Int = 50,
val guidanceScale: Double? = null,
val guidanceScale: Double = 7.5,
val strength: Double? = null
)

Expand All @@ -42,7 +42,12 @@ fun optionsToStableDiffusionParams(
seed = seed,
artID = "${config.bot.name}-${randomString(alphanumericCharPool, 32)}",
stableDiffusionParameters = StableDiffusionParameters(
prompt = prompt
prompt = prompt,
guidanceScale = if(event.getOption("guidance_scale") == null) {
7.5
} else {
event.getOption("guidance_scale")!!.asDouble
}
),
discoDiffusionParameters = null
)
Expand Down Expand Up @@ -71,5 +76,11 @@ fun optionsToStableDiffusionParams(
params.stableDiffusionParameters!!.initImage = base64Init
}

val guidanceScaleOption = event.getOption("guidance_scale")
if (initImageURLOption != null) {
val image = ImageIO.read(URL(initImageURLOption.asString))
val base64Init = bufferedImageToDataURI(image)
params.stableDiffusionParameters!!.initImage = base64Init
}
return params
}
8 changes: 5 additions & 3 deletions src/main/kotlin/discoart/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.protobuf.*
import com.google.protobuf.Struct.Builder
import commands.make.DiffusionParameters
import commands.make.DiscoDiffusionConfig
import config
import docarray.documentArrayProto
import docarray.documentProto
import io.grpc.ManagedChannel
Expand Down Expand Up @@ -176,6 +177,9 @@ class Client(

private fun addDefaultStableDiffusionParameters(params: DiffusionParameters, builder: Builder) {
val stableParams = params.stableDiffusionParameters!!
builder.putFields("hf_auth_token", value {
stringValue = config.bot.hfToken
})
builder.putFields("steps", value { numberValue = stableParams.steps.toDouble() })
builder.putFields("seed", value { stringValue = params.seed.toString() })
builder.putFields("width_height", value {
Expand All @@ -190,9 +194,7 @@ class Client(
if(stableParams.strength != null) {
builder.putFields("strength", value { numberValue = stableParams.strength.coerceIn(0.0..1.0) })
}
if(stableParams.guidanceScale != null) {
builder.putFields("guidance_scale", value { numberValue = stableParams.guidanceScale.coerceIn(0.0..50.0) })
}
builder.putFields("guidance_scale", value { numberValue = stableParams.guidanceScale.coerceIn(0.0..50.0) })
}

private fun addDefaultDiscoDiffusionParameters(params: DiffusionParameters, builder: Builder) {
Expand Down

0 comments on commit 214367d

Please sign in to comment.