File size: 9,646 Bytes
4d7f359 59ce6b3 4d7f359 59ce6b3 6be9fbf 4d7f359 6be9fbf a5ab9af 6be9fbf 4d7f359 6be9fbf 4d7f359 6be9fbf 4d7f359 6be9fbf a5ab9af 6be9fbf a5ab9af 4d7f359 a5ab9af 4d7f359 a5ab9af 4d7f359 a5ab9af 4d7f359 a5ab9af 4d7f359 6be9fbf 4d7f359 6be9fbf 4d7f359 59ce6b3 a5ab9af ea9c71b 59ce6b3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
// Global variables
let currentSlideIndex = 0;
let slideInterval;
let speechSynthesis = window.speechSynthesis;
let currentUtterance = null;
let slides = [];
const slideDuration = 8000; // 8 seconds per slide
// Generate presentation based on topic using OpenAI
async function generatePresentation(topic, slideCount) {
try {
// Show loading state
document.querySelector('main').innerHTML = `
<div class="max-w-4xl mx-auto bg-white rounded-2xl shadow-xl p-8 text-center">
<div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-pink-600 mx-auto mb-4"></div>
<p class="text-lg text-pink-700">Generating your presentation about ${topic}...</p>
</div>
`;
// Prepare prompt for OpenAI
const prompt = `Create a ${slideCount}-slide presentation about "${topic}".
For each slide, provide:
1. A concise slide title (max 8 words)
2. Detailed content (3-5 bullet points or 1-2 paragraphs)
Format as JSON with array of {title, content}. Include introduction and conclusion slides.`;
// Call OpenAI API (you'll need to replace OPENAI_API_KEY with your actual key)
// Mock response for testing since we can't use actual API key
const mockResponse = {
"choices": [{
"message": {
"content": JSON.stringify([
{
"title": "Introduction to Evaporation",
"content": "Evaporation is the process where liquid turns into vapor. It occurs when molecules gain enough energy to escape the liquid's surface. This is a key part of the water cycle."
},
{
"title": "How Evaporation Works",
"content": "Molecules in a liquid move at different speeds. The fastest molecules can break free from the surface. Heat increases the rate of evaporation."
},
{
"title": "Factors Affecting Evaporation",
"content": "Temperature - Higher temps increase evaporation. Surface area - More area means faster evaporation. Humidity - Lower humidity speeds up evaporation. Air flow - Moving air carries away vapor."
},
{
"title": "Evaporation vs Boiling",
"content": "Evaporation happens at any temp below boiling point. It occurs only at the surface. Boiling happens throughout the liquid at a specific temperature."
},
{
"title": "Applications of Evaporation",
"content": "Used in cooling systems like sweating. Important in distillation processes. Used in salt production from seawater. Helps in drying clothes and foods."
}
])
}
}]
};
// Use mock response instead of actual API call
const data = mockResponse;
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${OPENAI_API_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: prompt
}
],
temperature: 0.7
})
});
const data = await response.json();
const content = data.choices[0].message.content;
let slideData;
try {
// Try to parse JSON if returned as JSON string
slideData = JSON.parse(content);
} catch {
// If parsing fails, assume it's text and create slides
const sections = content.split('\n\n').filter(s => s.trim().length > 0);
slideData = sections.map((section, i) => {
const lines = section.split('\n');
const title = lines[0].replace('Title:', '').replace('Slide Title:', '').trim();
const content = lines.slice(1).join('\n').trim();
return { title, content };
});
}
// Process into slides
slides = [];
for (let i = 0; i < slideData.length; i++) {
const slide = slideData[i];
slides.push({
image: `http://static.photos/1200x630/${i+1}`,
title: slide.title || (i === 0 ? `Introduction to ${topic}` : `${topic}: Key Point ${i}`),
text: slide.content || slide.text || `Content for slide ${i+1} about ${topic}`
});
}
// Start the slideshow
startSlideshowWithVoice();
// Start the slideshow without redirect
startSlideshowWithVoice();
} catch (error) {
console.error('Error generating presentation:', error);
// Fallback with better sample content
slides = [];
for (let i = 0; i < slideCount; i++) {
let title, text;
if (i === 0) {
title = `Introduction to ${topic}`;
text = `${topic} is an important process in nature. Let's explore its key aspects in this presentation.`;
} else if (i === slideCount - 1) {
title = `Conclusion on ${topic}`;
text = `We've covered the main points about ${topic}. Thank you for your attention!`;
} else {
title = `${topic}: Key Aspect ${i}`;
text = `This slide discusses important details about ${topic}. Specific information about aspect ${i} would be presented here. For example, we might explain how this relates to the overall topic.`;
});
}
// Ensure we have at least one slide
if (slides.length === 0) {
slides.push({
image: `http://static.photos/science/1200x630/1`,
title: `Presentation on ${topic}`,
text: `This presentation will explore various aspects of ${topic}. Let's begin our journey of discovery!`
});
});
// Start the slideshow with fallback content
startSlideshowWithVoice();
}
}
function startSlideshowWithVoice() {
// Clear any existing interval
if (slideInterval) {
clearInterval(slideInterval);
}
if (currentUtterance) {
speechSynthesis.cancel();
}
// Start with first slide
updateSlide(0);
// Set up automatic slideshow
slideInterval = setInterval(() => {
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
updateSlide(currentSlideIndex);
}, slideDuration);
// Speak the first slide
speakCurrentSlide();
}
function updateSlide(index) {
const slide = slides[index];
const slideshow = document.querySelector('custom-slideshow');
if (slideshow) {
slideshow.setAttribute('image', slide.image);
slideshow.setAttribute('title', slide.title);
slideshow.setAttribute('text', slide.text);
slideshow.setAttribute('current-index', index + 1);
slideshow.setAttribute('total-slides', slides.length);
// Update progress bar
updateProgressBar();
}
// Speak the new slide content
speakCurrentSlide();
}
function speakCurrentSlide() {
if (currentUtterance) {
speechSynthesis.cancel();
}
const slide = slides[currentSlideIndex];
const text = `${slide.title}. ${slide.text}`; // Speak both title and content
currentUtterance = new SpeechSynthesisUtterance(text);
currentUtterance.rate = 1;
currentUtterance.pitch = 1;
currentUtterance.volume = 1;
speechSynthesis.speak(currentUtterance);
}
function updateProgressBar() {
const progressBar = document.querySelector('.progress-fill');
if (progressBar) {
progressBar.style.width = '0%';
// Reset animation
void progressBar.offsetWidth;
progressBar.style.width = '100%';
progressBar.style.transitionDuration = `${slideDuration/1000}s`;
}
}
// Expose controls to the window
window.toggleVoice = function() {
if (speechSynthesis.speaking) {
speechSynthesis.cancel();
} else {
speakCurrentSlide();
}
};
window.nextSlide = function() {
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
updateSlide(currentSlideIndex);
// Reset interval
clearInterval(slideInterval);
slideInterval = setInterval(() => {
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
updateSlide(currentSlideIndex);
}, slideDuration);
};
window.prevSlide = function() {
currentSlideIndex = (currentSlideIndex - 1 + slides.length) % slides.length;
updateSlide(currentSlideIndex);
// Reset interval
clearInterval(slideInterval);
slideInterval = setInterval(() => {
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
updateSlide(currentSlideIndex);
}, slideDuration);
};
// Pause on hover
document.querySelector('custom-slideshow')?.addEventListener('mouseenter', () => {
clearInterval(slideInterval);
if (currentUtterance) {
speechSynthesis.pause();
}
});
document.querySelector('custom-slideshow')?.addEventListener('mouseleave', () => {
slideInterval = setInterval(() => {
currentSlideIndex = (currentSlideIndex + 1) % slides.length;
updateSlide(currentSlideIndex);
}, slideDuration);
if (currentUtterance) {
speechSynthesis.resume();
}
}); |