View Horde kudos

This commit is contained in:
SillyLossy
2023-05-21 02:55:47 +03:00
parent 91315b4a74
commit 71d1688dfa
3 changed files with 92 additions and 44 deletions

View File

@ -1023,6 +1023,9 @@
<h5>Get it here: <a target="_blank" href="https://horde.koboldai.net/register">Register</a><br> <h5>Get it here: <a target="_blank" href="https://horde.koboldai.net/register">Register</a><br>
Enter <span class="monospace">0000000000</span> to use anonymous mode. Enter <span class="monospace">0000000000</span> to use anonymous mode.
</h5> </h5>
<div>
<a id="horde_kudos" href="javascript:void(0);">View my Kudos</a>
</div>
<div class="flex-container"> <div class="flex-container">
<input id="horde_api_key" name="horde_api_key" class="text_pole flex1" maxlength="500" type="text" placeholder="0000000000" autocomplete="off"> <input id="horde_api_key" name="horde_api_key" class="text_pole flex1" maxlength="500" type="text" placeholder="0000000000" autocomplete="off">
<div title="Clear your API key" class="menu_button fa-solid fa-circle-xmark clear-api-key" data-key="api_key_horde"></div> <div title="Clear your API key" class="menu_button fa-solid fa-circle-xmark clear-api-key" data-key="api_key_horde"></div>
@ -2644,7 +2647,7 @@
<script> <script>
// Configure toast library: // Configure toast library:
toastr.options.escapeHtml = true; // Prevent raw HTML inserts toastr.options.escapeHtml = false; // Prevent raw HTML inserts
toastr.options.timeOut = 4000; // How long the toast will display without user interaction toastr.options.timeOut = 4000; // How long the toast will display without user interaction
toastr.options.extendedTimeOut = 10000; // How long the toast will display after a user hovers over it toastr.options.extendedTimeOut = 10000; // How long the toast will display after a user hovers over it
toastr.options.progressBar = true; // Visually indicate how long before a toast expires. toastr.options.progressBar = true; // Visually indicate how long before a toast expires.

View File

@ -185,6 +185,28 @@ function loadHordeSettings(settings) {
$('#horde_auto_adjust_context_length').prop("checked", horde_settings.auto_adjust_context_length); $('#horde_auto_adjust_context_length').prop("checked", horde_settings.auto_adjust_context_length);
} }
async function showKudos() {
const response = await fetch('/horde_userinfo', {
method: 'POST',
headers: getRequestHeaders(),
});
if (!response.ok) {
toastr.warning('Could not load user info from Horde. Please try again later.');
return;
}
const data = await response.json();
if (data.anonymous) {
toastr.info('You are in anonymous mode. Set your personal Horde API key to see kudos.')
return;
}
console.log('Horde user data', data);
toastr.info(`${data.username}<br>Kudos: ${data.kudos}`);
}
jQuery(function () { jQuery(function () {
$("#use_horde").on("input", async function () { $("#use_horde").on("input", async function () {
horde_settings.use_horde = !!$(this).prop("checked"); horde_settings.use_horde = !!$(this).prop("checked");
@ -225,4 +247,5 @@ jQuery(function () {
}); });
$("#horde_refresh").on("click", getHordeModels); $("#horde_refresh").on("click", getHordeModels);
$("#horde_kudos").on("click", showKudos);
}) })

View File

@ -2921,54 +2921,76 @@ app.post('/horde_models', jsonParser, async (_, response) => {
response.send(models); response.send(models);
}); });
app.post('/horde_userinfo', jsonParser, async (_, response) => {
const api_key_horde = readSecret(SECRET_KEYS.HORDE);
if (!api_key_horde) {
return response.send({ anonymous: true });
}
try {
const user = await ai_horde.findUser({ token: api_key_horde });
return response.send(user);
} catch (error) {
console.error(error);
return response.sendStatus(500);
}
})
app.post('/horde_generateimage', jsonParser, async (request, response) => { app.post('/horde_generateimage', jsonParser, async (request, response) => {
const MAX_ATTEMPTS = 100; const MAX_ATTEMPTS = 100;
const CHECK_INTERVAL = 3000; const CHECK_INTERVAL = 3000;
const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY;
console.log('Stable Horde request:', request.body); console.log('Stable Horde request:', request.body);
const generation = await ai_horde.postAsyncImageGenerate(
{ try {
prompt: `${request.body.prompt_prefix} ${request.body.prompt} ### ${request.body.negative_prompt}`, const generation = await ai_horde.postAsyncImageGenerate(
params:
{ {
sampler_name: request.body.sampler, prompt: `${request.body.prompt_prefix} ${request.body.prompt} ### ${request.body.negative_prompt}`,
hires_fix: request.body.enable_hr, params:
use_gfpgan: request.body.restore_faces, {
cfg_scale: request.body.scale, sampler_name: request.body.sampler,
steps: request.body.steps, hires_fix: request.body.enable_hr,
width: request.body.width, use_gfpgan: request.body.restore_faces,
height: request.body.height, cfg_scale: request.body.scale,
karras: Boolean(request.body.karras), steps: request.body.steps,
n: 1, width: request.body.width,
height: request.body.height,
karras: Boolean(request.body.karras),
n: 1,
},
r2: false,
nsfw: request.body.nfsw,
models: [request.body.model],
}, },
r2: false, { token: api_key_horde });
nsfw: request.body.nfsw,
models: [request.body.model],
},
{ token: api_key_horde });
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
await delay(CHECK_INTERVAL); await delay(CHECK_INTERVAL);
const check = await ai_horde.getImageGenerationCheck(generation.id); const check = await ai_horde.getImageGenerationCheck(generation.id);
console.log(check); console.log(check);
if (check.done) { if (check.done) {
const result = await ai_horde.getImageGenerationStatus(generation.id); const result = await ai_horde.getImageGenerationStatus(generation.id);
return response.send(result.generations[0].img); return response.send(result.generations[0].img);
}
/*
if (!check.is_possible) {
return response.sendStatus(503);
}
*/
if (check.faulted) {
return response.sendStatus(500);
}
} }
/* return response.sendStatus(504);
if (!check.is_possible) { } catch (error) {
return response.sendStatus(503); console.error(error);
} return response.sendStatus(500);
*/
if (check.faulted) {
return response.sendStatus(500);
}
} }
return response.sendStatus(504);
}); });
function writeSecret(key, value) { function writeSecret(key, value) {