File size: 3,394 Bytes
8a3f25c
400caeb
8a3f25c
400caeb
 
8a3f25c
400caeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3b1912
400caeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cabb24a
400caeb
 
 
 
 
 
 
adc5884
 
400caeb
 
 
 
 
cabb24a
400caeb
 
 
617c204
 
656a72b
 
60b42a9
400caeb
 
 
 
 
 
 
f3b1912
400caeb
 
 
f3b1912
400caeb
 
 
 
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

const translateText = async (text) => {
    console.log(text)
    const inferResponse = await fetch(`infer_t5?input=${text}`);
    const inferJson = await inferResponse.json();
    console.log(inferJson.output)
    return inferJson.output;
  };


function generatePrompterAssistantText(inputString) {
    // Split the input string into an array of sentences
    const sentences = inputString.split('<|endoftext|>');
  
    // Initialize arrays for prompter and assistant text
    let prompterText = [];
    let assistantText = [];
  
    // Loop through each sentence and add it to the prompter or assistant text array
    for (let i = 0; i < sentences.length; i++) {
      // Check if the sentence contains the <prompter> tag
      if (sentences[i].includes('<|prompter|>')) {
        // Extract the text within the <prompter> tags and add it to the prompter text array
        const prompterSentence = sentences[i].replace(/<\|prompter\|>/g, '');
        prompterText.push(prompterSentence);
      } else if (sentences[i].includes('<|assistant|>')) {
        const assistantSentence = sentences[i].replace(/<\|assistant\|>/g, '');
        // Add the sentence to the assistant text array
        assistantText.push(assistantSentence);
      }
    }
  
    // Return the prompter and assistant text arrays
    return [prompterText, assistantText];
  }

const submitButton = document.querySelector('#submit')
const outPutElement = document.querySelector('#output')
const inputElement = document.querySelector('input')
const historyElement = document.querySelector('.history')
const buttonElement = document.querySelector('button')


function changeInput(value)
{
    console.log(value)
    const inputElement = document.querySelector('input')
    inputElement.value = value
}
async function getMessage(){
    //console.log("input value "+ inputElement.value)
    const options = {
        method: "POST",
        headers: {
            Authorization: `Bearer ${API_KEY}`,
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            inputs: "<|prompter|>" + inputElement.value + "<|endoftext|><|assistant|>",
            parameters: {"max_new_tokens": 200, "temperature": 0.9}
        })
    } 
    try{
        const response = await fetch("https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5", options);
        const data = await response.json()
        //console.log(data[0].generated_text)

        if(inputElement.value && data && data[0].generated_text){
            const [prompterText, assistantText] = generatePrompterAssistantText(data[0].generated_text);
            // const en_text_ml = "English: "  + assistantText[0] + " Malayalam:";
            // console.log(en_text_ml)
            //console.log(prompterText)
            //console.log(assistantText)
            outPutElement.textContent = await translateText(assistantText[0]);
            const pElement = document.createElement('p')
            pElement.textContent = inputElement.value
            pElement.addEventListener('click', () => changeInput(pElement.textContent))
            historyElement.append(pElement)
        }
    } catch(error) {
        console.log(error)
    }
}

submitButton.addEventListener('click', getMessage)

function clearInput(){
    inputElement.value = ''
}
buttonElement.addEventListener('click', clearInput)