-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
220 lines (182 loc) · 8.22 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
document.getElementById('contribute-form').addEventListener('submit', function(event) {
event.preventDefault();
const title = document.getElementById('title').value;
const description = document.getElementById('description').value;
const file = document.getElementById('file').files[0];
const author = document.getElementById('author').value;
const language = document.getElementById('language').value;
const archiveEntry = document.createElement('div');
archiveEntry.innerHTML = `
<h3>${title}</h3>
<p>${description}</p>
<p><strong>File:</strong> ${file.name}</p>
<p><strong>Submitted by:</strong> ${author}</p>
<p><strong>Language:</strong> ${language}</p>
`;
document.getElementById('archive-list').appendChild(archiveEntry);
updateVisualization();
document.getElementById('contribute-form').reset();
});
function updateVisualization() {
const labels = ['Language 1', 'Language 2', 'Language 3'];
const data = [Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100)];
const ctx = document.getElementById('contributionChart').getContext('2d');
const contributionChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Number of Contributions',
data: data,
backgroundColor: ['#4CAF50', '#81C784', '#FFEB3B'],
borderColor: '#333',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
document.getElementById('premium-button').addEventListener('click', function() {
alert('Thank you for upgrading to Premium!');
});
const sections = document.querySelectorAll('section');
sections.forEach(section => {
section.classList.add('fade-in');
});
const aiResponses = {
"what is noise pollution?": "Noise pollution is unwanted or harmful sound in the environment.",
"how does it affect indigenous cultures?": "It disrupts traditional practices, communication, and the natural sounds of the environment.",
"how can we preserve indigenous languages?": "We can document them using technology, teaching them in schools, and creating digital archives.",
"what are the benefits of preserving culture?": "Preserving culture helps maintain diversity, fosters community identity, and contributes to knowledge and history.",
"what can technology do for indigenous preservation?": "Technology can create platforms for documentation, sharing stories, and connecting communities."
};
document.getElementById('ask-button').addEventListener('click', () => {
const userInput = document.getElementById('user-input').value.toLowerCase();
const response = aiResponses[userInput] || "I'm sorry, I don't have an answer for that. Try asking something else!";
document.getElementById('ai-response').innerText = response;
document.getElementById('user-input').value = '';
});
function createMessageElement(text, isUserMessage) {
const messageDiv = document.createElement('div');
messageDiv.className = isUserMessage ? 'user-message' : 'ai-message';
messageDiv.innerText = text;
return messageDiv;
}
async function fetchAIResponse(userInput) {
const response = await fetch('https://api.example.com/getResponse', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer d4b9363a10164f4787b54a91a03c2dab'
},
body: JSON.stringify({ query: userInput })
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data.answer;
}
document.getElementById('ask-button').addEventListener('click', async () => {
const userInput = document.getElementById('user-input').value.trim();
if (userInput) {
const userMessageElement = createMessageElement(userInput, true);
document.getElementById('ai-response').appendChild(userMessageElement);
try {
const aiResponse = await fetchAIResponse(userInput);
setTimeout(() => {
const aiMessageElement = createMessageElement(aiResponse, false);
document.getElementById('ai-response').appendChild(aiMessageElement);
}, 500);
} catch (error) {
console.error('Error fetching AI response:', error);
const errorMessageElement = createMessageElement("There was an error retrieving the answer. Please try again.", false);
document.getElementById('ai-response').appendChild(errorMessageElement);
}
document.getElementById('user-input').value = '';
}
});
document.getElementById('contribute-form').addEventListener('submit', function(event) {
event.preventDefault();
const title = document.getElementById('title').value;
const description = document.getElementById('description').value;
const file = document.getElementById('file').files[0];
const author = document.getElementById('author').value;
const language = document.getElementById('language').value;
const archiveEntry = document.createElement('div');
archiveEntry.innerHTML = `
<h3>${title}</h3>
<p>${description}</p>
<p><strong>File:</strong> ${file.name}</p>
<p><strong>Submitted by:</strong> ${author}</p>
<p><strong>Language:</strong> ${language}</p>
`;
document.getElementById('archive-list').appendChild(archiveEntry);
updateVisualization();
document.getElementById('contribute-form').reset();
});
function updateVisualization() {
const labels = ['Language 1', 'Language 2', 'Language 3'];
const data = [Math.floor(Math.random() * 100), Math.floor(Math.random() * 100), Math.floor(Math.random() * 100)];
const ctx = document.getElementById('contributionChart').getContext('2d');
const contributionChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Number of Contributions',
data: data,
backgroundColor: ['#4CAF50', '#81C784', '#FFEB3B'],
borderColor: '#333',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
async function fetchAIResponse(userInput) {
const response = await fetch('https://api.example.com/getResponse', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer d4b9363a10164f4787b54a91a03c2dab'
},
body: JSON.stringify({ query: userInput })
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
return data.answer;
}
document.getElementById('ask-button').addEventListener('click', async () => {
const userInput = document.getElementById('user-input').value.trim();
if (userInput) {
const userMessageElement = createMessageElement(userInput, true);
document.getElementById('ai-response').appendChild(userMessageElement);
try {
const aiResponse = await fetchAIResponse(userInput);
setTimeout(() => {
const aiMessageElement = createMessageElement(aiResponse, false);
document.getElementById('ai-response').appendChild(aiMessageElement);
}, 500);
} catch (error) {
console.error('Error fetching AI response:', error);
const errorMessageElement = createMessageElement("There was an error retrieving the answer. Please try again.", false);
document.getElementById('ai-response').appendChild(errorMessageElement);
}
document.getElementById('user-input').value = '';
}
});