-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
152 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,33 @@ | |
* `/nai <word>` 生成一张图片 | ||
例如 `/nai 连裤袜 双马尾` (只有部分词条会自动翻译) | ||
例如 `/nai swimsuit #seed=12346` (设置种子) | ||
例如 `/nai swimsuit #steps=3` (AI迭代次数) | ||
例如 `/nai "swimsuit, ahegao"` (如果需要以 `,` 分割词条, 请用 `"` 包裹) | ||
可用的配置项有 | ||
`seed` 种子 | ||
`steps` 迭代次数 | ||
`width` 宽度 | ||
`height` 高度 | ||
`samples` 出图数量 | ||
`scale` 比例 | ||
`sampler` 采样器 可选值 `k_euler_ancestral`, `k_euler`, `k_lms`, `plms`, `ddim` | ||
`strength` 以图出图中对原图的更改程度 可选值 [0.00, 0.99] | ||
`noise` 以图出图中的噪声 可选值 [0.00, 0.99] | ||
* `/nai-login <mail> <password>` 登录账号 | ||
例如 `/nai-login [email protected] 1919810` | ||
* `/naifu <word>` 生成一张图片 | ||
对接 `naifu`, `naifu` 是基于 novelai 官方 web 端的修改版,所以指令用法 和 `nai` 一致 | ||
|
||
## 配置 | ||
|
||
* `config.yml` 配置文件 包括 `proxy`, `doh`, `ipv6` 等配置 | ||
* `config.yml` 配置文件 包括 `proxy`, `doh`, `ipv6`, `naifu_api` 等配置 | ||
* `ban.txt` 屏蔽的词条,可热编辑,保存后一段时间会自动启用 | ||
|
||
## NaiFu | ||
|
||
`naifu` 是基于 novelai 官方 web 端的修改版 | ||
相关信息可以看这 <https://www.bilibili.com/video/BV14e4y1E74X> | ||
|
||
## TODO | ||
|
||
* [ ] 更好的翻译 | ||
|
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
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/xyz/cssxsh/mirai/novelai/command/NovelAiFuCommand.kt
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,81 @@ | ||
package xyz.cssxsh.mirai.novelai.command | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.util.* | ||
import io.ktor.utils.io.core.* | ||
import kotlinx.serialization.json.* | ||
import net.mamoe.mirai.console.command.* | ||
import net.mamoe.mirai.message.data.* | ||
import net.mamoe.mirai.message.data.Image.Key.queryUrl | ||
import net.mamoe.mirai.message.data.MessageSource.Key.quote | ||
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource | ||
import xyz.cssxsh.mirai.novelai.* | ||
import xyz.cssxsh.mirai.novelai.data.* | ||
import xyz.cssxsh.novelai.* | ||
import kotlin.random.* | ||
|
||
public object NovelAiFuCommand : SimpleCommand( | ||
owner = NovelAiHelper, | ||
"nai-fu", "naifu", | ||
description = "生成图片" | ||
) { | ||
public val client: NovelAiClient = NovelAiClient(config = NovelAiHelperConfig, default = false) | ||
|
||
private val random = Random(seed = System.currentTimeMillis()) | ||
|
||
@Handler | ||
public suspend fun CommandSenderOnMessage<*>.handle(vararg tags: String) { | ||
this as UserCommandSender | ||
val input: MutableSet<String> = HashSet() | ||
val params: MutableMap<String, String> = HashMap() | ||
for (tag in tags) { | ||
when { | ||
tag.startsWith("#") -> { | ||
try { | ||
val (_, key, value) = tag.split('#', '=') | ||
params[key] = value | ||
} catch (_: Exception) { | ||
// | ||
} | ||
} | ||
tag.startsWith("[") -> continue | ||
else -> { | ||
input.add(NovelAiHelper.translate(word = tag) ?: tag) | ||
} | ||
} | ||
} | ||
fromEvent.message.findIsInstance<Image>()?.let { source -> | ||
try { | ||
val url = source.queryUrl() | ||
val response = client.http.get(url) | ||
val packet = response.body<ByteReadPacket>() | ||
params["image"] = packet.encodeBase64() | ||
} catch (cause: Exception) { | ||
NovelAiHelper.logger.warning("download image fail", cause) | ||
null | ||
} | ||
} | ||
|
||
val seed = random.nextLong(0, 2 shl 32 - 1) | ||
NovelAiHelper.logger.info(input.joinToString(", ", "generate image seed: $seed, tags: ")) | ||
val generate = try { | ||
client.ai.generateImage(input = input.joinToString(",")) { | ||
put("seed", seed) | ||
params.forEach { (key, value) -> | ||
when { | ||
key == "image" -> put(key, value) | ||
value.toDoubleOrNull() != null -> put(key, value.toDouble()) | ||
value.toBooleanStrictOrNull() != null -> put(key, value.toBoolean()) | ||
else -> put(key, value) | ||
} | ||
} | ||
} | ||
} catch (cause: NovelAiApiException) { | ||
sendMessage(cause.error.message) | ||
return | ||
} | ||
val image = subject.uploadImage(generate.data.toExternalResource().toAutoCloseable()) | ||
sendMessage(fromEvent.message.quote() + image + "\n$seed") | ||
} | ||
} |
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
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
Oops, something went wrong.