File size: 7,403 Bytes
a11b959 e7ef452 |
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChhayaGPT</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
background: linear-gradient(
to right,
#E0EAFC, /* Light Sky Blue */
#CFDEF3, /* Pastel Blue */
#A6E6FF /* Soft Blue */
);
height: 100vh;
color: black;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 20px;
}
.container {
width: 90%;
max-width: 600px;
background: rgba(255, 255, 255, 0.8); /* White with opacity */
backdrop-filter: blur(15px);
padding: 20px;
border-radius: 12px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
text-align: center;
}
.chat-box {
height: 300px;
overflow-y: auto;
padding: 10px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.6); /* White with opacity */
}
.user-message, .bot-message {
padding: 12px;
border-radius: 10px;
margin: 10px 0;
max-width: 75%;
word-wrap: break-word;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}
.user-message {
align-self: flex-end;
background: linear-gradient(135deg, #ff758c, #ff7eb3); /* Pink Gradient */
}
.bot-message {
align-self: flex-start;
background: linear-gradient(135deg, #36d1dc, #5b86e5); /* Blue Gradient */
}
.chat-input {
display: flex;
margin-top: 15px;
}
.chat-input input {
flex-grow: 1;
padding: 12px;
border-radius: 8px;
border: none;
outline: none;
background: rgba(255, 255, 255, 0.6); /* White with opacity */
color: black;
}
.send-btn {
padding: 12px 18px;
margin-left: 10px;
background: linear-gradient(45deg, #6a11cb, #2575fc); /* Purple Gradient */
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
}
.send-btn:hover {
background: linear-gradient(45deg, #2575fc, #6a11cb); /* Gradient on hover */
}
.image-container img {
width: 100%;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}
@media (max-width: 768px) {
.container {
width: 100%;
padding: 15px;
}
.chat-box {
height: 250px;
}
}
</style>
</head>
<body>
<div class="container">
<h2>ChhayaGPT</h2>
<input type="text" id="chat-api-key" placeholder="Enter Hugging Face API Key (Chat)" style="width: 100%; padding: 10px; margin-bottom: 10px; border-radius: 5px;">
<input type="text" id="image-api-key" placeholder="Enter Hugging Face API Key (Image)" style="width: 100%; padding: 10px; margin-bottom: 10px; border-radius: 5px;">
<div class="chat-box" id="chat-box"></div>
<div class="chat-input">
<input type="text" id="user-input" placeholder="Type a message...">
<button class="send-btn" id="send-button">Send</button>
</div>
<div class="image-container" id="image-container"></div>
<!-- Example Slider -->
<div class="slider">
<div class="slide active" style="background-image: url('https://example.com/image1.jpg');"></div>
<div class="slide inactive" style="background-image: url('https://example.com/image2.jpg');"></div>
</div>
</div>
<script type="module">
import { HfInference } from "https://cdn.jsdelivr.net/npm/@huggingface/inference/+esm";
let chatClient, imageClient;
document.getElementById("send-button").addEventListener("click", handleChat);
document.getElementById("user-input").addEventListener("keypress", function(event) {
if (event.key === "Enter") handleChat();
});
async function handleChat() {
const userInput = document.getElementById("user-input").value;
const chatApiKey = document.getElementById("chat-api-key").value;
const imageApiKey = document.getElementById("image-api-key").value;
if (!userInput.trim() || !chatApiKey.trim() || !imageApiKey.trim()) return;
if (!chatClient) chatClient = new HfInference(chatApiKey);
if (!imageClient) imageClient = new HfInference(imageApiKey);
const chatBox = document.getElementById("chat-box");
chatBox.innerHTML += `<div class='user-message'><strong>You:</strong> ${userInput}</div>`;
document.getElementById("user-input").value = "";
if (userInput.toLowerCase().startsWith("generate an image of")) {
const imagePrompt = userInput.replace("generate an image of", "").trim();
await generateImage(imagePrompt);
} else {
await fetchChatResponse(userInput);
}
chatBox.scrollTop = chatBox.scrollHeight;
}
async function fetchChatResponse(input) {
try {
const response = await chatClient.chatCompletion({
model: "mistralai/Mistral-7B-Instruct-v0.2",
messages: [{ role: "user", content: input }],
max_tokens: 500,
});
const botMessage = response.choices[0].message.content;
document.getElementById("chat-box").innerHTML += `<div class='bot-message'><strong>ChhayaGPT:</strong> ${botMessage}</div>`;
} catch (error) {
console.error("Error fetching chat response:", error);
}
}
async function generateImage(prompt) {
const imageContainer = document.getElementById("image-container");
imageContainer.innerHTML = `<p>Generating image...</p>`;
try {
if (!imageClient) {
console.error("Hugging Face Image API key is missing.");
imageContainer.innerHTML = `<p style="color: red;">Error: Missing API Key.</p>`;
return;
}
const response = await imageClient.textToImage({
model: "stabilityai/stable-diffusion-xl-base-1.0",
inputs: prompt,
parameters: { num_inference_steps: 50, guidance_scale: 7.5 },
});
// Check if the response is a Blob
if (response instanceof Blob) {
const imageBlob = response;
const imageUrl = URL.createObjectURL(imageBlob);
imageContainer.innerHTML = `<img src="${imageUrl}" alt="Generated Image">`;
} else {
console.error("Unexpected response format:", response);
imageContainer.innerHTML = `<p style="color: red;">Error: Invalid API response.</p>`;
}
} catch (error) {
console.error("Error generating image:", error.message || error);
imageContainer.innerHTML = `<p style="color: red;">Error generating image: ${error.message || "Unknown error"}</p>`;
}
}
</script>
</body>
</html>
|