File size: 2,536 Bytes
0e12947
 
 
 
d4f4a6d
0e12947
 
d4f4a6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e12947
 
 
 
d4f4a6d
0e12947
d4f4a6d
0e12947
 
 
 
 
 
 
 
 
 
 
 
 
 
d4f4a6d
0e12947
 
 
 
 
 
 
 
 
 
 
 
d4f4a6d
 
 
0e12947
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Anshul Chatbot</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #111;
      color: #eee;
      margin: 0;
      padding: 20px;
    }
    h2 {
      text-align: center;
      color: #4cafef;
    }
    #chatbox {
      width: 100%;
      max-width: 600px;
      margin: 20px auto;
      background: #222;
      padding: 15px;
      border-radius: 10px;
      height: 400px;
      overflow-y: auto;
    }
    .msg {
      margin: 8px 0;
      line-height: 1.4;
    }
    .user {
      color: #4cafef;
    }
    .bot {
      color: #90ee90;
    }
    #input-area {
      text-align: center;
      margin-top: 10px;
    }
    input {
      width: 70%;
      padding: 10px;
      border-radius: 5px;
      border: none;
      outline: none;
    }
    button {
      padding: 10px 15px;
      margin-left: 5px;
      border: none;
      border-radius: 5px;
      background: #4cafef;
      color: #fff;
      cursor: pointer;
    }
    button:hover {
      background: #3b8edb;
    }
  </style>
</head>
<body>
  <h2>🤖 Anshul Chatbot</h2>

  <div id="chatbox"></div>

  <div id="input-area">
    <input id="msg" placeholder="Type your message..." />
    <button onclick="sendMessage()">Send</button>
  </div>

  <script>
    const MODEL_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill";

    async function sendMessage() {
      const input = document.getElementById("msg");
      const chatbox = document.getElementById("chatbox");
      const userMsg = input.value.trim();
      if (!userMsg) return;

      // User message
      chatbox.innerHTML += `<div class="msg user"><b>You:</b> ${userMsg}</div>`;
      input.value = "";
      chatbox.scrollTop = chatbox.scrollHeight;

      try {
        const res = await fetch(MODEL_URL, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ inputs: userMsg })
        });

        const data = await res.json();
        const reply = data[0]?.generated_text || "⚠️ Sorry, no reply.";

        // Bot reply
        chatbox.innerHTML += `<div class="msg bot"><b>Bot:</b> ${reply}</div>`;
        chatbox.scrollTop = chatbox.scrollHeight;

      } catch (error) {
        chatbox.innerHTML += `<div class="msg bot"><b>Bot:</b> ❌ Error connecting to model.</div>`;
      }
    }
  </script>
</body>
</html>