File size: 2,521 Bytes
5997ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73241e7
5997ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const input = document.getElementById('input_prompt');
const button = document.getElementById('submit_button');
const messagesDiv = document.getElementById('messages');

let FILE_ID = ""
let curr_chat_id = chat_id

const file = document.getElementById("pdf_upload")
function update_file_status(status, error=null) {
    if (status) {
    appendMessage(`Uploaded ${file.files[0].name}`, 'file_uploaded')
    }
    else {
    window.alert(error)
    }
}
file.addEventListener("change", async () => {
        if (!file.files.length) {
        window.alert("No files selected")
        return ;
        }

        if (file.files.length > 1) {
        window.alert("You can upload only a single file at once")
        return ;
        }
        
        const form_data = new FormData()
        form_data.append("file", file.files[0])
        form_data.append("chatID", chat_id)
        
        try {

            const response = await fetch(`/upload_pdf`, {
                "method" : "POST",
                "body" : form_data
            })

            const json = await response.json()

            update_file_status(true, null)
            FILE_ID = json.filename
            // return json.filename
            
        }
        catch (error) {
            update_file_status(false, "Something went wrong while uploading PDF.")
        }
        
})
function appendMessage(text, sender) {
    const msg = document.createElement('div');
    msg.classList.add('message', sender);
if (sender === 'bot') {
    msg.innerHTML = `<pre><code>${text}</code></pre>`; // Render HTML for bot
}
if (sender === "file_uploaded") {
    msg.innerHTML = text
}
else {
    msg.innerHTML  = text; // Keep user input safe
}
    messagesDiv.appendChild(msg);
    messagesDiv.scrollTop = messagesDiv.scrollHeight;
}

button.addEventListener('click', async event => {
    event.preventDefault()
    
    const userText = input.value.trim();
    if (!userText) return;
    
    appendMessage(userText, 'user');
    input.value = '';
    
    try {
    const response = await fetch(`/generate`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ data: userText, id : FILE_ID, chatID : chat_id})
    });
    const result = await response.json();
    appendMessage(result.llm_response, 'bot');
    } catch (err) {
    appendMessage('Error connecting to server.', 'bot');
    }
});

input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
    button.click();
}
});