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://cdn.tailwindcss.com"></script>
|
| 8 |
+
<!-- JSZip for client side unzip and document parsing -->
|
| 9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
|
| 10 |
+
</head>
|
| 11 |
+
<body class="bg-gray-900 text-gray-100 h-screen flex flex-col font-sans">
|
| 12 |
+
|
| 13 |
+
<!-- Header Config -->
|
| 14 |
+
<header class="bg-gray-800 border-b border-gray-700 p-4">
|
| 15 |
+
<div class="max-w-6xl mx-auto flex flex-col sm:flex-row justify-between items-center gap-4">
|
| 16 |
+
<h1 class="text-xl font-bold tracking-tight text-white flex items-center gap-2">
|
| 17 |
+
<span>⚡</span> HF Space AI Gateway
|
| 18 |
+
</h1>
|
| 19 |
+
<div class="flex flex-wrap items-center gap-3 w-full sm:w-auto justify-end">
|
| 20 |
+
<!-- Smart HF URL Container: Shows only if HTML is run locally -->
|
| 21 |
+
<div id="hf-url-container" class="hidden w-full sm:w-auto">
|
| 22 |
+
<input type="text" id="hf-url-input" placeholder="Hugging Face Direct URL (e.g. https://your-space.hf.space)"
|
| 23 |
+
class="bg-gray-700 border border-gray-600 rounded px-3 py-1.5 text-xs text-white w-full sm:w-72">
|
| 24 |
+
</div>
|
| 25 |
+
|
| 26 |
+
<div class="flex items-center gap-2">
|
| 27 |
+
<label for="model-select" class="text-xs text-gray-300 whitespace-nowrap">Model:</label>
|
| 28 |
+
<select id="model-select" class="bg-gray-700 border border-gray-600 rounded px-2.5 py-1.5 text-xs text-white focus:outline-none focus:ring-2 focus:ring-blue-500">
|
| 29 |
+
<option value="claude-opus-4-8">claude-opus-4-8</option>
|
| 30 |
+
<option value="claude-opus-4-7">claude-opus-4-7</option>
|
| 31 |
+
<option value="claude-opus-4-6">claude-opus-4-6</option>
|
| 32 |
+
<option value="glm-5.2">glm-5.2</option>
|
| 33 |
+
<option value="gpt-5.5">gpt-5.5</option>
|
| 34 |
+
</select>
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
</div>
|
| 38 |
+
</header>
|
| 39 |
+
|
| 40 |
+
<!-- Chat Console -->
|
| 41 |
+
<main id="chat-container" class="flex-1 overflow-y-auto p-4 space-y-4 max-w-4xl mx-auto w-full">
|
| 42 |
+
<div class="bg-gray-800 rounded-lg p-4 border border-gray-700 text-gray-300 text-sm">
|
| 43 |
+
<p class="font-semibold text-white mb-1">CORS & WAF Bypassed Workspace</p>
|
| 44 |
+
<p class="text-xs">ZIP archives, python/js documents, aur images upload karein. ZIP files browser mein hi automatically extract ho kar readable XML codeblocks mein tabdeel ho jayengi taake AI poore code ko dekh sake.</p>
|
| 45 |
+
</div>
|
| 46 |
+
<div id="file-preview-area" class="flex flex-wrap gap-2"></div>
|
| 47 |
+
</main>
|
| 48 |
+
|
| 49 |
+
<!-- Controls Input -->
|
| 50 |
+
<footer class="bg-gray-800 border-t border-gray-700 p-4">
|
| 51 |
+
<div class="max-w-4xl mx-auto flex flex-col gap-3">
|
| 52 |
+
<div class="flex items-center gap-4">
|
| 53 |
+
<label class="cursor-pointer bg-gray-700 hover:bg-gray-600 text-xs text-gray-300 px-3 py-1.5 rounded border border-gray-600 flex items-center gap-1.5">
|
| 54 |
+
<span>📎</span> Attach Files / ZIPs / Images
|
| 55 |
+
<input type="file" id="file-input" multiple class="hidden">
|
| 56 |
+
</label>
|
| 57 |
+
<span id="attached-count" class="text-xs text-gray-400">No files attached</span>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<div class="flex gap-3">
|
| 61 |
+
<input type="text" id="user-input" placeholder="Type a message..."
|
| 62 |
+
class="flex-1 bg-gray-700 border border-gray-600 rounded px-4 py-3 text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-500">
|
| 63 |
+
<button id="send-btn" class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-6 py-3 rounded transition">
|
| 64 |
+
Send
|
| 65 |
+
</button>
|
| 66 |
+
</div>
|
| 67 |
+
</div>
|
| 68 |
+
</footer>
|
| 69 |
+
|
| 70 |
+
<script>
|
| 71 |
+
const API_KEY = "sk-P7JoqWr5SriePQgq1ijtiGxkL5elV4C84xtqL2MoNQpi1uLs";
|
| 72 |
+
|
| 73 |
+
const chatContainer = document.getElementById("chat-container");
|
| 74 |
+
const userInput = document.getElementById("user-input");
|
| 75 |
+
const sendBtn = document.getElementById("send-btn");
|
| 76 |
+
const modelSelect = document.getElementById("model-select");
|
| 77 |
+
const fileInput = document.getElementById("file-input");
|
| 78 |
+
const filePreviewArea = document.getElementById("file-preview-area");
|
| 79 |
+
const attachedCount = document.getElementById("attached-count");
|
| 80 |
+
const hfUrlContainer = document.getElementById("hf-url-container");
|
| 81 |
+
const hfUrlInput = document.getElementById("hf-url-input");
|
| 82 |
+
|
| 83 |
+
let conversationHistory = [];
|
| 84 |
+
let pendingFilesData = [];
|
| 85 |
+
|
| 86 |
+
// Check if index.html is loaded as local file or hosted directly
|
| 87 |
+
const isLocal = window.location.protocol === "file:" || window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1";
|
| 88 |
+
|
| 89 |
+
if (isLocal) {
|
| 90 |
+
hfUrlContainer.classList.remove("hidden"); // Show URL field if run as offline file
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
const isTextFile = (name) => /\.(txt|py|js|jsx|ts|tsx|html|css|json|csv|md|yaml|yml|rs|go|java|c|cpp|h|sh|bat)$/i.test(name);
|
| 94 |
+
const isImageFile = (name) => /\.(png|jpg|jpeg|webp|gif)$/i.test(name);
|
| 95 |
+
|
| 96 |
+
const toBase64 = file => new Promise((resolve, reject) => {
|
| 97 |
+
const reader = new FileReader();
|
| 98 |
+
reader.readAsDataURL(file);
|
| 99 |
+
reader.onload = () => resolve(reader.result);
|
| 100 |
+
reader.onerror = error => reject(error);
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
// Smart file handler for ZIPs and documents
|
| 104 |
+
fileInput.addEventListener("change", async (e) => {
|
| 105 |
+
const files = e.target.files;
|
| 106 |
+
for (let file of files) {
|
| 107 |
+
try {
|
| 108 |
+
if (file.name.endsWith(".zip")) {
|
| 109 |
+
const zip = await JSZip.loadAsync(file);
|
| 110 |
+
let extractedText = "";
|
| 111 |
+
for (let relativePath in zip.files) {
|
| 112 |
+
const zipFile = zip.files[relativePath];
|
| 113 |
+
if (!zipFile.dir && isTextFile(relativePath)) {
|
| 114 |
+
const content = await zipFile.async("string");
|
| 115 |
+
// Wrap contents in XML style tags so models can process structures correctly
|
| 116 |
+
extractedText += `\n<file path="${relativePath}">\n${content}\n</file>\n`;
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
pendingFilesData.push({
|
| 120 |
+
type: "text_data",
|
| 121 |
+
name: file.name,
|
| 122 |
+
content: `User uploaded a ZIP file named "${file.name}" with the following file structures inside:\n${extractedText}`
|
| 123 |
+
});
|
| 124 |
+
} else if (isTextFile(file.name)) {
|
| 125 |
+
const text = await file.text();
|
| 126 |
+
pendingFilesData.push({
|
| 127 |
+
type: "text_data",
|
| 128 |
+
name: file.name,
|
| 129 |
+
content: `\n<file path="${file.name}">\n${text}\n</file>\n`
|
| 130 |
+
});
|
| 131 |
+
} else if (isImageFile(file.name)) {
|
| 132 |
+
const base64Str = await toBase64(file);
|
| 133 |
+
pendingFilesData.push({
|
| 134 |
+
type: "image_data",
|
| 135 |
+
name: file.name,
|
| 136 |
+
content: base64Str
|
| 137 |
+
});
|
| 138 |
+
} else {
|
| 139 |
+
// Fallback plain text read
|
| 140 |
+
const text = await file.text();
|
| 141 |
+
pendingFilesData.push({
|
| 142 |
+
type: "text_data",
|
| 143 |
+
name: file.name,
|
| 144 |
+
content: `\n<file path="${file.name}">\n${text}\n</file>\n`
|
| 145 |
+
});
|
| 146 |
+
}
|
| 147 |
+
} catch (err) {
|
| 148 |
+
alert(`Could not process/read file: ${file.name}`);
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
updatePreviewUI();
|
| 152 |
+
});
|
| 153 |
+
|
| 154 |
+
function updatePreviewUI() {
|
| 155 |
+
filePreviewArea.innerHTML = "";
|
| 156 |
+
attachedCount.textContent = `${pendingFilesData.length} files attached`;
|
| 157 |
+
pendingFilesData.forEach((file, index) => {
|
| 158 |
+
const badge = document.createElement("div");
|
| 159 |
+
badge.className = "bg-gray-800 text-xs px-2.5 py-1 rounded border border-gray-700 flex items-center gap-1.5";
|
| 160 |
+
badge.innerHTML = `
|
| 161 |
+
<span class="text-gray-400">${file.type === 'image_data' ? '🖼️' : '📄'} ${file.name}</span>
|
| 162 |
+
<button class="text-red-400 font-bold ml-1 hover:text-red-300" onclick="removeFile(${index})">×</button>
|
| 163 |
+
`;
|
| 164 |
+
filePreviewArea.appendChild(badge);
|
| 165 |
+
});
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
window.removeFile = (index) => {
|
| 169 |
+
pendingFilesData.splice(index, 1);
|
| 170 |
+
updatePreviewUI();
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
function appendMessage(role, text) {
|
| 174 |
+
const messageDiv = document.createElement("div");
|
| 175 |
+
messageDiv.className = `flex ${role === 'user' ? 'justify-end' : 'justify-start'}`;
|
| 176 |
+
const innerDiv = document.createElement("div");
|
| 177 |
+
innerDiv.className = `max-w-[85%] rounded-lg px-4 py-3 ${
|
| 178 |
+
role === 'user'
|
| 179 |
+
? 'bg-blue-600 text-white rounded-br-none'
|
| 180 |
+
: 'bg-gray-800 text-gray-200 border border-gray-700 rounded-bl-none'
|
| 181 |
+
}`;
|
| 182 |
+
innerDiv.style.whiteSpace = 'pre-wrap';
|
| 183 |
+
innerDiv.textContent = text;
|
| 184 |
+
messageDiv.appendChild(innerDiv);
|
| 185 |
+
chatContainer.appendChild(messageDiv);
|
| 186 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 187 |
+
return innerDiv;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
async function handleSend() {
|
| 191 |
+
let apiEndpointUrl = "/v1/chat/completions";
|
| 192 |
+
|
| 193 |
+
if (isLocal) {
|
| 194 |
+
const customHfUrl = hfUrlInput.value.trim().replace(/\/$/, "");
|
| 195 |
+
if (!customHfUrl) {
|
| 196 |
+
alert("Pehle apna Hugging Face Space URL enter karein!");
|
| 197 |
+
return;
|
| 198 |
+
}
|
| 199 |
+
apiEndpointUrl = `${customHfUrl}/v1/chat/completions`;
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
const textInputVal = userInput.value.trim();
|
| 203 |
+
if (!textInputVal && pendingFilesData.length === 0) return;
|
| 204 |
+
|
| 205 |
+
let textPayload = textInputVal;
|
| 206 |
+
let imagePayloads = [];
|
| 207 |
+
|
| 208 |
+
pendingFilesData.forEach(file => {
|
| 209 |
+
if (file.type === "text_data") {
|
| 210 |
+
textPayload += `\n\n${file.content}`;
|
| 211 |
+
} else if (file.type === "image_data") {
|
| 212 |
+
imagePayloads.push(file.content);
|
| 213 |
+
}
|
| 214 |
+
});
|
| 215 |
+
|
| 216 |
+
appendMessage("user", textInputVal || "Attached files context uploaded");
|
| 217 |
+
userInput.value = "";
|
| 218 |
+
pendingFilesData = [];
|
| 219 |
+
updatePreviewUI();
|
| 220 |
+
|
| 221 |
+
// Construct payload content shape
|
| 222 |
+
let messageContent;
|
| 223 |
+
if (imagePayloads.length > 0) {
|
| 224 |
+
messageContent = [{ type: "text", text: textPayload }];
|
| 225 |
+
imagePayloads.forEach(imgBase64 => {
|
| 226 |
+
messageContent.push({
|
| 227 |
+
type: "image_url",
|
| 228 |
+
image_url: { url: imgBase64 }
|
| 229 |
+
});
|
| 230 |
+
});
|
| 231 |
+
} else {
|
| 232 |
+
messageContent = textPayload;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
conversationHistory.push({ role: "user", content: messageContent });
|
| 236 |
+
|
| 237 |
+
const assistantBubble = appendMessage("assistant", "Generating answer...");
|
| 238 |
+
assistantBubble.textContent = "";
|
| 239 |
+
|
| 240 |
+
try {
|
| 241 |
+
const response = await fetch(apiEndpointUrl, {
|
| 242 |
+
method: "POST",
|
| 243 |
+
headers: {
|
| 244 |
+
"Content-Type": "application/json",
|
| 245 |
+
"Authorization": `Bearer ${API_KEY}`
|
| 246 |
+
},
|
| 247 |
+
body: JSON.stringify({
|
| 248 |
+
model: modelSelect.value,
|
| 249 |
+
messages: conversationHistory,
|
| 250 |
+
stream: true
|
| 251 |
+
})
|
| 252 |
+
});
|
| 253 |
+
|
| 254 |
+
if (!response.ok) {
|
| 255 |
+
throw new Error(`HTTP Error: ${response.status}`);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
const reader = response.body.getReader();
|
| 259 |
+
const decoder = new TextDecoder("utf-8");
|
| 260 |
+
let buffer = "";
|
| 261 |
+
let completeResponse = "";
|
| 262 |
+
|
| 263 |
+
while (true) {
|
| 264 |
+
const { value, done } = await reader.read();
|
| 265 |
+
if (done) break;
|
| 266 |
+
|
| 267 |
+
buffer += decoder.decode(value, { stream: true });
|
| 268 |
+
const lines = buffer.split("\n");
|
| 269 |
+
buffer = lines.pop();
|
| 270 |
+
|
| 271 |
+
for (const line of lines) {
|
| 272 |
+
const cleanLine = line.trim();
|
| 273 |
+
if (cleanLine.startsWith("data: ")) {
|
| 274 |
+
const dataStr = cleanLine.slice(6);
|
| 275 |
+
if (dataStr === "[DONE]") break;
|
| 276 |
+
try {
|
| 277 |
+
const parsed = JSON.parse(dataStr);
|
| 278 |
+
const content = parsed.choices[0].delta.content;
|
| 279 |
+
if (content) {
|
| 280 |
+
completeResponse += content;
|
| 281 |
+
assistantBubble.textContent = completeResponse;
|
| 282 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 283 |
+
}
|
| 284 |
+
} catch (e) {}
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
conversationHistory.push({ role: "assistant", content: completeResponse });
|
| 290 |
+
|
| 291 |
+
} catch (error) {
|
| 292 |
+
console.error(error);
|
| 293 |
+
assistantBubble.textContent = `Error: ${error.message}. Connection lost or HF space is paused.`;
|
| 294 |
+
assistantBubble.classList.add("text-red-400");
|
| 295 |
+
}
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
sendBtn.addEventListener("click", handleSend);
|
| 299 |
+
userInput.addEventListener("keydown", (e) => {
|
| 300 |
+
if (e.key === "Enter") handleSend();
|
| 301 |
+
});
|
| 302 |
+
</script>
|
| 303 |
+
</body>
|
| 304 |
+
</html>
|