File size: 4,691 Bytes
0d3c10a
46abd15
0d3c10a
 
46abd15
0d3c10a
 
46abd15
0d3c10a
 
fe724e6
 
 
 
 
 
 
 
 
 
 
 
 
0d3c10a
 
fe724e6
0d3c10a
 
 
 
 
7a26dd9
46abd15
 
7a26dd9
0d3c10a
 
fe724e6
 
0d3c10a
7a26dd9
 
 
27f4327
3f7b6a8
27f4327
3f7b6a8
7a26dd9
 
46abd15
7a26dd9
 
 
46abd15
7a26dd9
 
 
 
 
 
 
 
3f7b6a8
 
 
 
 
fe724e6
3f7b6a8
 
 
 
 
 
46abd15
27f4327
 
46abd15
27f4327
3f7b6a8
 
 
 
 
 
 
46abd15
3f7b6a8
 
 
fe724e6
 
3f7b6a8
 
 
fe724e6
 
3f7b6a8
 
 
7a26dd9
3f7b6a8
7a26dd9
 
27f4327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46abd15
27f4327
 
 
 
 
 
 
 
46abd15
27f4327
 
 
 
 
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
const textGenForm = document.querySelector('.text-gen-form');
let docsList = [];

const embedText = async (text) => {
    const inferResponse = await fetch(`parsing?input=${text}`);
    const inferJson = await inferResponse.json();

    return inferJson;
};

const spinnerOverlay = document.createElement('div');
spinnerOverlay.classList.add('spinner-overlay');
spinnerOverlay.innerHTML = '<div class="spinner"></div>';
document.body.appendChild(spinnerOverlay);

const showSpinner = () => {
    spinnerOverlay.style.display = 'flex';
};

const hideSpinner = () => {
    spinnerOverlay.style.display = 'none';
};

textGenForm.addEventListener('submit', async (event) => {
    event.preventDefault();
    showSpinner();

    const textGenInput = document.getElementById('text-gen-input');
    const textGenParagraph = document.querySelector('.text-gen-output');

    try {
        const embeddings = await embedText(textGenInput.value);
        docsList = embeddings; // Store embeddings in the variable
        textGenParagraph.textContent = JSON.stringify(docsList);
        updateDownloadButtonState(); // Update button state
    } catch (err) {
        console.error(err);
    } finally {
        hideSpinner();
    }
});

const downloadButton = document.getElementById('download-embeddings');

const uploadButton = document.getElementById('upload-embeddings');

const fileInput = document.getElementById('file-input');

const updateDownloadButtonState = () => {
    downloadButton.disabled = docsList.length === 0;
};

const downloadEmbeddings = () => {
    const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(docsList));
    const downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href", dataStr);
    downloadAnchorNode.setAttribute("download", "embeddings.json");
    document.body.appendChild(downloadAnchorNode);
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
};

const uploadEmbeddings = () => {
    fileInput.click();
};

fileInput.addEventListener('change', async (event) => {
    showSpinner();
    const file = event.target.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = async (e) => {
            try {
                const embeddings = JSON.parse(e.target.result);
                docsList = embeddings; // Store uploaded embeddings in the variable

                const textGenParagraph = document.querySelector('.text-gen-output');
                textGenParagraph.textContent = JSON.stringify(docsList);

                updateDownloadButtonState(); // Update button state
                // Optionally, you can send the embeddings to the server
                await fetch('/receive-embeddings', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ embeddings: docsList })
                });
            } catch (err) {
                console.error('Error reading or parsing the file', err);
            } finally {
                hideSpinner();
            }
        };
        reader.readAsText(file);
    } else {
        hideSpinner();
    }
});

downloadButton.addEventListener('click', downloadEmbeddings);
uploadButton.addEventListener('click', uploadEmbeddings);

// Initialize button state
updateDownloadButtonState();

const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const chatBox = document.getElementById('chat-box');

const sendMessage = async (message) => {
    const response = await fetch('/chat', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ message })
    });
    const data = await response.json();
    return data.reply;
};

chatForm.addEventListener('submit', async (event) => {
    event.preventDefault();
    const userMessage = chatInput.value;
    if (userMessage.trim() === '') return;

    const userMessageElement = document.createElement('div');
    userMessageElement.textContent = `You: ${userMessage}`;
    userMessageElement.classList.add('user-message');
    chatBox.appendChild(userMessageElement);

    chatInput.value = '';

    try {
        const reply = await sendMessage(userMessage);
        const replyMessageElement = document.createElement('div');
        replyMessageElement.textContent = `Bot: ${reply}`;
        replyMessageElement.classList.add('bot-reply');
        chatBox.appendChild(replyMessageElement);
    } catch (err) {
        console.error('Error sending message:', err);
    }
});