Spaces:
Running
Running
Download script.js from hellooooooo000/lovable-ai-coder: direct link, hf CLI and curl.
- Browser
- Download file 7.6 kB
-
https://huggingface.co/spaces/hellooooooo000/lovable-ai-coder/resolve/main/script.js
- Command line
-
hf download hf://spaces/hellooooooo000/lovable-ai-coder/script.js
-
curl -L -o script.js https://huggingface.co/spaces/hellooooooo000/lovable-ai-coder/resolve/main/script.js
7.6 kB
| // Main application functionality | |
| document.addEventListener('DOMContentLoaded', () => { | |
| // Code generation | |
| const generateBtn = document.getElementById('generate-btn'); | |
| const appDescription = document.getElementById('app-description'); | |
| const codeOutput = document.getElementById('code-output'); | |
| const chatSection = document.getElementById('chat'); | |
| const previewSection = document.getElementById('preview'); | |
| const previewFrame = document.getElementById('preview-frame'); | |
| const viewCodeBtn = document.getElementById('view-code-btn'); | |
| const viewPreviewBtn = document.getElementById('view-preview-btn'); | |
| generateBtn.addEventListener('click', async () => { | |
| if (!appDescription.value.trim()) { | |
| alert('Please describe your app idea first!'); | |
| return; | |
| } | |
| // Show generating state | |
| generateBtn.disabled = true; | |
| generateBtn.innerHTML = '<i data-feather="loader" class="animate-spin"></i> Generating...'; | |
| feather.replace(); | |
| try { | |
| const prompt = `Crée une application web complète (HTML, CSS, JavaScript) qui ${appDescription.value}. Fournis le code complet et fonctionnel.`; | |
| // Call DeepSeek API | |
| const response = await fetch('https://api.deepseek.com/v1/chat/completions', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': 'Bearer YOUR_DEEPSEEK_API_KEY' | |
| }, | |
| body: JSON.stringify({ | |
| model: "deepseek-chat", | |
| messages: [ | |
| { | |
| role: "user", | |
| content: prompt | |
| } | |
| ], | |
| temperature: 0.7, | |
| max_tokens: 2048 | |
| }) | |
| }); | |
| const data = await response.json(); | |
| if (!response.ok || !data.choices?.[0]?.message?.content) { | |
| throw new Error(data.error?.message || 'Failed to generate code. Please check your API key and try again.'); | |
| } | |
| let generatedCode = data.choices[0].message.content; | |
| // Display the code with typewriter effect | |
| typeWriterEffect(codeOutput, generatedCode, 10, () => { | |
| generateBtn.disabled = false; | |
| generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App'; | |
| feather.replace(); | |
| // Show preview and chat sections | |
| previewSection.classList.remove('hidden'); | |
| chatSection.classList.remove('hidden'); | |
| // Update preview iframe | |
| // Extract just the code block if present | |
| const codeMatch = generatedCode.match(/```[a-zA-Z]*\n([\s\S]*?)\n```/); | |
| const codeToShow = codeMatch ? codeMatch[1] : generatedCode; | |
| const blob = new Blob([codeToShow], {type: 'text/html'}); | |
| previewFrame.src = URL.createObjectURL(blob); | |
| }); | |
| } catch (error) { | |
| console.error('Error generating code:', error); | |
| alert(`Error: ${error.message}\n\nPlease check:\n1. Your internet connection\n2. The API key is valid\n3. Try a different description`); | |
| generateBtn.disabled = false; | |
| generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App'; | |
| feather.replace(); | |
| } | |
| }); | |
| // Toggle between code and preview | |
| viewCodeBtn.addEventListener('click', () => { | |
| codeOutput.style.display = 'block'; | |
| previewFrame.style.display = 'none'; | |
| viewCodeBtn.classList.add('active'); | |
| viewPreviewBtn.classList.remove('active'); | |
| }); | |
| viewPreviewBtn.addEventListener('click', () => { | |
| codeOutput.style.display = 'none'; | |
| previewFrame.style.display = 'block'; | |
| viewPreviewBtn.classList.add('active'); | |
| viewCodeBtn.classList.remove('active'); | |
| }); | |
| // Chat functionality | |
| const chatInput = document.getElementById('chat-input'); | |
| const sendChatBtn = document.getElementById('send-chat-btn'); | |
| const chatMessages = document.getElementById('chat-messages'); | |
| // Add initial welcome message | |
| addMessage("Hi! I'm your AI coding assistant. I've generated the initial code for your app. How would you like to modify it?", 'ai'); | |
| sendChatBtn.addEventListener('click', sendMessage); | |
| chatInput.addEventListener('keypress', (e) => { | |
| if (e.key === 'Enter') sendMessage(); | |
| }); | |
| function sendMessage() { | |
| const message = chatInput.value.trim(); | |
| if (!message) return; | |
| // Add user message | |
| addMessage(message, 'user'); | |
| chatInput.value = ''; | |
| // Call Gemini API for response | |
| try { | |
| const response = await fetch('https://api.deepseek.com/v1/chat/completions', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': 'Bearer YOUR_DEEPSEEK_API_KEY' | |
| }, | |
| body: JSON.stringify({ | |
| model: "deepseek-chat", | |
| messages: [ | |
| { | |
| role: "user", | |
| content: `You are an AI coding assistant. The user asked: "${message}". Please provide a helpful response about modifying their code.` | |
| } | |
| ], | |
| temperature: 0.7, | |
| max_tokens: 1024 | |
| }) | |
| }); | |
| const data = await response.json(); | |
| if (!response.ok || !data.choices?.[0]?.message?.content) { | |
| throw new Error(data.error?.message || 'Failed to get AI response.'); | |
| } | |
| const aiResponse = data.choices[0].message.content; | |
| addMessage(aiResponse, 'ai'); | |
| chatMessages.scrollTop = chatMessages.scrollHeight; | |
| } catch (error) { | |
| console.error('Error getting AI response:', error); | |
| addMessage(`Error: ${error.message}\n\nPlease try again or refresh the page.`, 'ai'); | |
| } | |
| } | |
| function addMessage(text, sender) { | |
| const messageDiv = document.createElement('div'); | |
| messageDiv.className = `flex mb-4 ${sender === 'user' ? 'justify-end' : 'justify-start'}`; | |
| const bubble = document.createElement('div'); | |
| bubble.className = `${sender === 'user' ? 'bg-indigo-600 text-white' : 'bg-indigo-100 text-gray-800'} p-3 rounded-lg max-w-xs md:max-w-md lg:max-w-lg`; | |
| bubble.textContent = text; | |
| bubble.style.whiteSpace = 'pre-wrap'; // Preserve line breaks | |
| messageDiv.appendChild(bubble); | |
| chatMessages.appendChild(messageDiv); | |
| chatMessages.scrollTop = chatMessages.scrollHeight; | |
| } | |
| // Helper functions | |
| function typeWriterEffect(element, text, speed, callback) { | |
| let i = 0; | |
| element.textContent = ''; | |
| function type() { | |
| if (i < text.length) { | |
| element.textContent += text.charAt(i); | |
| i++; | |
| setTimeout(type, speed); | |
| } else if (callback) { | |
| callback(); | |
| } | |
| } | |
| type(); | |
| } | |
| function extractTitle(description) { | |
| const firstSentence = description.split('.')[0]; | |
| return firstSentence || "My Awesome App"; | |
| } | |
| function extractDescription(description) { | |
| return description.length > 100 ? description.substring(0, 100) + "..." : description; | |
| } | |
| }); |