File size: 1,167 Bytes
18fab46
 
 
 
 
 
5104a50
18fab46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
async function generateInitialImage() {
  const prompt = document.getElementById('prompt').value;
  const seed = parseInt(document.getElementById('seed').value);
  state.seed = seed;

  try {
    const response = await fetch("https://devinendorphin-mcmc-generator.hf.space/⁰run/predict", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ data: [prompt, seed] })
    });

    const result = await response.json();
    const imageDataUrl = result.data[0];

    const img = new Image();
    img.src = imageDataUrl;
    await new Promise(res => img.onload = res);

    const tempCanvas = document.createElement('canvas');
    tempCanvas.width = 512;
    tempCanvas.height = 512;
    const ctx2 = tempCanvas.getContext('2d');
    ctx2.drawImage(img, 0, 0, 512, 512);

    state.baseImage = tempCanvas;
    ctx.clearRect(0, 0, 512, 512);
    ctx.drawImage(tempCanvas, 0, 0);
    const imageData = ctx.getImageData(0, 0, 512, 512);
    state.currentEnergy = calculateEnergy(imageData);
    log('Image from Hugging Face Space loaded.');
  } catch (e) {
    log('Error using Hugging Face Space: ' + e.message);
  }
}