Update static/js/chat.js
Browse files- static/js/chat.js +32 -1
static/js/chat.js
CHANGED
@@ -5,6 +5,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
5 |
const tellStoryButton = document.getElementById('tell-story-button');
|
6 |
const storyThemeInput = document.getElementById('story-theme');
|
7 |
const storyOutput = document.getElementById('story-output');
|
|
|
|
|
|
|
8 |
const comicStrip = document.getElementById('comic-strip');
|
9 |
|
10 |
// Clear chat messages on page load (do not load history from Firebase)
|
@@ -63,6 +66,21 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
63 |
const storyData = await storyResponse.json();
|
64 |
storyOutput.innerHTML = `<p><strong>Krishna’s Story:</strong> ${storyData.story}</p>`;
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
// Generate the comic strip
|
67 |
const comicResponse = await fetch('/comic');
|
68 |
const comicData = await comicResponse.json();
|
@@ -73,9 +91,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
73 |
comicStrip.appendChild(img);
|
74 |
});
|
75 |
} catch (error) {
|
76 |
-
console.error('Error generating story or comic strip:', error);
|
77 |
storyOutput.innerHTML = '<p>Error generating story—try again!</p>';
|
|
|
78 |
comicStrip.innerHTML = '<p>Error generating comic strip—try again!</p>';
|
79 |
}
|
80 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
});
|
|
|
5 |
const tellStoryButton = document.getElementById('tell-story-button');
|
6 |
const storyThemeInput = document.getElementById('story-theme');
|
7 |
const storyOutput = document.getElementById('story-output');
|
8 |
+
const storyAudio = document.getElementById('story-audio');
|
9 |
+
const tellRiddleButton = document.getElementById('tell-riddle-button');
|
10 |
+
const riddleOutput = document.getElementById('riddle-output');
|
11 |
const comicStrip = document.getElementById('comic-strip');
|
12 |
|
13 |
// Clear chat messages on page load (do not load history from Firebase)
|
|
|
66 |
const storyData = await storyResponse.json();
|
67 |
storyOutput.innerHTML = `<p><strong>Krishna’s Story:</strong> ${storyData.story}</p>`;
|
68 |
|
69 |
+
// Generate audio narration for the story
|
70 |
+
const audioResponse = await fetch('/story_audio', {
|
71 |
+
method: 'POST',
|
72 |
+
headers: {
|
73 |
+
'Content-Type': 'application/json',
|
74 |
+
},
|
75 |
+
body: JSON.stringify({ story_text: storyData.story })
|
76 |
+
});
|
77 |
+
const audioData = await audioResponse.json();
|
78 |
+
if (audioData.audio_url) {
|
79 |
+
storyAudio.src = audioData.audio_url;
|
80 |
+
storyAudio.style.display = 'block';
|
81 |
+
storyAudio.play().catch(error => console.error('Error playing audio:', error));
|
82 |
+
}
|
83 |
+
|
84 |
// Generate the comic strip
|
85 |
const comicResponse = await fetch('/comic');
|
86 |
const comicData = await comicResponse.json();
|
|
|
91 |
comicStrip.appendChild(img);
|
92 |
});
|
93 |
} catch (error) {
|
94 |
+
console.error('Error generating story, audio, or comic strip:', error);
|
95 |
storyOutput.innerHTML = '<p>Error generating story—try again!</p>';
|
96 |
+
storyAudio.style.display = 'none';
|
97 |
comicStrip.innerHTML = '<p>Error generating comic strip—try again!</p>';
|
98 |
}
|
99 |
});
|
100 |
+
|
101 |
+
// Handle "Tell me a riddle!" button click
|
102 |
+
tellRiddleButton.addEventListener('click', async () => {
|
103 |
+
try {
|
104 |
+
const riddleResponse = await fetch('/riddle');
|
105 |
+
const riddleData = await riddleResponse.json();
|
106 |
+
riddleOutput.innerHTML = `<p><strong>Krishna’s Riddle:</strong> ${riddleData.riddle}</p>`;
|
107 |
+
} catch (error) {
|
108 |
+
console.error('Error generating riddle:', error);
|
109 |
+
riddleOutput.innerHTML = '<p>Error generating riddle—try again!</p>';
|
110 |
+
}
|
111 |
+
});
|
112 |
});
|