File size: 3,011 Bytes
d88e0c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function () {
  const form = document.querySelector('#translationForm');
  const targetLangSelect = document.querySelector('#target_lang');
  const sourceLangSelect = document.querySelector('#source_lang');
  const userInput = document.querySelector('#userinput');
  const outputText = document.querySelector('#output');

  // Define your languages and codes
  const languages = [
    { name: 'Swahili', code: 'swh_Latn' },
    { name: 'Kikuyu', code: 'kik_Latn' },
    { name: 'Spanish', code: 'spa_Latn' },
    { name: 'French', code: 'fra_Latn' },
    { name: 'Amharic', code: 'amh_Ethi' },
    { name: 'English', code: 'eng_Latn' },
    // Add your other languages and codes here...
  ];

  // Function to populate the dropdown options
  function populateDropdown(select, options) {
    options.forEach((option) => {
      const optionElem = document.createElement('option');
      optionElem.value = option.code;
      optionElem.text = option.name;
      select.add(optionElem);
    });
  }

  // Call the function to populate dropdown options
  populateDropdown(targetLangSelect, languages);
  populateDropdown(sourceLangSelect, [{ name: 'Auto Detect', code: 'auto' }, ...languages]);

  form.addEventListener('submit', async (e) => {
    e.preventDefault();

    const targetLang = targetLangSelect.value;
    const sourceLang = sourceLangSelect.value;

    try {
      // Change placeholder text to "Translating..."
      outputText.placeholder = 'Translating...';

      let sourceLanguage;

      // Check if Auto Detect is selected for source language
      if (sourceLang === 'auto') {
        const detectionResponse = await fetch('https://lewiskimaru-helloworld.hf.space/translate_detect/', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            userinput: userInput.value,
            target_lang: targetLang
          })
        });

        const detectionData = await detectionResponse.json();
        sourceLanguage = detectionData.source_language;
      } else {
        sourceLanguage = sourceLang;
      }

      // Check if Auto Detect is selected for target language
      const targetLanguage = targetLang === 'auto' ? 'eng_Latn' : targetLang;

      const translationResponse = await fetch('https://lewiskimaru-helloworld.hf.space/translate_enter/', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          userinput: userInput.value,
          target_lang: targetLanguage,
          source_lang: sourceLanguage
        })
      });

      const translatedText = (await translationResponse.json()).translated_text;

      // Update placeholder with the translated text
      outputText.placeholder = translatedText;

    } catch (error) {
      console.error(error);
      outputText.placeholder = 'An error occurred. Please try again.';
    }
  });
});