-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
150 lines (136 loc) · 4.88 KB
/
index.html
File metadata and controls
150 lines (136 loc) · 4.88 KB
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
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Chat with Bob</title>
<!--<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
}
#chatbox {
max-width: 600px;
width: 100%;
background: #ffffff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
#chatHistoryCtn {
height: 300px;
border: 1px solid #ccc;
overflow-y: auto;
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
background: #ffffff;
}
#userInput {
width: 80%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-right: 5px;
}
#sendBtn {
width: 18%;
padding: 10px;
border: none;
background: #0078d4;
color: #ffffff;
border-radius: 5px;
cursor: pointer;
}
#sendBtn:disabled {
background: #ccc;
cursor: not-allowed;
}
#sendBtn:hover:not(:disabled) {
background: #005bb5;
}
</style>-->
</head>
<body>
<div id="chatbox">
<h1>Chat with Bob</h1>
<div id="chatHistoryCtn"></div>
<input id="userInput" type="text" placeholder="Type your message...">
<button id="sendBtn" onclick="sendMessage()">Send</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
let chatHistory = [];
async function sendMessage() {
const userInput = document.getElementById('userInput');
const sendBtn = document.getElementById('sendBtn');
const chatHistoryCtn = document.getElementById('chatHistoryCtn');
if (!userInput.value.trim()) return;
// Send user message to chat
addMessageToChat('You', userInput.value);
// Disable input and button, show loading
userInput.disabled = true;
sendBtn.disabled = true;
sendBtn.textContent = "Bob is Thinking...";
// Response from Bob
try {
let bobResponse = await generateBobResponse(userInput.value);
addMessageToChat('Bob', bobResponse);
} catch (error) {
addMessageToChat('Error', 'Failed to get a response from Bob.');
console.error('Error fetching response:', error);
}
// Re-enable input and button
userInput.value = '';
userInput.disabled = false;
sendBtn.disabled = false;
sendBtn.textContent = "Send";
}
function addMessageToChat(sender, message) {
chatHistory.push({ sender, message });
updateChatDisplay();
}
function updateChatDisplay() {
const chatHistoryCtn = document.getElementById('chatHistoryCtn');
chatHistoryCtn.innerHTML = chatHistory.map(msg =>
`<p><strong>${msg.sender}:</strong> ${msg.message}</p>`
).join('');
chatHistoryCtn.scrollTop = chatHistoryCtn.scrollHeight;
}
// Send message with Enter
document.getElementById('userInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') sendMessage();
});
// Hugging Face API
async function generateBobResponse(userMessage) {
const apiKey = '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';
const model = 'gpt2';
const prompt = `User: ${userMessage}\nBob:`;
try {
const response = await axios.post(`https://api-inference.huggingface.co/models/${model}`, {
inputs: prompt,
}, {
headers: { 'Authorization': `Bearer ${apiKey}` }
});
console.log('API Response:', response);
if (response.data && response.data[0] && response.data[0].generated_text) {
return response.data[0].generated_text.split('Bob:')[1].trim();
} else {
console.error('Unexpected response format:', response.data);
throw new Error('Unexpected response format from AI.');
}
} catch (error) {
console.error('Error generating response:', error);
throw error;
}
}
</script>
</body>
</html>