Skip to content

Commit

Permalink
AI optimization message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vipertechofficial committed Jun 23, 2024
1 parent 0fab689 commit 51579a4
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 89 deletions.
2 changes: 1 addition & 1 deletion client/chunk_11.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/chunk_24.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ var LOAD_FILES_USEFUL = ["/src/fonts/normative/index.css"].concat(["illusion.jpg
var LOAD_FILES_STATIC = ["sfx/md/hero_decorative-celebration-02", "sfx/md/navigation_selection-complete-celebration", "sfx/md/navigation_transition-left", "sfx/md/state-change_confirm-down", "sfx/md/ui_lock", "sfx/md/ui_unlock", "sfx/md/ui_scan", "sfx/md/alert_high-intensity", "sfx/md/navigation_transition-right", "voice/cn/accessing_memory", "voice/cn/complete", "voice/cn/please_wait", "voice/cn/data_upload", "voice/cn/processing", "voice/cn/enhanced", "voice/cn/rewriting_deep_layer_protocols", "voice/cn/vision_activated", "voice/cn/vision_deactivated", "voice/cn/filtering", "music/redeclipse/track_09"].map(F_SND).concat(["presentation", "tutorial", "create", "enhanced", "pixelated", "upload", "share1", "joke1", "create", "enhanced", "pixelated", "presentation", "presentation2", "sponsors", "tutorial", "upload", "labintro", "share2", "share3", "share4", "share5", "share6", "share7", "joke2", "joke3", "joke4", "joke5", "joke6", "joke7", "joke8", "joke9", "joke10", "joke11"].map(F_VID));

// Cache names
var REQUIRED_CACHE = "unless-update-cache-v1058-required";
var USEFUL_CACHE = "unless-update-cache-v1058-useful";
var STATIC_CACHE = "unless-update-cache-v1058-static";
var OTHER_CACHE = "unless-update-cache-v1058-other";
var REQUIRED_CACHE = "unless-update-cache-v1059-required";
var USEFUL_CACHE = "unless-update-cache-v1059-useful";
var STATIC_CACHE = "unless-update-cache-v1059-static";
var OTHER_CACHE = "unless-update-cache-v1059-other";

// Regular expressions for chunk matching
var MAIN_CHILD_CHUNK_REGEX = /chunk_(main_[a-z0-9]+)\.min\.js$/i;
Expand Down
31 changes: 3 additions & 28 deletions src/js/pages/Pixel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1889,8 +1889,8 @@ class Pixel extends React.PureComponent {
}
}

const longCaptionerAPI = new LongCaptionerAPI();
const faceToAllAPI = new FaceToAllAPI();
const longCaptionerAPI = new LongCaptionerAPI(actions.trigger_snackbar);
const faceToAllAPI = new FaceToAllAPI(actions.trigger_snackbar);

if (smart_file === null && dumb_file === null) {
actions.trigger_snackbar("Looks like I can't get your file as something is erroneous.", 5700);
Expand Down Expand Up @@ -1952,43 +1952,18 @@ class Pixel extends React.PureComponent {
actions.jamy_update("angry");
actions.trigger_voice("processing");
actions.trigger_loading_update(10);
actions.trigger_snackbar("Uploading file (3 sec)");

actions.jamy_update("annoyed", 8000);
actions.trigger_loading_update(20);
actions.trigger_snackbar("Creating captions (10 sec)");
setTimeout(() => {
actions.jamy_update("annoyed", 8000);
actions.trigger_loading_update(20);
actions.trigger_snackbar("Creating captions (5/10)");
}, 5000);


const prompt = await longCaptionerAPI.run(file)
const prompt = await longCaptionerAPI.run(file);
actions.trigger_snackbar("IMAGE: " + prompt, 5000);

setTimeout(() => {
actions.jamy_update("flirty", 8000);
actions.trigger_loading_update(40);
actions.trigger_snackbar("Loading pixel art style (3 sec)");
}, 4000);

setTimeout(() => {
actions.jamy_update("annoyed", 5000);
actions.trigger_loading_update(60);
actions.trigger_snackbar("Waiting on generation (15 sec)");
setTimeout(() => {
actions.jamy_update("annoyed", 8000);
actions.trigger_loading_update(20);
actions.trigger_snackbar("Waiting on generation (5/15)");
}, 5000);
setTimeout(() => {
actions.jamy_update("annoyed", 8000);
actions.trigger_loading_update(20);
actions.trigger_snackbar("Waiting on generation (10/15)");
}, 10000);
}, 4000);

const blob = await faceToAllAPI.run(file, prompt)
actions.jamy_update("flirty", 666);
actions.trigger_loading_update(100);
Expand Down
142 changes: 87 additions & 55 deletions src/js/utils/AI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class HuggingFaceAPI {
constructor(baseUrl) {
constructor(baseUrl, msgCallback) {
this.baseUrl = baseUrl;
this.msgCallback = msgCallback || function(){};
}

generateRandomSeed() {
Expand All @@ -10,6 +11,41 @@ class HuggingFaceAPI {
return Math.round(Math.random() * 0xFFFFFFFF);
}
}

message(line) {
try {
const data = JSON.parse(line.slice(5)) || {};
const message = data.msg;
const previous_date = this.date_message || Date.now() - 3000;

if(previous_date + 3000 <= Date.now()) {
this.date_message = Date.now();
switch (message) {
case "estimation":
const rank_eta = data.rank_eta || 0;
const queue_size = data.queue_size || 0;
const queue_length = queue_size - 1;
const rank = data.rank || 0;
const string1 = `Estimated waiting duration: ${parseInt(rank_eta)} seconds. ${queue_length > rank ? `Process will start after ${queue_length} remaining job${queue_length ? "s": ""}`: "Process will start immediately"}`;
this.msgCallback(string1);
break;
case "progress":
const progress_data = data.progress_data || [];
const progress_data_inside = progress_data[0] || {};
const index = progress_data_inside.index;
const length = progress_data_inside.length;
const unit = progress_data_inside.unit;
const string2 = `Processing: ${index}/${length} ${unit}.`
this.msgCallback(string2);
break;
}
}

} catch (e) {

}
}

generateRandomId() {
return Math.round(Math.random() * 0xFFFFFF).toString(16).padStart(6, "0");
}
Expand Down Expand Up @@ -139,18 +175,27 @@ class HuggingFaceAPI {
});
return Promise.resolve(response.body.getReader());
}

async fetchEventSourceJSON(url) {
const response = await fetch(url, {
headers: this.getHeadersJson()
});
const text = await response.clone().text();
const json_text = text.replaceAll("event: complete\ndata: ", "")
return Promise.resolve(JSON.parse(json_text));
}
}
class LongCaptionerAPI extends HuggingFaceAPI {
constructor() {
super("https://gokaygokay-sd3-long-captioner-v2.hf.space");
constructor(msgCallback) {
super("https://gokaygokay-sd3-long-captioner-v2.hf.space", msgCallback);
}

getCreateCaptionsUrl() {
return `${this.baseUrl}/call/create_captions_rich`;
}

getReadCaptions(line) {
return line;
return JSON.parse(line.slice(5));
}

async readResponse(reader) {
Expand All @@ -169,20 +214,11 @@ class LongCaptionerAPI extends HuggingFaceAPI {

for (let line of lines) {
line = line.trim();

if (line.startsWith("event: complete")) {
this.message(line);
if (line.includes("complete")) {
// The event is complete, so we can mark done as true
done = true;
}

if (line.startsWith("data:") && done) {
// Extract the data part of the final complete event
try {
let jsonData = JSON.parse(line.slice(5)); // Remove 'data: ' and parse JSON
finalData = jsonData;
} catch (error) {
console.error("Failed to parse data as JSON", error);
}
finalData = line;
}
}

Expand Down Expand Up @@ -220,26 +256,17 @@ class LongCaptionerAPI extends HuggingFaceAPI {
const path = await this.uploadFile(file, id);
const url = this.getCreateCaptionsUrl();
const eventId = await this.createCaptions(path);
const response = await this.fetchEventSource(`${url}/${eventId}`);

try {

const line = await this.readResponse(response);
const caption = this.getReadCaptions(line);
return Promise.resolve(caption);
}catch (e) {
const json = await response();
return Promise.resolve(json[0])
}
const response = await this.fetchEventSourceJSON(`${url}/${eventId}`);
return Promise.resolve(response[0])
}
}

class FloranceCaptionerAPI extends HuggingFaceAPI {
constructor() {
super("https://gokaygokay-florence-2.hf.space");
constructor(msgCallback) {
super("https://gokaygokay-florence-2.hf.space", msgCallback);
}

getPredictHeader(path, url, size, type, hash) {
getPredictHeader(path, url, size, type, hash, short) {
return {
headers: this.getHeadersJson(),
body: JSON.stringify(
Expand All @@ -253,12 +280,12 @@ class FloranceCaptionerAPI extends HuggingFaceAPI {
mime_type: type,
meta: { _type: "gradio.FileData" }
},
"More Detailed Caption",
""
short ? "Caption": "More Detailed Caption",
short ? "microsoft/Florence-2-large-ft": ""
],
event_data:null,
fn_index:3,
trigger_id:7,
trigger_id: short ? 9: 7,
session_hash:hash
}
),
Expand Down Expand Up @@ -290,7 +317,7 @@ class FloranceCaptionerAPI extends HuggingFaceAPI {

for (let line of lines) {
line = line.trim();

this.message(line);
if (line.includes("complete")) {
// The event is complete, so we can mark done as true
done = true;
Expand All @@ -305,18 +332,22 @@ class FloranceCaptionerAPI extends HuggingFaceAPI {
return Promise.resolve(finalData);
}

readLine(line) {
readLine(line, short) {
console.log(line)
line = line.slice(5).trim();
line = line.slice(5);
const json = JSON.parse(line);
const output = json.output || {};
const data = output.data || [];
const response = data[0];
const responseSliced = response.slice(29, response.length-2);
return responseSliced;
if(json.success){
const output = json.output || {};
const data = output.data || [];
const response = data[0];
const responseSliced = short ? response.slice(16, response.length-2): response.slice(29, response.length-2);
return Promise.resolve(responseSliced);
} else {
return Promise.resolve("");
}
}

async run(input) {
async run(input, short) {

let file;
if(typeof input === "string"){
Expand All @@ -330,15 +361,15 @@ class FloranceCaptionerAPI extends HuggingFaceAPI {
const hash = this.generateRandomId();
const path = await this.uploadFile(file, hash);
const url = this.getCreateImagePathUrl(path);
const header = this.getPredictHeader(path, url, file.size, file.type, hash)
const header = this.getPredictHeader(path, url, file.size, file.type, hash, short)
const responseQueue = await fetch(this.getQueueJoinUrl(), header);
const responseQueueJSON = await responseQueue.json();
const event_id = responseQueueJSON.event_id;
if(event_id) {

const response = await this.fetchEventSource(this.getResultUrl(hash));
const line = await this.readResponse(response);
return Promise.resolve(this.readLine(line));
return this.readLine(line, short);
}else {

return Promise.reject();
Expand All @@ -348,8 +379,8 @@ class FloranceCaptionerAPI extends HuggingFaceAPI {
}

class RemoveBackgroundAPI extends HuggingFaceAPI {
constructor() {
super("https://kenjiedec-rembg.hf.space");
constructor(msgCallback) {
super("https://kenjiedec-rembg.hf.space", msgCallback);
}

getQueuePushUrl() {
Expand Down Expand Up @@ -459,8 +490,8 @@ class RemoveBackgroundAPI extends HuggingFaceAPI {
}

class ImageCreatorAPI extends HuggingFaceAPI {
constructor() {
super("https://pixart-alpha-pixart-sigma.hf.space");
constructor(msgCallback) {
super("https://pixart-alpha-pixart-sigma.hf.space", msgCallback);
}
getPredictHeader(prompt, hash, width = 512, height = 512, number = 1, style = "(No style)", negative_prompt="bad shape, disformed, photography, photo, realistic, photo-realistic.", solver = "DPM-Solver") {
const seed = this.generateRandomSeed();
Expand Down Expand Up @@ -519,7 +550,7 @@ class ImageCreatorAPI extends HuggingFaceAPI {

for (let line of lines) {
line = line.trim();

this.message(line);
if (line.includes("complete")) {
// The event is complete, so we can mark done as true
done = true;
Expand Down Expand Up @@ -590,8 +621,8 @@ class ImageCreatorAPI extends HuggingFaceAPI {
}
}
class FaceToAllAPI extends HuggingFaceAPI {
constructor() {
super("https://abidlabs-face-to-all.hf.space");
constructor(msgCallback) {
super("https://abidlabs-face-to-all.hf.space", msgCallback);
}

getPredictUrl() {
Expand Down Expand Up @@ -622,7 +653,7 @@ class FaceToAllAPI extends HuggingFaceAPI {

getQueueJoinHeader(path, url, size, type, prompt, hash) {

const finalPrompt = `A low color number palette based (retrowave:1.25) pixel art (pixelart:1.75) in lucasarts style of ${prompt}... (lucasarts_style:1.5). Truthful facial traits, highly detailed face for a pixel art, retro video game art, masterpiece retro game art, beautiful, 2D, illustration, computer art, computer retro, pixelized, crisp-edge.`;
const finalPrompt = `A low color number (retrowave:1.25) pixel art (pixelart:1.75) in lucasarts style of "${prompt}"(0.5). Truthful palette, (lucasarts_style:1.5). Truthful facial traits, highly detailed face for a pixel art, retro video game art, masterpiece retro game art, beautiful, 2D, illustration, computer art, computer retro, pixelized, crisp-edge.`;

return {
headers: this.getHeadersJson(),
Expand All @@ -631,12 +662,12 @@ class FaceToAllAPI extends HuggingFaceAPI {
path: path, url: url, orig_name: "image."+type.replaceAll("image/", ""), size: size, mime_type: type, meta: { _type: "gradio.FileData" }
},
finalPrompt,
"Photography, bad light, too much colors, missing fingers, bad result, error, unsatisfying, photo, picture, photo-realistic, real, realistic.",
1.00,
"Photography, bad light, too much colors, missing fingers, bad result, error, unsatisfying, photo, picture, photo-realistic.",
0.95,
null,
0.85,
0.10,
12.5,
9.0,
0.75,
null,
null
Expand Down Expand Up @@ -669,6 +700,7 @@ class FaceToAllAPI extends HuggingFaceAPI {
// Process each line
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
this.message(line);
if (line.includes("complete")) {
done = true;
finalLine = line;
Expand Down

0 comments on commit 51579a4

Please sign in to comment.