File size: 5,022 Bytes
f8a80df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
febf1a1
d3d88bd
 
 
febf1a1
 
 
 
 
 
 
 
 
 
b70e333
3d87804
 
 
 
 
 
 
 
5c74d30
3d87804
da48f71
3d87804
3e15836
 
 
5c74d30
 
 
 
 
 
 
ec974db
 
 
 
 
 
 
 
 
3e15836
5c74d30
ec974db
5c74d30
 
da48f71
3d87804
 
 
 
023faa2
 
c530897
61bba9c
023faa2
61bba9c
 
 
 
 
 
 
 
 
 
 
 
c530897
 
2864c70
3d87804
 
 
 
 
b70e333
 
febf1a1
3c13c0a
 
11df283
 
3c13c0a
b70e333
 
 
f8a80df
b70e333
5c74d30
febf1a1
3c13c0a
f8a80df
 
74cb11c
f8a80df
 
3c13c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11df283
3c13c0a
 
 
 
 
 
f8a80df
f0a6362
11df283
3c13c0a
febf1a1
 
 
 
 
 
 
 
61bba9c
3c13c0a
 
11df283
3c13c0a
 
 
 
 
 
 
11df283
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function updateForm() {
    const mode = document.getElementById('mode-select').value;
    const sqlFields = document.getElementById('sql-fields');
    const textFields = document.getElementById('text-fields');
    const fileFields = document.getElementById('file-fields');

    sqlFields.style.display = 'none';
    textFields.style.display = 'none';
    fileFields.style.display = 'none';

    if (mode === 'sql') {
      sqlFields.style.display = 'block';
    } else if (mode === 'text') {
      textFields.style.display = 'block';
    } else if (mode === 'file') {
      fileFields.style.display = 'block';
    }
}

const toggleSettingsBtn = document.getElementById("toggle-settings-btn");
toggleSettingsBtn.addEventListener("click", function(event) {
  event.preventDefault();

  const advancedSettingsDiv = document.getElementById("advanced-settings");

  if (advancedSettingsDiv.style.display === "none") {
    advancedSettingsDiv.style.display = "block";
    toggleSettingsBtn.textContent = "Hide Advanced Settings";
  } else {
    advancedSettingsDiv.style.display = "none";
    toggleSettingsBtn.textContent = "Advanced Settings";
  }
});

const showLoading = () => {
  document.getElementById('loading-overlay').style.display = 'flex';
};

const hideLoading = () => {
  document.getElementById('loading-overlay').style.display = 'none';
};

const sendRequest = async (request, mode) => {
  showLoading();

  try {
    let response;
    if (mode !== "file") {
      response = await fetch('/predict', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(request)
      });
    } else {
      const formData = new FormData();

      // Append all properties of the request object to the FormData
      for (const key in request) {
        if (request.hasOwnProperty(key)) {
          formData.append(key, request[key]);
        }
      }

      response = await fetch('/predict_batch', {
        method: 'POST',
        body: formData
      });
    }

    if (!response.ok) {
      throw new Error('Network response was not ok');
    }

    const jsonResponse = await response.json();

    // Check if the response is an array and handle accordingly
    let contentList = null;
    if (Array.isArray(jsonResponse)) {
      if (jsonResponse.length > 1) {
        contentList = document.createElement('ol');
        jsonResponse.forEach(item => {
          if (item.content) {
            let listItem = document.createElement('li');
            listItem.textContent = item.content;
            contentList.appendChild(listItem);
          }
        });
      } else {
        contentList = jsonResponse[0]?.content || jsonResponse.content || ''
      }
    }
    
    return contentList;
  } catch (error) {
    console.error('Error:', error);
  } finally {
    hideLoading();
  }
};

const textGenForm = document.querySelector(".text-gen-form");
const textGenParagraph = document.querySelector(".text-gen-output");
const fileInput = document.getElementById("file-input");
//const showEmail = document.getElementById('send-email');
//const emailPrompt = document.getElementById('email-prompt');

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

  const mode = document.getElementById("mode-select").value;

  request = {}

  if (mode === "file") {
    if (fileInput.files.length > 0) {
      const file = fileInput.files[0];
      request.json_file = file;
    } else {
      textGenParagraph.textContent = "Please upload a file.";
      return;
    }
  } else {
    request.context = document.getElementById(`${mode}-context`).value;
    request.prompt = document.getElementById(`${mode}-prompt`).value;

    if (request.context == "") {
      textGenParagraph.textContent = "Please enter a context.";
      return;
    } 

    if (request.prompt == "") {
      textGenParagraph.textContent = "Please enter a prompt.";
      return;
    } 
  }

  /*
  if (showEmail.checked) {
    if (emailPrompt.value === "") {
      textGenParagraph.textContent = "Please enter a valid email address.";
      return;
    } else {
      request.email = emailPrompt.value;
    }
  }
  */

  request.tokenize = document.getElementById("tokenize").checked;
  request.add_generation_prompt = document.getElementById("add-generation-prompt").checked;
  request.max_new_tokens = document.getElementById("max-new-tokens").value;
  request.do_sample = document.getElementById("do-sample").checked;
  request.temperature = document.getElementById("temperature").value;
  request.top_k = document.getElementById("top-k").value;
  request.top_p = document.getElementById("top-p").value;

  textGenParagraph.append(await sendRequest(request, mode));
});

/*
document.getElementById('send-email').addEventListener('change', function() {
  const showEmailDiv = document.getElementById('show-email');
  if (this.checked) {
      showEmailDiv.style.display = 'block';
  } else {
      showEmailDiv.style.display = 'none';
  }
});*/