Spaces:
Running
Running
Create script.js
Browse files- modules/templates/script.js +89 -0
modules/templates/script.js
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.addEventListener('DOMContentLoaded', function () {
|
2 |
+
const form = document.querySelector('#translationForm');
|
3 |
+
const targetLangSelect = document.querySelector('#target_lang');
|
4 |
+
const sourceLangSelect = document.querySelector('#source_lang');
|
5 |
+
const userInput = document.querySelector('#userinput');
|
6 |
+
const outputText = document.querySelector('#output');
|
7 |
+
|
8 |
+
// Define your languages and codes
|
9 |
+
const languages = [
|
10 |
+
{ name: 'Swahili', code: 'swh_Latn' },
|
11 |
+
{ name: 'Kikuyu', code: 'kik_Latn' },
|
12 |
+
{ name: 'Spanish', code: 'spa_Latn' },
|
13 |
+
{ name: 'French', code: 'fra_Latn' },
|
14 |
+
{ name: 'Amharic', code: 'amh_Ethi' },
|
15 |
+
{ name: 'English', code: 'eng_Latn' },
|
16 |
+
// Add your other languages and codes here...
|
17 |
+
];
|
18 |
+
|
19 |
+
// Function to populate the dropdown options
|
20 |
+
function populateDropdown(select, options) {
|
21 |
+
options.forEach((option) => {
|
22 |
+
const optionElem = document.createElement('option');
|
23 |
+
optionElem.value = option.code;
|
24 |
+
optionElem.text = option.name;
|
25 |
+
select.add(optionElem);
|
26 |
+
});
|
27 |
+
}
|
28 |
+
|
29 |
+
// Call the function to populate dropdown options
|
30 |
+
populateDropdown(targetLangSelect, languages);
|
31 |
+
populateDropdown(sourceLangSelect, [{ name: 'Auto Detect', code: 'auto' }, ...languages]);
|
32 |
+
|
33 |
+
form.addEventListener('submit', async (e) => {
|
34 |
+
e.preventDefault();
|
35 |
+
|
36 |
+
const targetLang = targetLangSelect.value;
|
37 |
+
const sourceLang = sourceLangSelect.value;
|
38 |
+
|
39 |
+
try {
|
40 |
+
// Change placeholder text to "Translating..."
|
41 |
+
outputText.placeholder = 'Translating...';
|
42 |
+
|
43 |
+
let sourceLanguage;
|
44 |
+
|
45 |
+
// Check if Auto Detect is selected for source language
|
46 |
+
if (sourceLang === 'auto') {
|
47 |
+
const detectionResponse = await fetch('https://lewiskimaru-helloworld.hf.space/translate_detect/', {
|
48 |
+
method: 'POST',
|
49 |
+
headers: {
|
50 |
+
'Content-Type': 'application/json'
|
51 |
+
},
|
52 |
+
body: JSON.stringify({
|
53 |
+
userinput: userInput.value,
|
54 |
+
target_lang: targetLang
|
55 |
+
})
|
56 |
+
});
|
57 |
+
|
58 |
+
const detectionData = await detectionResponse.json();
|
59 |
+
sourceLanguage = detectionData.source_language;
|
60 |
+
} else {
|
61 |
+
sourceLanguage = sourceLang;
|
62 |
+
}
|
63 |
+
|
64 |
+
// Check if Auto Detect is selected for target language
|
65 |
+
const targetLanguage = targetLang === 'auto' ? 'eng_Latn' : targetLang;
|
66 |
+
|
67 |
+
const translationResponse = await fetch('https://lewiskimaru-helloworld.hf.space/translate_enter/', {
|
68 |
+
method: 'POST',
|
69 |
+
headers: {
|
70 |
+
'Content-Type': 'application/json'
|
71 |
+
},
|
72 |
+
body: JSON.stringify({
|
73 |
+
userinput: userInput.value,
|
74 |
+
target_lang: targetLanguage,
|
75 |
+
source_lang: sourceLanguage
|
76 |
+
})
|
77 |
+
});
|
78 |
+
|
79 |
+
const translatedText = (await translationResponse.json()).translated_text;
|
80 |
+
|
81 |
+
// Update placeholder with the translated text
|
82 |
+
outputText.placeholder = translatedText;
|
83 |
+
|
84 |
+
} catch (error) {
|
85 |
+
console.error(error);
|
86 |
+
outputText.placeholder = 'An error occurred. Please try again.';
|
87 |
+
}
|
88 |
+
});
|
89 |
+
});
|