File size: 1,375 Bytes
0731409
 
abf9047
 
 
0731409
abf9047
 
 
 
 
 
0731409
abf9047
 
 
 
 
0731409
 
 
abf9047
0731409
abf9047
 
 
 
 
 
 
 
 
0731409
abf9047
 
0731409
 
abf9047
 
 
 
 
 
 
 
 
 
 
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
if (document.location.search.includes("dark-theme=true")) {
  document.body.classList.add("dark-theme");
}

const textToImage = async (text) => {
  const inferenceResponse = await fetch(`biggan_infer?input=${text}`);
  const inferenceBlob = await inferenceResponse.blob();

  return URL.createObjectURL(inferenceBlob);
};

const translateText = async (text) => {
  const inferResponse = await fetch(`t5_infer?input=${text}`);
  const inferJson = await inferResponse.json();

  return inferJson.output;
};

const imageGenSelect = document.getElementById("image-gen-input");
const imageGenImage = document.querySelector(".image-gen-output");
const textGenForm = document.querySelector(".text-gen-form");

imageGenSelect.addEventListener("change", async (event) => {
  const value = event.target.value;

  try {
    imageGenImage.src = await textToImage(value);
  } catch (err) {
    console.error(err);
  }
});

textGenForm.addEventListener("submit", async (event) => {
  event.preventDefault();

  const textGenInput = document.getElementById("text-gen-input");
  const textGenParagraph = document.querySelector(".text-gen-output");

  try {
    textGenParagraph.textContent = await translateText(textGenInput.value);
  } catch (err) {
    console.error(err);
  }
});

textToImage(imageGenSelect.value)
  .then((image) => (imageGenImage.src = image))
  .catch(console.error);