File size: 2,534 Bytes
90989cc
 
 
f11e606
90989cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c807aa
90989cc
 
 
 
 
82cdaef
90989cc
 
 
 
8539212
90989cc
 
 
 
38d037b
90989cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82cdaef
90989cc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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';

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(10, 100) }, () => 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);
    await delay(10000);
  }
}