Spaces:
Running

chatdepp / index.html
ericmendez's picture
Como configurar com a minha API? - Initial Deployment
ca7e0e3 verified
Raw History Blame Contribute Delete
24.8 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DeepSeek Chat</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
<script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<script src="https://unpkg.com/feather-icons"></script>
<style>
.chat-container {
height: calc(100vh - 64px);
}
.message-stream {
height: calc(100% - 72px);
}
.markdown-content pre {
background-color: #1e293b;
color: #f8fafc;
padding: 1rem;
border-radius: 0.5rem;
overflow-x: auto;
}
.markdown-content code {
background-color: #1e293b;
color: #f8fafc;
padding: 0.2rem 0.4rem;
border-radius: 0.25rem;
font-family: monospace;
}
</style>
</head>
<body class="bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-gray-100">
<div id="app" class="flex flex-col h-screen">
<!-- Header -->
<header class="bg-white dark:bg-gray-800 shadow-sm">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 flex justify-between items-center">
<div class="flex items-center space-x-2">
<div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center">
<i data-feather="message-square" class="text-white"></i>
</div>
<h1 class="text-xl font-bold">DeepSeek Chat</h1>
</div>
<div class="flex items-center space-x-4">
<button id="settings-btn" class="p-2 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700">
<i data-feather="settings"></i>
</button>
<button id="auth-btn" class="px-4 py-2 rounded-md bg-blue-500 text-white hover:bg-blue-600">
Sign In
</button>
</div>
</div>
</header>
<!-- Main Content -->
<main class="flex flex-1 overflow-hidden">
<!-- Sidebar -->
<aside id="sidebar" class="w-64 bg-white dark:bg-gray-800 border-r border-gray-200 dark:border-gray-700 flex flex-col">
<div class="p-4 border-b border-gray-200 dark:border-gray-700">
<button id="new-chat-btn" class="w-full flex items-center justify-center space-x-2 px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">
<i data-feather="plus"></i>
<span>New Chat</span>
</button>
</div>
<div class="flex-1 overflow-y-auto">
<div id="conversation-list" class="p-2 space-y-1">
<!-- Conversations will be loaded here -->
</div>
</div>
<div class="p-4 border-t border-gray-200 dark:border-gray-700">
<div class="flex items-center space-x-2">
<div class="w-8 h-8 rounded-full bg-gray-200 dark:bg-gray-700 flex items-center justify-center">
<i data-feather="user" class="text-gray-600 dark:text-gray-300"></i>
</div>
<span id="user-email" class="text-sm">Guest User</span>
</div>
</div>
</aside>
<!-- Chat Area -->
<section class="flex-1 flex flex-col">
<div id="chat-container" class="chat-container flex-1 flex flex-col">
<!-- Messages -->
<div id="message-stream" class="message-stream overflow-y-auto p-4 space-y-6">
<!-- Messages will be loaded here -->
<div class="text-center py-12 text-gray-500 dark:text-gray-400">
<div class="mx-auto w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-4">
<i data-feather="message-square" class="w-8 h-8"></i>
</div>
<h3 class="text-lg font-medium">Start a new conversation</h3>
<p class="mt-1">Ask anything or try one of the example prompts</p>
</div>
</div>
<!-- Input Area -->
<div class="p-4 border-t border-gray-200 dark:border-gray-700">
<form id="message-form" class="relative">
<textarea id="message-input" rows="1" placeholder="Type your message..."
class="w-full px-4 py-3 pr-12 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 resize-none"></textarea>
<button type="submit" id="send-btn" class="absolute right-3 bottom-3 p-1 rounded-md text-blue-500 hover:bg-blue-50 dark:hover:bg-gray-700">
<i data-feather="send"></i>
</button>
<button type="button" id="stop-btn" class="absolute right-3 bottom-3 p-1 rounded-md text-red-500 hover:bg-red-50 dark:hover:bg-gray-700 hidden">
<i data-feather="square"></i>
</button>
</form>
<p class="text-xs text-gray-500 dark:text-gray-400 mt-2 text-center">
DeepSeek Chat may produce inaccurate information. Consider verifying important information.
</p>
</div>
</div>
</section>
</main>
</div>
<!-- Settings Modal -->
<div id="settings-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-2xl max-h-[90vh] overflow-hidden">
<div class="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center">
<h2 class="text-lg font-semibold">Chat Settings</h2>
<button id="close-settings" class="p-1 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700">
<i data-feather="x"></i>
</button>
</div>
<div class="overflow-y-auto p-4">
<div class="space-y-6">
<div>
<h3 class="font-medium mb-2">Model Settings</h3>
<div class="space-y-4">
<div>
<label for="model-select" class="block text-sm font-medium mb-1">Model</label>
<select id="model-select" class="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md">
<option value="deepseek-chat">DeepSeek Chat</option>
<option value="deepseek-coder">DeepSeek Coder</option>
</select>
</div>
<div>
<label for="temperature" class="block text-sm font-medium mb-1">Temperature: <span id="temperature-value">0.7</span></label>
<input type="range" id="temperature" min="0" max="1" step="0.1" value="0.7" class="w-full">
</div>
<div>
<label for="max-tokens" class="block text-sm font-medium mb-1">Max Tokens: <span id="max-tokens-value">1024</span></label>
<input type="range" id="max-tokens" min="128" max="4096" step="128" value="1024" class="w-full">
</div>
</div>
</div>
<div>
<h3 class="font-medium mb-2">System Prompt</h3>
<textarea id="system-prompt" rows="4" class="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md" placeholder="You are a helpful AI assistant..."></textarea>
<div class="flex justify-between mt-2">
<div>
<label for="prompt-templates" class="block text-sm font-medium mb-1">Templates</label>
<div class="flex space-x-2">
<select id="prompt-templates" class="flex-1 px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md">
<option value="">Select a template...</option>
<option value="assistant">Default Assistant</option>
<option value="coder">Code Assistant</option>
<option value="creative">Creative Writer</option>
</select>
<button id="save-template-btn" class="px-3 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">
<i data-feather="save" class="w-4 h-4"></i>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="p-4 border-t border-gray-200 dark:border-gray-700 flex justify-end">
<button id="save-settings" class="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Save Settings</button>
</div>
</div>
</div>
<!-- Auth Modal -->
<div id="auth-modal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden">
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md overflow-hidden">
<div class="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center">
<h2 class="text-lg font-semibold">Sign In</h2>
<button id="close-auth" class="p-1 rounded-full hover:bg-gray-100 dark:hover:bg-gray-700">
<i data-feather="x"></i>
</button>
</div>
<div class="p-6">
<div class="space-y-4">
<button id="google-auth-btn" class="w-full flex items-center justify-center space-x-2 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-md hover:bg-gray-50 dark:hover:bg-gray-700">
<i data-feather="mail" class="w-4 h-4"></i>
<span>Continue with Google</span>
</button>
<div class="relative">
<div class="absolute inset-0 flex items-center">
<div class="w-full border-t border-gray-300 dark:border-gray-600"></div>
</div>
<div class="relative flex justify-center text-sm">
<span class="px-2 bg-white dark:bg-gray-800 text-gray-500 dark:text-gray-400">Or</span>
</div>
</div>
<div>
<label for="email" class="block text-sm font-medium mb-1">Email</label>
<input type="email" id="email" class="w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md" placeholder="your@email.com">
</div>
<button id="email-auth-btn" class="w-full px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Send Magic Link</button>
</div>
</div>
</div>
</div>
<script>
// Initialize AOS and Feather Icons
AOS.init();
feather.replace();
// DOM Elements
const settingsBtn = document.getElementById('settings-btn');
const authBtn = document.getElementById('auth-btn');
const settingsModal = document.getElementById('settings-modal');
const authModal = document.getElementById('auth-modal');
const closeSettings = document.getElementById('close-settings');
const closeAuth = document.getElementById('close-auth');
const saveSettings = document.getElementById('save-settings');
const messageForm = document.getElementById('message-form');
const messageInput = document.getElementById('message-input');
const sendBtn = document.getElementById('send-btn');
const stopBtn = document.getElementById('stop-btn');
const messageStream = document.getElementById('message-stream');
const newChatBtn = document.getElementById('new-chat-btn');
const conversationList = document.getElementById('conversation-list');
const modelSelect = document.getElementById('model-select');
const temperature = document.getElementById('temperature');
const maxTokens = document.getElementById('max-tokens');
const temperatureValue = document.getElementById('temperature-value');
const maxTokensValue = document.getElementById('max-tokens-value');
const systemPrompt = document.getElementById('system-prompt');
const promptTemplates = document.getElementById('prompt-templates');
const saveTemplateBtn = document.getElementById('save-template-btn');
const googleAuthBtn = document.getElementById('google-auth-btn');
const emailAuthBtn = document.getElementById('email-auth-btn');
const emailInput = document.getElementById('email');
// Event Listeners
settingsBtn.addEventListener('click', () => {
settingsModal.classList.remove('hidden');
});
authBtn.addEventListener('click', () => {
authModal.classList.remove('hidden');
});
closeSettings.addEventListener('click', () => {
settingsModal.classList.add('hidden');
});
closeAuth.addEventListener('click', () => {
authModal.classList.add('hidden');
});
saveSettings.addEventListener('click', () => {
// Save settings to localStorage
localStorage.setItem('model', modelSelect.value);
localStorage.setItem('temperature', temperature.value);
localStorage.setItem('maxTokens', maxTokens.value);
localStorage.setItem('systemPrompt', systemPrompt.value);
settingsModal.classList.add('hidden');
});
temperature.addEventListener('input', () => {
temperatureValue.textContent = temperature.value;
});
maxTokens.addEventListener('input', () => {
maxTokensValue.textContent = maxTokens.value;
});
promptTemplates.addEventListener('change', () => {
const template = promptTemplates.value;
if (template === 'assistant') {
systemPrompt.value = 'You are a helpful AI assistant. Answer questions accurately and concisely.';
} else if (template === 'coder') {
systemPrompt.value = 'You are an expert programming assistant. Provide code examples with explanations. Format code properly.';
} else if (template === 'creative') {
systemPrompt.value = 'You are a creative writer. Generate imaginative and engaging content. Be descriptive and original.';
}
});
saveTemplateBtn.addEventListener('click', () => {
const templateName = prompt('Enter a name for this template:');
if (templateName) {
// Save template to localStorage
const templates = JSON.parse(localStorage.getItem('promptTemplates') || '{}');
templates[templateName] = systemPrompt.value;
localStorage.setItem('promptTemplates', JSON.stringify(templates));
// Update dropdown
const option = document.createElement('option');
option.value = templateName;
option.textContent = templateName;
promptTemplates.appendChild(option);
promptTemplates.value = templateName;
}
});
messageForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = messageInput.value.trim();
if (!message) return;
// Add user message to chat
addMessage('user', message);
messageInput.value = '';
// Show stop button, hide send button
sendBtn.classList.add('hidden');
stopBtn.classList.remove('hidden');
// Create assistant message placeholder
const assistantMessageId = 'msg-' + Date.now();
addMessage('assistant', '', assistantMessageId);
// Scroll to bottom
messageStream.scrollTop = messageStream.scrollHeight;
try {
// Simulate API call
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: modelSelect.value,
messages: [
{role: 'system', content: systemPrompt.value},
{role: 'user', content: message}
],
temperature: parseFloat(temperature.value),
max_tokens: parseInt(maxTokens.value),
stream: true
})
});
if (!response.ok) {
throw new Error('API request failed');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let assistantMessage = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n').filter(line => line.trim() !== '');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.substring(6);
if (data === '[DONE]') break;
try {
const parsed = JSON.parse(data);
if (parsed.choices && parsed.choices[0].delta.content) {
assistantMessage += parsed.choices[0].delta.content;
updateMessage(assistantMessageId, assistantMessage);
messageStream.scrollTop = messageStream.scrollHeight;
}
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
}
}
} catch (error) {
console.error('Error:', error);
updateMessage(assistantMessageId, 'Sorry, there was an error processing your request.');
} finally {
sendBtn.classList.remove('hidden');
stopBtn.classList.add('hidden');
messageStream.scrollTop = messageStream.scrollHeight;
}
});
stopBtn.addEventListener('click', () => {
// TODO: Implement abort controller for streaming
sendBtn.classList.remove('hidden');
stopBtn.classList.add('hidden');
});
newChatBtn.addEventListener('click', () => {
// Clear current chat
messageStream.innerHTML = `
<div class="text-center py-12 text-gray-500 dark:text-gray-400">
<div class="mx-auto w-16 h-16 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center mb-4">
<i data-feather="message-square" class="w-8 h-8"></i>
</div>
<h3 class="text-lg font-medium">Start a new conversation</h3>
<p class="mt-1">Ask anything or try one of the example prompts</p>
</div>
`;
feather.replace();
});
// Load saved settings
if (localStorage.getItem('model')) {
modelSelect.value = localStorage.getItem('model');
}
if (localStorage.getItem('temperature')) {
temperature.value = localStorage.getItem('temperature');
temperatureValue.textContent = temperature.value;
}
if (localStorage.getItem('maxTokens')) {
maxTokens.value = localStorage.getItem('maxTokens');
maxTokensValue.textContent = maxTokens.value;
}
if (localStorage.getItem('systemPrompt')) {
systemPrompt.value = localStorage.getItem('systemPrompt');
}
// Load prompt templates from localStorage
const templates = JSON.parse(localStorage.getItem('promptTemplates') || '{}');
for (const [name, content] of Object.entries(templates)) {
const option = document.createElement('option');
option.value = name;
option.textContent = name;
promptTemplates.appendChild(option);
}
// Helper functions
function addMessage(role, content, id = '') {
if (role === 'user') {
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-end';
messageDiv.innerHTML = `
<div class="max-w-3xl bg-blue-500 text-white rounded-lg p-4">
<div class="markdown-content">${escapeHtml(content)}</div>
</div>
`;
messageStream.appendChild(messageDiv);
} else if (role === 'assistant') {
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-start';
messageDiv.id = id;
messageDiv.innerHTML = `
<div class="max-w-3xl bg-gray-100 dark:bg-gray-700 rounded-lg p-4">
<div class="markdown-content">${escapeHtml(content)}</div>
</div>
`;
messageStream.appendChild(messageDiv);
}
feather.replace();
}
function updateMessage(id, content) {
const messageDiv = document.getElementById(id);
if (messageDiv) {
const contentDiv = messageDiv.querySelector('.markdown-content');
if (contentDiv) {
contentDiv.innerHTML = markdownToHtml(content);
}
}
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
function markdownToHtml(markdown) {
// Simple markdown to HTML conversion (basic support for **bold** and `code`)
let html = escapeHtml(markdown);
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
html = html.replace(/\n/g, '<br>');
return html;
}
// Auto-resize textarea
messageInput.addEventListener('input', function() {
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
});
</script>
</body>
</html>