Anonymusmee commited on
Commit
2961247
·
verified ·
1 Parent(s): eb9357b

Create static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +59 -0
static/script.js ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const storyInput = document.getElementById('story-input');
3
+ const animateBtn = document.getElementById('animate-btn');
4
+ const loader = document.getElementById('loader');
5
+
6
+ // Output elements
7
+ const outputVideo = document.getElementById('output-video');
8
+ const storyExplanationText = document.getElementById('story-explanation-text');
9
+ const explainerAudio = document.getElementById('explainer-audio');
10
+ const codeExplanationText = document.getElementById('code-explanation-text');
11
+ const promptOutput = document.getElementById('prompt-output');
12
+
13
+ animateBtn.addEventListener('click', async () => {
14
+ const story = storyInput.value;
15
+ if (!story.trim()) {
16
+ alert('Please write a story first!');
17
+ return;
18
+ }
19
+
20
+ // Show loader and disable button
21
+ loader.style.display = 'block';
22
+ animateBtn.disabled = true;
23
+ animateBtn.textContent = 'Generating...';
24
+
25
+ try {
26
+ const response = await fetch('/animate', {
27
+ method: 'POST',
28
+ headers: {
29
+ 'Content-Type': 'application/json',
30
+ },
31
+ body: JSON.stringify({ story: story }),
32
+ });
33
+
34
+ if (!response.ok) {
35
+ throw new Error(`Server error: ${response.statusText}`);
36
+ }
37
+
38
+ const data = await response.json();
39
+
40
+ // Update the page with the results from the backend
41
+ outputVideo.src = data.video_url;
42
+ outputVideo.load(); // Important to load the new source
43
+
44
+ storyExplanationText.textContent = data.story_explanation;
45
+ explainerAudio.src = data.audio_url;
46
+ codeExplanationText.textContent = data.code_explanation;
47
+ promptOutput.textContent = data.prompt;
48
+
49
+ } catch (error) {
50
+ console.error('Error:', error);
51
+ alert('An error occurred while creating the animation. See the console for details.');
52
+ } finally {
53
+ // Hide loader and re-enable button
54
+ loader.style.display = 'none';
55
+ animateBtn.disabled = false;
56
+ animateBtn.textContent = '✨ Create Animation';
57
+ }
58
+ });
59
+ });