🎓
AI Tutor
Smart Learning Assistant
Advertisement
🎯 General
🇮🇳 Hindi
🇬🇧 English
🔢 Math
⚛️ Physics
🧪 Chemistry
🧬 Biology
🎓
Welcome to AI Tutor!
Ask me anything or upload an image of your problem
Advertisement
×
📷
Send
`; // Execute the scripts const scripts = adContent.querySelectorAll('script'); scripts.forEach(script => { const newScript = document.createElement('script'); if (script.src) { newScript.src = script.src; } else { newScript.textContent = script.textContent; } document.head.appendChild(newScript); }); // Hide ad after 10 seconds setTimeout(() => { chatAd.style.display = 'none'; }, 10000); } } function addLoadingMessage() { const messagesContainer = document.getElementById('chatMessages'); const loadingDiv = document.createElement('div'); loadingDiv.className = 'message ai'; loadingDiv.id = 'loadingMessage'; loadingDiv.innerHTML = `
AI
Thinking
`; messagesContainer.appendChild(loadingDiv); messagesContainer.scrollTop = messagesContainer.scrollHeight; } function removeLoadingMessage() { const loadingMessage = document.getElementById('loadingMessage'); if (loadingMessage) { loadingMessage.remove(); } } async function sendMessage() { if (isLoading) return; const input = document.getElementById('messageInput'); const message = input.value.trim(); if (!message && !selectedImageData) { showError('Please enter a message or upload an image'); return; } // Show user message if (message || selectedImage) { addMessage('user', message || 'Uploaded an image', selectedImage); } // Clear input input.value = ''; removeImage(); // Show loading isLoading = true; document.getElementById('sendBtn').disabled = true; addLoadingMessage(); try { const response = await callGeminiAPI(message); removeLoadingMessage(); addMessage('ai', response); } catch (error) { removeLoadingMessage(); console.error('Error:', error); addMessage('ai', '❌ Sorry, I encountered an error. Please try again. Error: ' + error.message); } finally { isLoading = false; document.getElementById('sendBtn').disabled = false; } } async function callGeminiAPI(question) { const systemPrompt = subjectPrompts[currentSubject]; const requestBody = { contents: [{ parts: [] }], generationConfig: { temperature: 0.7, topK: 40, topP: 0.95, maxOutputTokens: 1024, } }; // Add text part if exists if (question) { requestBody.contents[0].parts.push({ text: `${systemPrompt}\n\nStudent's question: ${question}` }); } // Add image part if exists if (selectedImageData) { requestBody.contents[0].parts.push({ text: selectedImageData ? `${systemPrompt}\n\nPlease analyze this image and help solve the problem shown.` : systemPrompt }); requestBody.contents[0].parts.push({ inline_data: { mime_type: "image/jpeg", data: selectedImageData } }); } const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(requestBody) }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error?.message || `HTTP error! status: ${response.status}`); } const data = await response.json(); if (!data.candidates || !data.candidates[0] || !data.candidates[0].content) { throw new Error('Invalid response format from API'); } return data.candidates[0].content.parts[0].text; } function showError(message) { const errorDiv = document.createElement('div'); errorDiv.className = 'error-message'; errorDiv.textContent = message; const container = document.querySelector('.input-container'); container.insertBefore(errorDiv, container.firstChild); setTimeout(() => { errorDiv.remove(); }, 5000); } function openImageModal(src) { window.open(src, '_blank'); } // Auto-resize textarea document.getElementById('messageInput').addEventListener('input', function(e) { this.style.height = 'auto'; this.style.height = Math.min(this.scrollHeight, 120) + 'px'; }); // Send message on Enter (Shift+Enter for new line) document.getElementById('messageInput').addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } }); // Initialize with a welcome message setTimeout(() => { addMessage('ai', 'Hello! I\'m your AI tutor. I can help you with any subject - just type your question or upload an image of your problem! 📚✨'); }, 500); // Refresh ads periodically for better performance setInterval(() => { // This helps refresh ads every 30 seconds for better monetization console.log('Ad refresh cycle'); }, 30000);