import { assertExists } from '@std/assert'; import { emptyDirSync, ensureDirSync } from '@std/fs'; import { delay } from '@std/async'; import { randomIntegerBetween } from '@std/random'; import * as hub from '@huggingface/hub'; import { generateImage } from './ai.ts'; import { getDirectoryStructureString, getFilesInDirectory, randomString } from './utils.ts'; import { executeWebhook } from './discord.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); const WEBHOOK_URL = Deno.env.get('WEBHOOK_URL'); 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 }; console.info('uploading...'); const result = await hub.uploadFiles({ repo, accessToken: HUGGINGFACE_ACCESS_TOKEN, files: files.map((file) => ({ path: `images2/${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(10, 100) }, () => randomString(randomIntegerBetween(1, 100))).join(' '); const prompt = Array.from({ length: 1000 }, () => String.fromCharCode(randomIntegerBetween(32, 126))).join(''); const imgArrayBuffer = await generateImage('black-forest-labs/FLUX.1-schnell', { inputs: prompt, parameters: { width: 1280, height: 1280, guidance_scale: 1, num_inference_steps: 4, }, }); const now = Date.now(); Deno.writeFileSync(`./images/${now}.jpg`, new Uint8Array(imgArrayBuffer), { create: true }); try { WEBHOOK_URL && await executeWebhook(WEBHOOK_URL, new Blob([imgArrayBuffer]), `${now}.jpg`); } catch (e) { console.error(e); } } // 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); // await delay(10000); // } // }