File size: 3,081 Bytes
e476578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ChatBot</title>
    <link href="https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.css" rel="stylesheet" type="text/css" />
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 h-screen flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-md">
        <h1 class="text-2xl font-bold mb-4 text-center">Your Friendly ChatBot</h1>
        <div id="chatbox" class="chat chat-start h-96 overflow-y-auto mb-4"></div>
        <div class="flex">
            <input type="text" id="inputMessage" placeholder="Type your message here..." class="input input-bordered flex-grow">
            <button id="sendMessage" class="btn btn-primary ml-2">Send</button>
        </div>
    </div>

    <script>
        var inputMessage = document.getElementById("inputMessage");
        var sendMessage = document.getElementById("sendMessage");
        var chatbox = document.getElementById("chatbox");

        sendMessage.addEventListener("click", function() {
            var message = inputMessage.value;
            if (message.trim() !== "") {
                sendMessageToChat(message, "user");
                inputMessage.value = "";

                fetchResponse(message)
                    .then(response => {
                        sendMessageToChat(response, "assistant");
                    })
                    .catch(error => {
                        console.error("Error fetching response:", error);
                        sendMessageToChat("Sorry, I'm having trouble understanding you right now.", "assistant");
                    });
            }
        });

        function sendMessageToChat(message, role) {
            var messageElement = document.createElement("div");
            messageElement.classList.add("chat-bubble", role === "user" ? "chat-bubble-primary" : "chat-bubble-secondary");
            messageElement.innerHTML = "<p><strong>" + role + ":</strong> " + message + "</p>";
            chatbox.appendChild(messageElement);
            chatbox.scrollTop = chatbox.scrollHeight;
        }

        async function fetchResponse(message) {
            const response = await fetch(
                "https://api-inference.huggingface.co/models/google/flan-t5-xxl",
                {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json",
                        "Authorization": "Bearer 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" 
                    },
                    body: JSON.stringify({
                        inputs: message,
                    }),
                }
            );

            if (!response.ok) {
                throw new Error("Request failed with status " + response.status);
            }

            const data = await response.json();
            return data[0].generated_text; 
        }
    </script>
</body>
</html>