forked from Big-Data-Brawlers/mazingira-ai-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (126 loc) · 6.09 KB
/
script.js
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
const authToken = window.AUTH_TOKEN;
query({"inputs": `Start model`}).then((response) => {
$(".splash-screen").fadeOut("slow");
});
$(document).ready(function () {
// Event handler for the send button
$("#sendButton").click(function (event) {
event.preventDefault(); // Prevent form submission
var userInput = $("#userInput").val();
if (userInput) {
hideErrorAlert();
// Display user message
appendMessage(userInput, "user-message", true);
$("#userInput").val('');
// Disable input and button during message processing
$("#userInput, #sendButton").prop("disabled", true);
$("#loading-message").removeClass("hidden");
// Simulate API call with a timeout (modify this part for your actual API call)
var requestTimeout = setTimeout(function () {
// Display a timeout message to the user
var timeoutMessage = "The request is taking too long. Please try again later.";
appendMessage(timeoutMessage, "timeout-message", false);
// Add buttons for regeneration
var regenerateButtons = $("<div>").addClass("flex justify-center mt-2");
var yesButton = $("<button>").text("Yes").addClass("bg-green-500 text-white px-4 py-2 rounded mr-4 hover:bg-green-600");
var noButton = $("<button>").text("No").addClass("bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600");
regenerateButtons.append(yesButton, noButton);
$("#chat-container").append(regenerateButtons);
yesButton.click(function () {
// Disable input and button during message processing
$("#userInput, #sendButton").prop("disabled", true);
$("#loading-message").removeClass("hidden");
// Regenerate the response using the query method
query({ "inputs": `[I]: ${userInput.toLowerCase()}` }).then((response) => {
// Clear the timeout since the response arrived within the expected time
clearTimeout(requestTimeout);
// Display AI message with AI avatar
appendMessage(response, "ai-message", false);
// Enable the input field and send button after generation
$("#userInput, #sendButton").prop("disabled", false);
// Hide loading message
$("#loading-message").addClass("hidden");
// Clear the input field
$("#userInput").val('');
});
});
noButton.click(function () {
// Clear the input field
$("#userInput").val('');
});
// Enable the input field and send button
$("#userInput, #sendButton").prop("disabled", false);
// Hide loading message
$("#loading-message").addClass("hidden");
// Clear the input field
$("#userInput").val('');
}, 10000); // 10 seconds timeout (adjust as needed)
// Make the API call (replace with your actual API call)
query({ "inputs": `[I]: ${userInput.toLowerCase()}` }).then((response) => {
// Clear the timeout since the response arrived within the expected time
clearTimeout(requestTimeout);
// Display AI message with AI avatar
appendMessage(response, "ai-message", false);
// Enable the input field and send button after generation
$("#userInput, #sendButton").prop("disabled", false);
// Hide loading message
$("#loading-message").addClass("hidden");
// Clear the input field
$("#userInput").val('');
});
} else {
showErrorAlert();
}
});
});
// Call the Hugging Face API here and get the response
async function query(data) {
const response = await fetch(
"https://api-inference.huggingface.co/models/Danroy/mazingira-gpt",
{
headers: { Authorization: authToken},
method: "POST",
body: JSON.stringify(data),
}
);
const result = await response.json();
// Extract the 'generated_text' field
var text = result[0]['generated_text'];
// Use a regular expression to find the '[O]: ' and everything that follows
var match = text.match(/\[O\]: ([^\[]+)/);
// Append the result to the 'result' div
if (match) {
var cleanedText = match[1].replace(/\\\"/g, '');
return capitalizeSentences(cleanedText);
}
return 'Encountered and error.';
}
function capitalizeSentences(text) {
return text.replace(/(^\s*\w|[\.\!\?]\s*\w)/g, function(c) {
return c.toUpperCase();
});
}
function showErrorAlert() {
$("#inputError").removeClass('hidden');
$("#userInput").removeClass('border-green-300').addClass('border-red-400');
}
function hideErrorAlert() {
$("#inputError").addClass('hidden');
$("#userInput").addClass('border-green-300').removeClass('border-red-400');
}
function appendMessage(text, className, isUserMessage) {
if (className == 'timeout-message') {
var messageDiv = $("<div>").addClass(className);
messageDiv.text(text);
$("#chat-container").append(messageDiv);
// Scroll to the bottom of the chat container
$("#chat-container").scrollTop($("#chat-container")[0].scrollHeight);
}
else {
var messageDiv = $("<div>").addClass("rounded-lg p-3 shadow my-2 max-w-xl " + (isUserMessage ? "ml-auto bg-white" : "mr-auto bg-blue-100"));
messageDiv.text(text);
$("#chat-container").append(messageDiv);
// Scroll to the bottom of the chat container
$("#chat-container").scrollTop($("#chat-container")[0].scrollHeight);
}
}