Spaces:
Sleeping
Sleeping
import { assertExists } from '@std/assert'; | |
import { emptyDirSync, ensureDirSync } from '@std/fs'; | |
import { delay } from '@std/async'; | |
import { randomIntegerBetween, sample } from '@std/random'; | |
import * as hub from '@huggingface/hub'; | |
import { generateImage } from './ai.ts'; | |
import { getDirectoryStructureString, getFilesInDirectory, randomString } from './utils.ts'; | |
const HUGGINGFACE_ACCESS_TOKEN = Deno.env.get('HUGGINGFACE_ACCESS_TOKEN')!; | |
assertExists(HUGGINGFACE_ACCESS_TOKEN); | |
const REPO_ID = Deno.env.get('REPO_ID')!; | |
assertExists(REPO_ID); | |
ensureDirSync('./images'); | |
Deno.serve({ port: 7860 }, async () => { | |
return new Response(await getDirectoryStructureString('./', '', Infinity)); | |
}); | |
async function main() { | |
const files = await getFilesInDirectory('./images'); | |
console.info('files:', files.length); | |
if (files.length >= 100) { | |
const repo: hub.RepoDesignation = { type: 'dataset', name: REPO_ID }; | |
const result = await hub.uploadFiles({ | |
repo, | |
accessToken: HUGGINGFACE_ACCESS_TOKEN, | |
files: files.map((file) => ({ | |
path: `./images/${file}`, | |
content: new Blob([Deno.readFileSync(`./images/${file}`)]), | |
})), | |
}); | |
console.log(result); | |
emptyDirSync('./images'); | |
} | |
// const prompt = `Photorealistic rendering, hyper-detailed textures, cinematic lighting with volumetric effects, sharp focus with shallow depth of field, subtle chromatic aberration, high dynamic range, meticulous surface imperfections, natural color grading, ultra-high resolution, capturing nuanced light and shadow play, emphasizing tactile qualities, ambient occlusion, advanced ray tracing, realistic lens flares, precise material properties, capturing the essence of observed reality. ` + | |
// Array.from({ length: randomIntegerBetween(100, 200) }, () => randomString(randomIntegerBetween(1, 100))).join(' '); | |
const prompt = Array.from({ length: randomIntegerBetween(100, 200) }, () => randomString(randomIntegerBetween(1, 100))).join(' '); | |
const imgArrayBuffer = await generateImage('black-forest-labs/FLUX.1-schnell', { | |
inputs: prompt, | |
parameters: { | |
width: 1280, | |
height: 1280, | |
guidance_scale: 1.7, | |
num_inference_steps: 4, | |
}, | |
}); | |
Deno.writeFileSync(`./images/${Date.now()}.jpg`, new Uint8Array(imgArrayBuffer), { create: true }); | |
} | |
while (true) { | |
const SPACE_HOST = Deno.env.get('SPACE_HOST'); | |
SPACE_HOST && await fetch(`https://${SPACE_HOST}`); | |
try { | |
await main(); | |
} catch (e) { | |
console.error(e); | |
delay(10000); | |
} | |
} | |