aitool / index.html
8421bit's picture
Update index.html
91e52d9 verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DeepSeek Chat Interface</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background-color: #f0f2f5;
color: #1c1e21;
display: flex;
justify-content: center;
}
.container {
max-width: 800px;
width: 100%;
background: #fff;
padding: 25px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #333;
margin-bottom: 25px;
}
.auth-section {
text-align: right;
margin-bottom: 15px;
font-size: 0.9em;
}
.auth-section button {
padding: 5px 10px;
font-size: 0.9em;
cursor: pointer;
border: 1px solid #ddd;
border-radius: 4px;
background-color: #f5f5f5;
margin-left: 5px;
}
.auth-section button:hover {
background-color: #e9e9e9;
}
.auth-section span {
color: #606770;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #4b4f56;
}
textarea {
width: calc(100% - 22px); /* Full width minus padding */
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccd0d5;
border-radius: 6px;
font-size: 1rem;
min-height: 80px;
resize: vertical;
}
button#send-button {
display: block;
width: 100%;
padding: 12px;
background-color: #1877f2; /* Facebook Blue */
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
font-weight: 600;
transition: background-color 0.2s ease;
}
button#send-button:hover:not(:disabled) {
background-color: #166fe5;
}
button#send-button:disabled {
background-color: #a0c3f0;
cursor: not-allowed;
}
#status {
margin-top: 15px;
padding: 10px;
border-radius: 4px;
font-size: 0.95em;
display: none; /* Hidden by default */
text-align: center;
}
#status.info {
background-color: #e7f3ff;
border: 1px solid #b8d7f7;
color: #1877f2;
display: block;
}
#status.error {
background-color: #fdecea;
border: 1px solid #f8bbc1;
color: #a94442;
display: block;
}
#response-area {
margin-top: 20px;
border: 1px solid #e0e0e0;
border-radius: 6px;
padding: 15px;
min-height: 150px;
background-color: #f9f9f9;
overflow-y: auto;
max-height: 400px;
white-space: pre-wrap; /* Preserve whitespace and line breaks */
word-wrap: break-word; /* Wrap long words */
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; /* Monospace font for code */
font-size: 0.9em;
line-height: 1.5;
}
#response-area:empty::before {
content: "Model's response will appear here...";
color: #999;
font-style: italic;
}
</style>
</head>
<body>
<div class="container">
<h1>DeepSeek Chat</h1>
<div class="auth-section">
<span id="user-info">Checking login status...</span>
<button id="auth-button">Login</button>
</div>
<label for="user-prompt">Your Prompt:</label>
<textarea id="user-prompt" placeholder="Enter your message or instructions for the AI..."></textarea>
<!-- Optional: Add inputs/areas for 'html' and 'previousPrompt' if needed -->
<!--
<label for="context-html">Current HTML (Optional):</label>
<textarea id="context-html" placeholder="Paste existing HTML if you want the AI to modify it..."></textarea>
-->
<button id="send-button">Send Prompt</button>
<div id="status"></div>
<label for="response-area" style="margin-top: 20px;">AI Response:</label>
<div id="response-area"></div>
<!-- Use a div with pre-wrap instead of <pre> for better control -->
</div>
<script>
const userInfoSpan = document.getElementById('user-info');
const authButton = document.getElementById('auth-button');
const promptInput = document.getElementById('user-prompt');
const sendButton = document.getElementById('send-button');
const statusDiv = document.getElementById('status');
const responseArea = document.getElementById('response-area');
// Optional: Get context elements if you uncomment them
// const contextHtmlInput = document.getElementById('context-html');
let isLoggedIn = false;
let currentUsername = 'Guest';
// --- Authentication Handling ---
async function checkLoginStatus() {
try {
const response = await fetch('/api/@me');
if (response.ok) {
const user = await response.json();
currentUsername = user.preferred_username || user.name || 'Logged In User';
userInfoSpan.textContent = `Logged in as: ${currentUsername}`;
authButton.textContent = 'Logout';
authButton.onclick = () => window.location.href = '/auth/logout';
isLoggedIn = true;
} else {
throw new Error('Not logged in');
}
} catch (error) {
userInfoSpan.textContent = 'Not logged in.';
authButton.textContent = 'Login with Hugging Face';
authButton.onclick = () => window.location.href = '/api/login';
isLoggedIn = false;
}
}
// --- Display Status ---
function showStatus(message, type = 'info') {
statusDiv.textContent = message;
statusDiv.className = type; // 'info' or 'error'
}
function clearStatus() {
statusDiv.textContent = '';
statusDiv.className = '';
statusDiv.style.display = 'none'; // Hide it properly
}
// --- Handle Sending Prompt ---
sendButton.addEventListener('click', async () => {
const prompt = promptInput.value.trim();
// Optional: Get context
// const contextHtml = contextHtmlInput.value.trim();
if (!prompt) {
showStatus('Please enter a prompt.', 'error');
return;
}
// Clear previous results and disable button
responseArea.textContent = ''; // Clear previous response
clearStatus();
showStatus('Sending request and waiting for response...', 'info');
sendButton.disabled = true;
const requestBody = {
prompt: prompt,
// Optional: Include context if available
// html: contextHtml || null, // Send null if empty
// previousPrompt: null // Add logic if you track previous prompts
};
try {
const response = await fetch('/api/ask-ai', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/plain', // Expecting a stream
},
body: JSON.stringify(requestBody),
});
// --- Handle Non-OK HTTP Responses (e.g., 429, 500, 402) ---
if (!response.ok) {
let errorMsg = `Error: ${response.status} ${response.statusText}`;
try {
// Try to parse JSON error message from backend
const errorData = await response.json();
errorMsg = errorData.message || errorMsg;
// Check for specific flags from backend
if (errorData.openLogin) {
errorMsg += " Please log in.";
// Maybe redirect or show login button prominently
}
if (errorData.openProModal) {
errorMsg += " (Credit limit likely reached)";
}
} catch (e) {
// If response is not JSON, use the status text
console.warn("Could not parse error response as JSON");
}
throw new Error(errorMsg); // Throw to be caught below
}
// --- Process the Stream ---
const reader = response.body.getReader();
const decoder = new TextDecoder(); // Defaults to utf-8
responseArea.textContent = ''; // Ensure it's clear before streaming starts
showStatus('Receiving response...', 'info'); // Update status
while (true) {
const { value, done } = await reader.read();
if (done) {
console.log('Stream finished.');
break;
}
const chunk = decoder.decode(value, { stream: true });
responseArea.textContent += chunk; // Append chunk to display
// Auto-scroll to bottom (optional)
responseArea.scrollTop = responseArea.scrollHeight;
}
clearStatus(); // Clear status on successful completion
} catch (error) {
console.error('Error fetching or streaming response:', error);
showStatus(`Failed: ${error.message}`, 'error');
} finally {
sendButton.disabled = false; // Re-enable button
}
});
// --- Initial Setup ---
checkLoginStatus(); // Check login status when the page loads
</script>
</body>
</html>