File size: 2,813 Bytes
c144f47
 
06a4efd
 
 
0fe50fd
 
 
 
 
06a4efd
0fe50fd
 
 
06a4efd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687447e
 
06a4efd
 
 
 
 
1ce79db
06a4efd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce79db
06a4efd
 
 
 
 
 
 
124aade
06a4efd
 
 
124aade
06a4efd
124aade
06a4efd
55949e2
c144f47
 
 
06a4efd
1ce79db
06a4efd
 
 
55949e2
c144f47
 
 
06a4efd
1ce79db
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
96
import { fetchEventSource } from '@microsoft/fetch-event-source';

// On loaded, add event listeners
window.document.addEventListener("DOMContentLoaded", () => {
  const form = window.document.querySelector("form");
  form.addEventListener("submit", async (event) => {
    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");

    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",
      spaceId: "huggingface/space-build",
    };

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

    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 ${hfToken.value}`,
      },
      body: JSON.stringify(data),
    });

    const result = await jobResponse.json();

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

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

    const jobId = result["metadata"]["job_id"];

    fetchEventSource(`https://huggingface.co/api/jobs/${tokenOwer}/${jobId}/sse`, {
      onmessage: (event) => {
        eventOutput.textContent += `${event.data}\n`;
      },
      headers: {
        Authorization: `Bearer ${hfToken.value}`,
      },
    });

    fetchEventSource(`https://huggingface.co/api/jobs/${tokenOwer}/${jobId}/logs-stream`, {
      onmessage: (event) => {
        logOuput.textContent += `${event.data}\n`;
      },
      headers: {
        Authorization: `Bearer ${hfToken.value}`,
      },
    });
  });
});