File size: 2,693 Bytes
06a4efd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// On loaded, add event listeners
window.document.addEventListener("DOMContentLoaded", () => {
  const form = window.document.querySelector("form");
  const spaceName = window.document.querySelector("#space-name");
  const buildCommand = window.document.querySelector("#build-command");
  const hfToken = window.document.querySelector("#hf-token");
  const outputPath = window.document.querySelector("#output-path");

  const output = window.document.querySelector("#output");
  const logOuput = window.document.querySelector("#log-output");
  const eventOutput = window.document.querySelector("#event-output");

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

    output.textContent = "";
    logOuput.textContent = "";
    eventOutput.textContent = "";

    const data = {
      environment: {
        HF_SPACE_NAME: spaceName.value,
        BUILD_COMMAND: buildCommand.value,
        HF_TOKEN: hfToken.value,
        // Remove filename
        OUTPUT_PATH: outputPath.value.replace(/\/[^/]*$/, ""),
      },
      arguments: [],
      command: ["/app/build.sh"],
      flavor: "cpu-basic"
    };

    const tokenOwnerResp = await fetch("https://huggingface.co/api/whoami-v2", {
      method: "GET",
      headers: {
        Authorization: `Bearer ${data.hfToken}`,
      },
    });

    if (!tokenOwnerResp.ok) {
      output.textContent = "Invalid token";
      return;
    }

    const tokenOwer = (await tokenOwnerResp.json())["name"];

    if (!tokenOwer) {
      output.textContent = "Invalid token";
      return;
    }

    output.textContent += `Token owner: ${tokenOwer}\n`;

    const jobResponse = await fetch(`https://huggingface.co/api/jobs/${tokenOwer}`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${data.hfToken}`,
      },
      body: JSON.stringify(data),
    });

    const result = await jobResponse.json();

    if (!jobResponse.ok) {
      output.textContent = `Error: ${result.error}`;
      return;
    }

    result.textContent += JSON.stringify(result, null, 2);

    const jobId = result["id"];

    const eventSource = new EventSource(`https://huggingface.co/api/jobs/${jobId}/sse`, {
      headers: {
        Authorization: `Bearer ${data.hfToken}`,
      },
    });

    eventSource.onmessage = (event) => {
      eventOutput.textContent += `${event.data}\n`;
    };

    const logSource  = new EventSource(`https://huggingface.co/api/jobs/${jobId}/logs-stream`, {
      headers: {
        Authorization: `Bearer ${data.hfToken}`,
      },
    });

    logSource.onmessage = (event) => {
      logOuput.textContent += `${event.data}\n`;
    };
  });
});