Riddwam commited on
Commit
fc0b7ea
·
verified ·
1 Parent(s): e64082e

Upload 7 files

Browse files
Files changed (7) hide show
  1. index.css +145 -0
  2. index.html +24 -0
  3. index.tsx +167 -0
  4. metadata.json +4 -0
  5. package.json +20 -0
  6. tsconfig.json +30 -0
  7. vite.config.ts +17 -0
index.css ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @import url('https://fonts.googleapis.com/css2?family=Indie+Flower&family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap');
2
+
3
+ * {
4
+ box-sizing: border-box;
5
+ margin: 0;
6
+ padding: 0;
7
+ }
8
+
9
+ body {
10
+ font-family: 'Space Mono', monospace;
11
+ padding: 20px;
12
+ background-color: light-dark(#f8f9fa, #212529);
13
+ color: light-dark(#343a40, #f8f9fa);
14
+ line-height: 1.6;
15
+ max-width: 1200px;
16
+ margin: 20px auto;
17
+ }
18
+
19
+ .container {
20
+ display: flex;
21
+ flex-direction: column;
22
+ gap: 8px;
23
+ }
24
+
25
+ h1 {
26
+ margin-bottom: 40px;
27
+ color: light-dark(#212529, #ffffff);
28
+ text-align: center;
29
+ }
30
+
31
+ #examples {
32
+ list-style: none;
33
+ padding: 0;
34
+ cursor: pointer;
35
+ }
36
+
37
+ #examples li {
38
+ margin-bottom: 10px;
39
+ padding: 15px 20px;
40
+ border: 1px solid light-dark(#dee2e6, #495057);
41
+ border-radius: 6px;
42
+ background-color: light-dark(#ffffff, #343a40);
43
+ transition: background-color 0.2s ease-in-out, border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
44
+ }
45
+
46
+ #examples li:hover {
47
+ background-color: light-dark(#e9ecef, #495057);
48
+ border-color: light-dark(#adb5bd, #adb5bd);
49
+ box-shadow: 0 2px 4px light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.05));
50
+ }
51
+
52
+ #slideshow {
53
+ display: flex;
54
+ flex-direction: row;
55
+ overflow-x: auto;
56
+ scroll-snap-type: x mandatory;
57
+ overscroll-behavior-x: contain;
58
+ gap: 25px;
59
+ margin-bottom: 40px;
60
+ padding: 10px 5px 20px 5px; /* Adjusted padding */
61
+ border: 1px solid light-dark(#e9ecef, #495057);
62
+ border-radius: 8px;
63
+ background-color: light-dark(#ffffff, #343a40);
64
+ box-shadow: 0 2px 8px light-dark(rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.05));
65
+
66
+ &[hidden] {
67
+ display: none;
68
+ }
69
+ }
70
+
71
+ .slide {
72
+ border: 1px solid light-dark(#ced4da, #495057);
73
+ padding: 25px;
74
+ font-family: "Indie Flower", cursive;
75
+ scroll-snap-align: center;
76
+ background-color: #ffffff;
77
+ border-radius: 8px;
78
+ display: flex;
79
+ flex-direction: column;
80
+ align-items: center;
81
+ min-width: 380px; /* Slightly increased min-width */
82
+ box-shadow: 0 4px 12px light-dark(rgba(0, 0, 0, 0.08), rgba(255, 255, 255, 0.08));
83
+ transition: transform 0.2s ease-in-out;
84
+ }
85
+
86
+ .slide:hover {
87
+ transform: translateY(-3px);
88
+ }
89
+
90
+ .slide div { /* Targeting the caption div inside .slide */
91
+ font-size: 24px;
92
+ text-align: center;
93
+ text-wrap: balance;
94
+ margin-top: 20px;
95
+ color: #495057;
96
+ }
97
+
98
+ .slide img {
99
+ height: 320px; /* Adjusted height */
100
+ max-width: 100%;
101
+ object-fit: contain;
102
+ border-radius: 6px;
103
+ }
104
+
105
+ .user-turn {
106
+ font-weight: bold;
107
+ padding: 15px 20px;
108
+ background-color: light-dark(#e7f5ff, #0b3d66);
109
+ border-radius: 6px;
110
+ border-left: 4px solid light-dark(#1c7ed6, #66b2ff);
111
+ }
112
+
113
+ textarea#input {
114
+ width: 100%;
115
+ padding: 15px 20px;
116
+ border: 1px solid light-dark(#ced4da, #495057);
117
+ border-radius: 6px;
118
+ font-family: 'Space Mono', monospace;
119
+ margin-top: 8px;
120
+ min-height: 90px;
121
+ resize: vertical;
122
+ transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
123
+ background-color: light-dark(#ffffff, #343a40);
124
+ color: light-dark(#343a40, #f8f9fa);
125
+ }
126
+
127
+ textarea#input:focus {
128
+ outline: none;
129
+ border-color: light-dark(#1c7ed6, #66b2ff);
130
+ box-shadow: 0 0 0 2px light-dark(rgba(28, 126, 214, 0.2), rgba(102, 178, 255, 0.2));
131
+ }
132
+
133
+ #output + p {
134
+ margin-bottom: 8px;
135
+ font-weight: bold;
136
+ color: light-dark(#495057, #dee2e6);
137
+ }
138
+
139
+ #error {
140
+ font-weight: bold;
141
+ padding: 15px 20px;
142
+ background-color: light-dark(#ffe7e7, #660b0b);
143
+ border-radius: 6px;
144
+ border-left: 4px solid light-dark(#d61c1c, #ff6666);
145
+ }
index.html ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script type="importmap">
2
+ {
3
+ "imports": {
4
+ "@google/genai": "https://esm.sh/@google/genai@^0.7.0",
5
+ "marked": "https://esm.sh/marked@^15.0.7"
6
+ }
7
+ }
8
+ </script>
9
+ <div class="container">
10
+ <h1>Explain Things with Lots of Tiny Cats</h1>
11
+ <p>Examples:</p>
12
+ <ul id="examples">
13
+ <li>Explain how neural networks work.</li>
14
+ <li>Explain how The Matrix works.</li>
15
+ <li>Explain how spaghettification works.</li>
16
+ </ul>
17
+ <p>or enter your own prompt:</p>
18
+ <textarea id="input" placeholder="Enter some prompt and press Enter."></textarea>
19
+ <div id="output"></div>
20
+ <div id="error" hidden></div>
21
+ <div id="slideshow" hidden></div>
22
+ </div>
23
+ <link rel="stylesheet" href="/index.css">
24
+ <script type="module" src="/index.tsx"></script>
index.tsx ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * @license
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ import {GoogleGenAI, Modality} from '@google/genai';
7
+ import {marked} from 'marked';
8
+
9
+ const ai = new GoogleGenAI({apiKey: process.env.API_KEY}); // Use API_KEY
10
+
11
+ const chat = ai.chats.create({
12
+ model: 'gemini-2.5-flash-preview-04-17', // Updated model name
13
+ config: {
14
+ responseModalities: [Modality.TEXT, Modality.IMAGE],
15
+ },
16
+ history: [],
17
+ });
18
+
19
+ const userInput = document.querySelector('#input') as HTMLTextAreaElement;
20
+ const modelOutput = document.querySelector('#output') as HTMLDivElement;
21
+ const slideshow = document.querySelector('#slideshow') as HTMLDivElement;
22
+ const error = document.querySelector('#error') as HTMLDivElement;
23
+
24
+ const additionalInstructions = `
25
+ Use a fun story about lots of tiny cats as a metaphor.
26
+ Keep sentences short but conversational, casual, and engaging.
27
+ Generate a cute, minimal illustration for each sentence with black ink on white background.
28
+ No commentary, just begin your explanation.
29
+ Keep going until you're done.`;
30
+
31
+ async function addSlide(text: string, image: HTMLImageElement | null) {
32
+ const slide = document.createElement('div');
33
+ slide.className = 'slide';
34
+ const caption = document.createElement('div') as HTMLDivElement;
35
+ caption.innerHTML = await marked.parse(text);
36
+ if (image) {
37
+ slide.append(image);
38
+ }
39
+ slide.append(caption);
40
+ slideshow.append(slide);
41
+ }
42
+
43
+ function parseGeminiErrorMessage(errorCandidate: string): string {
44
+ try {
45
+ // Attempt to find a JSON string within the errorCandidate
46
+ const jsonRegex = /\{.*\}/s;
47
+ const match = errorCandidate.match(jsonRegex);
48
+
49
+ if (match && match[0]) {
50
+ const jsonObj = JSON.parse(match[0]);
51
+ if (jsonObj.error && jsonObj.error.message) {
52
+ return jsonObj.error.message;
53
+ }
54
+ if (jsonObj.message) {
55
+ return jsonObj.message;
56
+ }
57
+ }
58
+ } catch (e) {
59
+ // Parsing failed, or not the expected structure. Fall through.
60
+ }
61
+ // If no JSON or specific message found, return a cleaned up part or the original string.
62
+ const statusMatch = errorCandidate.match(/status:\s*\d+\s*([\s\S]*)/);
63
+ if (statusMatch && statusMatch[1]) {
64
+ const generalMessage = statusMatch[1].replace(/\{.*\}/s, '').trim();
65
+ if (generalMessage) return generalMessage;
66
+ }
67
+ if (errorCandidate.includes('API_KEY_INVALID')) {
68
+ return 'Invalid API Key. Please ensure your API key is correctly configured.';
69
+ }
70
+ return errorCandidate; // Fallback
71
+ }
72
+
73
+ async function generate(message: string) {
74
+ userInput.disabled = true;
75
+
76
+ chat.history.length = 0;
77
+ modelOutput.innerHTML = '';
78
+ slideshow.innerHTML = '';
79
+ error.innerHTML = '';
80
+ error.toggleAttribute('hidden', true);
81
+
82
+ try {
83
+ const userTurn = document.createElement('div') as HTMLDivElement;
84
+ userTurn.innerHTML = await marked.parse(message);
85
+ userTurn.className = 'user-turn';
86
+ modelOutput.append(userTurn);
87
+ userInput.value = '';
88
+
89
+ const result = await chat.sendMessageStream({
90
+ message: message + additionalInstructions,
91
+ });
92
+
93
+ let text = '';
94
+ let img: HTMLImageElement | null = null;
95
+
96
+ for await (const chunk of result) {
97
+ for (const candidate of chunk.candidates) {
98
+ for (const part of candidate.content.parts ?? []) {
99
+ if (part.text) {
100
+ text += part.text;
101
+ } else if (part.inlineData) {
102
+ try {
103
+ const data = part.inlineData;
104
+ img = document.createElement('img');
105
+ img.src = `data:image/png;base64,` + data.data;
106
+ } catch (e) {
107
+ console.warn('Failed to process inlineData:', e);
108
+ img = null; // Ensure img is null if processing fails
109
+ }
110
+ }
111
+
112
+ // If we have both text and an image, create a slide
113
+ if (text.trim() && img) {
114
+ await addSlide(text, img);
115
+ slideshow.removeAttribute('hidden');
116
+ text = '';
117
+ img = null;
118
+ }
119
+ }
120
+ }
121
+ }
122
+ // If there's leftover text (e.g., last part was text, or an image failed to load but text was present)
123
+ // and no image was paired with it.
124
+ if (text.trim()) {
125
+ await addSlide(text, img); // img might be null here, addSlide handles it
126
+ slideshow.removeAttribute('hidden');
127
+ text = ''; // Clear text
128
+ img = null; // Clear img
129
+ }
130
+
131
+ } catch (e: unknown) {
132
+ let rawMessage: string;
133
+ if (e instanceof Error) {
134
+ rawMessage = e.message;
135
+ } else if (typeof e === 'string') {
136
+ rawMessage = e;
137
+ } else {
138
+ rawMessage = 'An unexpected error occurred and its details could not be extracted.';
139
+ }
140
+ const displayMessage = parseGeminiErrorMessage(rawMessage);
141
+ error.innerHTML = `Something went wrong: ${displayMessage}`;
142
+ error.removeAttribute('hidden');
143
+ }
144
+ userInput.disabled = false;
145
+ userInput.focus();
146
+ }
147
+
148
+ userInput.addEventListener('keydown', async (e: KeyboardEvent) => {
149
+ if (e.code === 'Enter' && !e.shiftKey) { // Allow shift+enter for newlines
150
+ e.preventDefault();
151
+ const message = userInput.value.trim();
152
+ if (message) {
153
+ await generate(message);
154
+ }
155
+ }
156
+ });
157
+
158
+ const examples = document.querySelectorAll('#examples li');
159
+ examples.forEach((li) =>
160
+ li.addEventListener('click', async () => {
161
+ const textContent = li.textContent;
162
+ if (textContent) {
163
+ userInput.value = textContent; // Populate textarea for consistency
164
+ await generate(textContent);
165
+ }
166
+ }),
167
+ );
metadata.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "name": "Tiny Cats",
3
+ "description": "Tiny cats explain! Get fun, illustrated slideshows breaking down complex topics with adorable feline metaphors."
4
+ }
package.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "tiny-cats",
3
+ "private": true,
4
+ "version": "0.0.0",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "vite build",
9
+ "preview": "vite preview"
10
+ },
11
+ "dependencies": {
12
+ "@google/genai": "^0.7.0",
13
+ "marked": "^15.0.7"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "^22.14.0",
17
+ "typescript": "~5.7.2",
18
+ "vite": "^6.2.0"
19
+ }
20
+ }
tsconfig.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "experimentalDecorators": true,
5
+ "useDefineForClassFields": false,
6
+ "module": "ESNext",
7
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
8
+ "skipLibCheck": true,
9
+
10
+ /* Bundler mode */
11
+ "moduleResolution": "bundler",
12
+ "allowImportingTsExtensions": true,
13
+ "isolatedModules": true,
14
+ "moduleDetection": "force",
15
+ "noEmit": true,
16
+ "allowJs": true,
17
+ "jsx": "react-jsx",
18
+
19
+ /* Linting */
20
+ "strict": true,
21
+ "noUnusedLocals": true,
22
+ "noUnusedParameters": true,
23
+ "noFallthroughCasesInSwitch": true,
24
+ "noUncheckedSideEffectImports": true,
25
+
26
+ "paths": {
27
+ "@/*" : ["./*"]
28
+ }
29
+ }
30
+ }
vite.config.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import path from 'path';
2
+ import { defineConfig, loadEnv } from 'vite';
3
+
4
+ export default defineConfig(({ mode }) => {
5
+ const env = loadEnv(mode, '.', '');
6
+ return {
7
+ define: {
8
+ 'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY),
9
+ 'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY)
10
+ },
11
+ resolve: {
12
+ alias: {
13
+ '@': path.resolve(__dirname, '.'),
14
+ }
15
+ }
16
+ };
17
+ });