Spaces:
Running
Running
import axios from "axios"; | |
import FormData from "form-data"; | |
import { fileTypeFromBuffer } from "file-type"; | |
async function toAnime(imgbuffer) { | |
// base url | |
const BASE_URL = "https://tuan2308-diffusecraft.hf.space/"; | |
// session hash | |
const session_hash = Math.random().toString(36).substring(2).slice(1); | |
// identification buffer | |
const types = await fileTypeFromBuffer(imgbuffer); | |
const filenames = "images_" + Math.floor(1000 + Math.random() * 9000) + "." + types.ext; | |
// upload form image with gradio | |
const uploadImage = async() => { | |
// input data with formdata | |
const form = new FormData(); | |
form.append("files", Buffer.from(imgbuffer, "binary"), { | |
filename: filenames, | |
contentType: types.mime | |
}); | |
// send input from gradio API | |
const res = await axios({ | |
method: "POST", | |
url: BASE_URL + "upload", | |
data: form, | |
headers: { | |
...form.getHeaders() | |
} | |
}) | |
return res.data[0] | |
} | |
const response = await axios({ | |
method: "POST", | |
url: BASE_URL + "queue/join?__theme=light", | |
data: { | |
data: [ | |
"anime", | |
"strange color, blurred , ugly, tiling, bad hand drawing, poorly drawn feet, poorly drawn face, out of frame, extra limbs, deformity, deformity, body out of frame, blur, bad anatomy, blurred, watermark, grainy, signature, cropped, draft , low detail, low quality, two faces, 2 faces, garbled, ugly, low resolution, tiled, repetitive , grainy, garbled, plastic, distorted , ostentatious, ugly, oversaturated, grainy, low resolution, distorted, blurry, bad anatomy, distortion, poorly drawn face, mutant , mutant, appendage, ugly, poorly drawn hand, missing limb, blurred, floating limb, disjointed limb, hand deformity, blurred, out of focus, long neck, long body, ugly, hideous, drawing bad, garbled, distorted, imperfect, surreal, bad hand, text, error, extra digits, less digits, garbled , worst quality , low quality, normal quality, jpeg artifact, signature, watermark, username, blurry, artist name, multiple hands, extra limb, extra finger, conjoined finger, deformed finger, old, bad eye, imperfect eye, misaligned eye , unnatural face, stiff face, stiff body, disproportionate, unnatural body, lack of body, unclear details, sticky details, low details, details distorted, ugly hands, imperfect hands, (deformed hands and fingers: 1.5), (long body :1.3), (mutated, poorly drawn :1.2) bad hands, fused hands , lost hand, arm disappeared, thighs disappeared, calves disappeared, legs disappeared, ui, lost fingers, (((((realistic, semi-realistic, render, outline)))), already crop, worst quality, low quality, jpeg shaping, ugly, duplicate, garbled, out of frame, extra fingers, mutated hand, poorly drawn hand, bad drawn face, mutated variable, deformed, blurred, dehydrated, bad anatomy, bad proportions, superfluous limbs, cloned face, deformity, gross proportions, deformed limbs, lost hands, lost legs, extra arms, extra legs , sticky fingers, too many fingers , long neck, text, close-up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, sick, cropped amputee, extra fingers, mutated hand, bad hand drawing, bad drawn face, mutant, deformed, blurred, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, deformity, gross proportions, deformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, blurred, deformed face, variable hands shape, deformed fingers, ugly, bad anatomy, extra fingers, extra hands, deformed eyes, logo, text (:1.2) (:1.2) , (:1.2) sketch, (poor quality) best:1.4), (low quality:1.4), (normal quality:1.4), low quality, bad anatomy, bad hand, vaginal vagina, ((monochrome)), ((scale) gray)), drooping eyelids, many eyebrows, (cropped), oversaturated, extra limb, amputee, deformed arms, long neck, long body, imperfection, (bad hands), signature, figure blurred, username, artist name, conjoined fingers, deformed fingers, bad eyes, imperfect eyes, misaligned eyes, unnatural face, unnatural body, error, bad photo, bad photo", | |
1, | |
28, | |
7.5, | |
true, | |
-1, | |
"loras/3DMM_V12.safetensors", | |
0.5, | |
null, | |
0.33, | |
null, | |
0.33, | |
null, | |
0.33, | |
null, | |
0.33, | |
"Euler a", | |
1024, | |
1024, | |
"cagliostrolab/animagine-xl-3.1", | |
null, | |
"img2img", | |
{ | |
path: await (await uploadImage()), | |
meta: { | |
_type: "gradio.FileData" | |
}, | |
mimetype: types.mime, | |
orig_name: filenames, | |
size: imgbuffer?.length, | |
url: BASE_URL + await (await uploadImage()) | |
}, | |
"Canny", | |
512, | |
1024, | |
[], | |
null, | |
null, | |
0.59, | |
100, | |
200, | |
0.1, | |
0.1, | |
1, | |
0, | |
1, | |
false, | |
"Compel", | |
null, | |
1.4, | |
100, | |
10, | |
30, | |
0.55, | |
"Use same sampler", | |
"", | |
"", | |
false, | |
true, | |
1, | |
true, | |
false, | |
true, | |
true, | |
false, | |
"./images", | |
false, | |
false, | |
false, | |
true, | |
1, | |
0.55, | |
false, | |
true, | |
false, | |
true, | |
false, | |
"Use same sampler", | |
false, | |
"", | |
"", | |
0.35, | |
true, | |
true, | |
false, | |
4, | |
4, | |
32, | |
false, | |
"", | |
"", | |
0.35, | |
true, | |
true, | |
false, | |
4, | |
4, | |
32, | |
false, | |
null, | |
null, | |
"plus_face", | |
"original", | |
0.7, | |
null, | |
null, | |
"base", | |
"style", | |
0.7 | |
], | |
event_data: null, | |
fn_index: 11, | |
session_hash, | |
trigger_id: 14 | |
} | |
}); | |
if(response.data.event_id) { | |
const res = await axios({ | |
method: "GET", | |
url: BASE_URL + "queue/data?session_hash=" + session_hash | |
}); | |
const splited = res.data.split("\n"); | |
const processStartsLine = splited.find(line => line.includes('process_completed')); | |
const processStartsData = JSON.parse(processStartsLine.replace('data: ', '')); | |
const processStartsLine2 = splited.find(line => line.includes('process_generating')); | |
const processStartsData2 = JSON.parse(processStartsLine2.replace('data: ', '')); | |
if(processStartsData?.success) { | |
return processStartsData.output.data; | |
} else if(processStartsData2?.success) { | |
return processStartsData2.output.data; | |
} | |
} | |
} | |
export { toAnime } |