radames commited on
Commit
0e2b5bb
·
unverified ·
0 Parent(s):
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +31 -0
  2. .gitignore +16 -0
  3. Makefile +9 -0
  4. README.md +12 -0
  5. app.py +26 -0
  6. data.json +1 -0
  7. extract.py +31 -0
  8. frontend/.eslintignore +13 -0
  9. frontend/.eslintrc.cjs +20 -0
  10. frontend/.gitignore +12 -0
  11. frontend/.npmrc +1 -0
  12. frontend/.prettierignore +13 -0
  13. frontend/.prettierrc +8 -0
  14. frontend/README.md +38 -0
  15. frontend/package.json +45 -0
  16. frontend/postcss.config.cjs +6 -0
  17. frontend/src/app.css +3 -0
  18. frontend/src/app.d.ts +9 -0
  19. frontend/src/app.html +13 -0
  20. frontend/src/lib/ColorPalette.svelte +18 -0
  21. frontend/src/lib/Palette.svelte +49 -0
  22. frontend/src/lib/store.ts +3 -0
  23. frontend/src/lib/types.ts +15 -0
  24. frontend/src/lib/utils.ts +112 -0
  25. frontend/src/routes/+layout.svelte +5 -0
  26. frontend/src/routes/+layout.ts +1 -0
  27. frontend/src/routes/+page.svelte +145 -0
  28. frontend/static/data.json +1 -0
  29. frontend/static/favicon.png +0 -0
  30. frontend/static/images/00000-3329858467-a photo of a beautiful sunset in San Francisco.jpg +0 -0
  31. frontend/static/images/00001-3329858468-a photo of a beautiful sunset in San Francisco.jpg +0 -0
  32. frontend/static/images/00003-3329858470-a photo of a beautiful sunset in San Francisco.jpg +0 -0
  33. frontend/static/images/00009-3082979825-a photo of a polar bear in the Arctic.jpg +0 -0
  34. frontend/static/images/00010-3082979826-a photo of a polar bear in the Arctic.jpg +0 -0
  35. frontend/static/images/00011-3082979827-a photo of a polar bear in the Arctic.jpg +0 -0
  36. frontend/static/images/00013-2991374000-a photo of a polar bear in the zoo.jpg +0 -0
  37. frontend/static/images/00026-1171300814-a photo of a polar bear in the zoo.jpg +0 -0
  38. frontend/static/images/00027-1171300815-a photo of a polar bear in the zoo.jpg +0 -0
  39. frontend/static/images/00035-2824881701-a photo of a elmo drinking at starbucks.jpg +0 -0
  40. frontend/static/images/00036-2824881702-a photo of a elmo drinking at starbucks.jpg +0 -0
  41. frontend/static/images/00037-2824881703-a photo of a elmo drinking at starbucks.jpg +0 -0
  42. frontend/static/images/00044-2327336161-a painting of a Ferrari by Claude Monet.jpg +0 -0
  43. frontend/static/images/00045-2327336162-a painting of a Ferrari by Claude Monet.jpg +0 -0
  44. frontend/static/images/00046-2327336163-a painting of a Ferrari by Claude Monet.jpg +0 -0
  45. frontend/static/images/00048-3943684506-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg +0 -0
  46. frontend/static/images/00049-3943684507-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg +0 -0
  47. frontend/static/images/00050-3943684508-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg +0 -0
  48. frontend/static/robots.txt +3 -0
  49. frontend/static/svelte-welcome.png +0 -0
  50. frontend/static/svelte-welcome.webp +0 -0
.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Ignore files for PNPM, NPM and YARN
11
+ pnpm-lock.yaml
12
+ package-lock.json
13
+ yarn.lock
14
+ venv/
15
+ __pycache__/
16
+ flagged/
Makefile ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ build-client:
2
+ cd frontend && npm install && npm run build && rm -rf ../static && cp -r build/ ../static/
3
+ build-dev:
4
+ cd frontend && npm install && npm run build && rm -rf ../static && cp -r build/ ../static/
5
+ run-dev:
6
+ FLASK_ENV=development python app.py
7
+ run-prod:
8
+ python app.py
9
+ build-all: run-prod
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Sd Color Palette Generator
3
+ emoji: 📖🎨⚙️
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.3
8
+ app_file: run.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from datasets import load_dataset
3
+ from pathlib import Path
4
+ import json
5
+ from datasets import load_dataset
6
+ from flask import Flask, request, jsonify
7
+ from flask_cors import CORS
8
+ from PIL import Image
9
+ from huggingface_hub import Repository
10
+
11
+ app = Flask(__name__, static_url_path='/static')
12
+
13
+ CORS(app)
14
+
15
+
16
+ @ app.route('/')
17
+ def index():
18
+ return app.send_static_file('index.html')
19
+
20
+
21
+ if __name__ == '__main__':
22
+ mode = os.environ.get('FLASK_ENV', 'production')
23
+ print(mode)
24
+ dev = mode == 'development'
25
+ app.run(host='0.0.0.0', port=int(
26
+ os.environ.get('PORT', 7860)), debug=True, use_reloader=dev)
data.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"a painting of a Ferrari by Claude Monet": [{"colors": ["#885f45", "#181b26", "#a5966f", "#505f68"], "prompt": "a painting of a Ferrari by Claude Monet"}, {"colors": ["#ca9566", "#14161f", "#845340", "#4c556b"], "prompt": "a painting of a Ferrari by Claude Monet"}, {"colors": ["#896554", "#435d70", "#233c33", "#556a5e"], "prompt": "a painting of a Ferrari by Claude Monet"}], "a photo of a beautiful sunset in San Francisco": [{"colors": ["#ac5913", "#ed8703", "#290f03", "#f68f0e"], "prompt": "a photo of a beautiful sunset in San Francisco"}, {"colors": ["#78464d", "#2a0d08", "#3a1d24", "#e0a365"], "prompt": "a photo of a beautiful sunset in San Francisco"}, {"colors": ["#848ab9", "#6074b9", "#e39459", "#734c5c"], "prompt": "a photo of a beautiful sunset in San Francisco"}], "a photo of a elmo drinking at starbucks": [{"colors": ["#530205", "#231812", "#a4050e", "#895531"], "prompt": "a photo of a elmo drinking at starbucks"}, {"colors": ["#45010c", "#ab0211", "#c6ab77", "#0b0b13"], "prompt": "a photo of a elmo drinking at starbucks"}, {"colors": ["#31180e", "#ce010c", "#855139", "#240e11"], "prompt": "a photo of a elmo drinking at starbucks"}], "a photo of a polar bear in the Arctic": [{"colors": ["#a0d2f8", "#7ca9df", "#92c1f4", "#577db6"], "prompt": "a photo of a polar bear in the Arctic"}, {"colors": ["#8a9bb5", "#66758d", "#707c95", "#b8cde4"], "prompt": "a photo of a polar bear in the Arctic"}, {"colors": ["#7ca0da", "#527dc9", "#082e84", "#546d84"], "prompt": "a photo of a polar bear in the Arctic"}], "a paint of a polar bear in the Arctic painted by Vincent van Gogh": [{"colors": ["#eae8a7", "#dada95", "#deefe9", "#2e54b6"], "prompt": "a paint of a polar bear in the Arctic painted by Vincent van Gogh"}, {"colors": ["#91a7b4", "#163488", "#335998", "#c9d2cb"], "prompt": "a paint of a polar bear in the Arctic painted by Vincent van Gogh"}, {"colors": ["#61afdf", "#f2f1ca", "#a9e3f1", "#89d4ec"], "prompt": "a paint of a polar bear in the Arctic painted by Vincent van Gogh"}], "a photo of a polar bear in the zoo": [{"colors": ["#4b6743", "#201f15", "#20321d", "#5c6b39"], "prompt": "a photo of a polar bear in the zoo"}, {"colors": ["#1f331e", "#282c14", "#686356", "#e5eff4"], "prompt": "a photo of a polar bear in the zoo"}, {"colors": ["#6f5f3a", "#dfe6ef", "#362811", "#ecefee"], "prompt": "a photo of a polar bear in the zoo"}]}
extract.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from colorthief import ColorThief
2
+ from pathlib import Path
3
+ import json
4
+ from PIL import Image
5
+
6
+
7
+ images_path = Path('frontend/static/images')
8
+ images = images_path.glob("*.[jpeg jpg png]*")
9
+ print(images)
10
+ data = {}
11
+ for image in images:
12
+ print(image.stem)
13
+ image_pil = Image.open(image)
14
+ color_thief = ColorThief(image)
15
+ image_pil.save(Path.joinpath(images_path, (image.stem + ".jpg")), optimize=True, quality=95)
16
+ prompt = image.stem.split("-")[2]
17
+ try:
18
+ type(data[prompt]) == list
19
+ except:
20
+ data[prompt] = []
21
+
22
+ colors = color_thief.get_palette(color_count=5, quality=1)
23
+ colors_hex = ['#%02x%02x%02x' % (color) for color in colors]
24
+ data[prompt].append({
25
+ "colors": colors_hex,
26
+ "imgURL": "static/images/" + image.stem + ".jpg"
27
+ })
28
+ prompts = [{"prompt": prompt, "images": values}
29
+ for (prompt, values) in data.items()]
30
+ with open('frontend/static/data.json', 'w') as f:
31
+ json.dump(prompts, f)
frontend/.eslintignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Ignore files for PNPM, NPM and YARN
11
+ pnpm-lock.yaml
12
+ package-lock.json
13
+ yarn.lock
frontend/.eslintrc.cjs ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module.exports = {
2
+ root: true,
3
+ parser: '@typescript-eslint/parser',
4
+ extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
5
+ plugins: ['svelte3', '@typescript-eslint'],
6
+ ignorePatterns: ['*.cjs'],
7
+ overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
8
+ settings: {
9
+ 'svelte3/typescript': () => require('typescript')
10
+ },
11
+ parserOptions: {
12
+ sourceType: 'module',
13
+ ecmaVersion: 2020
14
+ },
15
+ env: {
16
+ browser: true,
17
+ es2017: true,
18
+ node: true
19
+ }
20
+ };
frontend/.gitignore ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+ .vercel
10
+ .output
11
+ venv/
12
+ __pycache__/
frontend/.npmrc ADDED
@@ -0,0 +1 @@
 
 
1
+ engine-strict=true
frontend/.prettierignore ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .DS_Store
2
+ node_modules
3
+ /build
4
+ /.svelte-kit
5
+ /package
6
+ .env
7
+ .env.*
8
+ !.env.example
9
+
10
+ # Ignore files for PNPM, NPM and YARN
11
+ pnpm-lock.yaml
12
+ package-lock.json
13
+ yarn.lock
frontend/.prettierrc ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "useTabs": true,
3
+ "singleQuote": true,
4
+ "trailingComma": "none",
5
+ "printWidth": 100,
6
+ "pluginSearchDirs": ["."],
7
+ "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
8
+ }
frontend/README.md ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # create-svelte
2
+
3
+ Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
4
+
5
+ ## Creating a project
6
+
7
+ If you're seeing this, you've probably already done this step. Congrats!
8
+
9
+ ```bash
10
+ # create a new project in the current directory
11
+ npm create svelte@latest
12
+
13
+ # create a new project in my-app
14
+ npm create svelte@latest my-app
15
+ ```
16
+
17
+ ## Developing
18
+
19
+ Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
20
+
21
+ ```bash
22
+ npm run dev
23
+
24
+ # or start the server and open the app in a new browser tab
25
+ npm run dev -- --open
26
+ ```
27
+
28
+ ## Building
29
+
30
+ To create a production version of your app:
31
+
32
+ ```bash
33
+ npm run build
34
+ ```
35
+
36
+ You can preview the production build with `npm run preview`.
37
+
38
+ > To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
frontend/package.json ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "frontend",
3
+ "version": "0.0.1",
4
+ "scripts": {
5
+ "dev": "NODE_ENV=development vite --config vite.config.dev.ts dev",
6
+ "build": "NODE_ENV=production vite build",
7
+ "preview": "vite preview",
8
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
9
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
10
+ "lint": "prettier --check . && eslint .",
11
+ "format": "prettier --write ."
12
+ },
13
+ "devDependencies": {
14
+ "@sveltejs/adapter-static": "^1.0.0-next.43",
15
+ "@sveltejs/kit": "next",
16
+ "@tailwindcss/forms": "^0.5.3",
17
+ "@tailwindcss/line-clamp": "^0.4.2",
18
+ "@types/cookie": "^0.5.1",
19
+ "@types/d3-color": "^3.1.0",
20
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
21
+ "@typescript-eslint/parser": "^5.27.0",
22
+ "autoprefixer": "^10.4.11",
23
+ "d3-color": "^3.1.0",
24
+ "eslint": "^8.16.0",
25
+ "eslint-config-prettier": "^8.3.0",
26
+ "eslint-plugin-svelte3": "^4.0.0",
27
+ "postcss": "^8.4.16",
28
+ "prettier": "^2.6.2",
29
+ "prettier-plugin-svelte": "^2.7.0",
30
+ "svelte": "^3.46.0",
31
+ "svelte-check": "^2.7.1",
32
+ "svelte-preprocess": "^4.10.7",
33
+ "tailwindcss": "^3.1.8",
34
+ "tslib": "^2.3.1",
35
+ "typescript": "^4.7.4",
36
+ "vite": "^3.1.0"
37
+ },
38
+ "type": "module",
39
+ "dependencies": {
40
+ "@fontsource/fira-mono": "^4.5.0",
41
+ "extract-colors": "^1.1.22",
42
+ "quantize": "^1.0.2",
43
+ "uninstall": "^0.0.0"
44
+ }
45
+ }
frontend/postcss.config.cjs ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ module.exports = {
2
+ plugins: {
3
+ tailwindcss: {},
4
+ autoprefixer: {},
5
+ },
6
+ }
frontend/src/app.css ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
frontend/src/app.d.ts ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ // See https://kit.svelte.dev/docs/types#app
2
+ // for information about these interfaces
3
+ // and what to do when importing types
4
+ declare namespace App {
5
+ // interface Locals {}
6
+ // interface PageData {}
7
+ // interface PageError {}
8
+ // interface Platform {}
9
+ }
frontend/src/app.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <link rel="icon" href="%sveltekit.assets%/favicon.png" />
6
+ <meta name="viewport" content="width=device-width" />
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>
8
+ %sveltekit.head%
9
+ </head>
10
+ <body class="dark:bg-[rgb(11,15,25)] bg-white dark:text-white text-black">
11
+ <div>%sveltekit.body%</div>
12
+ </body>
13
+ </html>
frontend/src/lib/ColorPalette.svelte ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { Color } from 'd3-color';
3
+
4
+ export let colors: Color[];
5
+ const n = colors.length;
6
+ </script>
7
+
8
+ <div class="flex flex-col items-center">
9
+ <div class="flex bg-black">
10
+ {#each colors as color}
11
+ <div class="aspect-square">
12
+ <svg class="max-w-full" width="100" viewBox="0 0 50 50">
13
+ <rect x="0" y="0" width="50" height="50" fill={color.hex()} />
14
+ </svg>
15
+ </div>
16
+ {/each}
17
+ </div>
18
+ </div>
frontend/src/lib/Palette.svelte ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import * as d3 from 'd3-color';
3
+ import type { ColorsPrompt } from '$lib/types';
4
+ import ColorPalette from '$lib/ColorPalette.svelte';
5
+
6
+ export let promptData: ColorsPrompt;
7
+ console.log(promptData);
8
+ let seletecdImage = 0;
9
+ $: prompt = promptData.prompt;
10
+ $: colors = promptData.images[seletecdImage].colors.map((e) => d3.rgb(e));
11
+ $: imageSrc = promptData.images[seletecdImage].imgURL;
12
+ </script>
13
+
14
+ <div class="grid grid-cols-5">
15
+ <blockquote
16
+ class="col-span-3 italic font-semibold max-w-prose text-base text-center line-clamp-3 my-3"
17
+ title={prompt}
18
+ >
19
+ <p>{prompt}</p>
20
+ </blockquote>
21
+
22
+ <div class="row-start-2 col-span-3 flex items-center justify-center">
23
+ <ColorPalette {colors} />
24
+ </div>
25
+ <div class="row-start-2 col-start-4 ml-auto">
26
+ <div class="relative">
27
+ <img class="relative max-w-[100px]" src={imageSrc} alt={prompt} />
28
+ <div class="absolute flex justify-around w-full">
29
+ {#each promptData.images as image, i}
30
+ <button
31
+ class="{seletecdImage === i
32
+ ? 'bg-black dark:bg-white'
33
+ : 'bg-white dark:bg-black'} dark:bg-slate-300 rounded-full h-3 w-3 m-1 border border-black dark:border-white"
34
+ on:click={() => (seletecdImage = i)}
35
+ on:mousemove={() => (seletecdImage = i)}
36
+ />
37
+ {/each}
38
+ </div>
39
+ </div>
40
+ </div>
41
+
42
+ <div class="row-start-2 col-start-5 flex items-center justify-center">
43
+ <button
44
+ class="bg-black text-white border-2 dark:border-white border-black rounded-2xl ml-2 p-3 shadow-sm font-bold focus:outline-none focus:border-gray-400"
45
+ >
46
+ Remix
47
+ </button>
48
+ </div>
49
+ </div>
frontend/src/lib/store.ts ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ import { writable } from 'svelte/store';
2
+ export const loadingState = writable<string>('');
3
+ export const isLoading = writable<boolean>(false);
frontend/src/lib/types.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Color } from 'd3-color';
2
+
3
+ export interface ColorsImage {
4
+ colors: Color[];
5
+ imgURL: string;
6
+ }
7
+ export interface ColorsPrompt {
8
+ prompt: string;
9
+ images: ColorsImage[];
10
+ }
11
+
12
+ export interface LoadingState {
13
+ loadingMessage: string;
14
+ isLoading: boolean;
15
+ }
frontend/src/lib/utils.ts ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import quantize from 'quantize';
2
+ import * as d3 from 'd3-color';
3
+ import type { Color } from 'd3-color';
4
+ import { dev } from '$app/environment';
5
+
6
+ export function randomSeed() {
7
+ return BigInt(13248873089935215612 & (((1 << 63) - 1) * Math.random()));
8
+ }
9
+
10
+ function sortColors(colors: Color[]): Color[] {
11
+ const reverse = true;
12
+ return colors
13
+ .map((color) => d3.hcl(color))
14
+ .sort((a, b) => {
15
+ console.log(a);
16
+ const aa = a.h;
17
+ const bb = b.h;
18
+
19
+ return !reverse ? aa - bb || isNaN(aa) - isNaN(bb) : bb - aa || isNaN(bb) - isNaN(aa);
20
+ });
21
+ }
22
+
23
+ function createPixelArray(imgData: Uint8ClampedArray, pixelCount: number, quality: number) {
24
+ // from https://github.com/lokesh/color-thief
25
+ const pixels = imgData;
26
+ const pixelArray = [];
27
+
28
+ for (let i = 0, offset, r, g, b, a; i < pixelCount; i = i + quality) {
29
+ offset = i * 4;
30
+ r = pixels[offset + 0];
31
+ g = pixels[offset + 1];
32
+ b = pixels[offset + 2];
33
+ a = pixels[offset + 3];
34
+
35
+ // If pixel is mostly opaque and not white
36
+ if (typeof a === 'undefined' || a >= 125) {
37
+ if (!(r > 250 && g > 250 && b > 250)) {
38
+ pixelArray.push([r, g, b]);
39
+ }
40
+ }
41
+ }
42
+ return pixelArray;
43
+ }
44
+
45
+ export function extractPalette(
46
+ base64image: string,
47
+ colorCount = 5,
48
+ quality = 1
49
+ ): Promise<{ colors: Color[]; imgBlob: Blob }> {
50
+ return new Promise((resolve) => {
51
+ const img = new Image();
52
+ img.onload = async () => {
53
+ const w = img.width;
54
+ const h = img.height;
55
+ const canvas: HTMLCanvasElement = document.createElement('canvas');
56
+ canvas.width = w;
57
+ canvas.height = h;
58
+ const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
59
+ ctx.drawImage(img, 0, 0, w, h);
60
+ const imageData = ctx.getImageData(0, 0, w, h);
61
+ const pixelArray = createPixelArray(imageData.data, w * h, quality);
62
+ const cmap = quantize(pixelArray, colorCount);
63
+ const colors: number[][] = cmap.palette();
64
+
65
+ const imgBlob: Blob = await new Promise((r) => canvas.toBlob(r, 'image/jpeg', 0.95));
66
+ const colorsRGB = colors.map((color) => d3.rgb(...(color as [number, number, number])));
67
+ resolve({
68
+ colors: sortColors(colorsRGB),
69
+ imgBlob
70
+ });
71
+ };
72
+ img.src = base64image;
73
+ });
74
+ }
75
+
76
+ export async function uploadImage(imagBlob: Blob, prompt: string): string {
77
+ // simple regex slugify string for file name
78
+ const promptSlug = slugify(prompt);
79
+ const UPLOAD_URL = dev ? 'moon/uploads' : 'https://huggingface.co/uploads';
80
+
81
+ const hash = crypto.randomUUID().split('-')[0];
82
+ const fileName = `color-palette-${hash}-${promptSlug}.jpeg`;
83
+
84
+ const file = new File([imagBlob], fileName, { type: 'image/jpeg' });
85
+
86
+ console.log('uploading image', file);
87
+
88
+ const response = await fetch(UPLOAD_URL, {
89
+ method: 'POST',
90
+ headers: {
91
+ 'Content-Type': file.type,
92
+ 'X-Requested-With': 'XMLHttpRequest'
93
+ },
94
+ body: file /// <- File inherits from Blob
95
+ });
96
+ const url = await response.text();
97
+
98
+ console.log('uploaded images', url);
99
+ return url;
100
+ }
101
+
102
+ function slugify(text: string) {
103
+ if (!text) return '';
104
+ return text
105
+ .toString()
106
+ .toLowerCase()
107
+ .replace(/\s+/g, '-')
108
+ .replace(/[^\w\-]+/g, '')
109
+ .replace(/\-\-+/g, '-')
110
+ .replace(/^-+/, '')
111
+ .replace(/-+$/, '');
112
+ }
frontend/src/routes/+layout.svelte ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ <script>
2
+ import '../app.css';
3
+ </script>
4
+
5
+ <slot />
frontend/src/routes/+layout.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ export const prerender = true;
frontend/src/routes/+page.svelte ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount } from 'svelte';
3
+
4
+ import type { ColorsPrompt, ColorsImage } from '$lib/types';
5
+ import { dev } from '$app/environment';
6
+ import { randomSeed, extractPalette, uploadImage } from '$lib/utils';
7
+ import { isLoading, loadingState } from '$lib/store';
8
+ import { PUBLIC_WS_ENDPOINT } from '$env/static/public';
9
+
10
+ import Pallette from '$lib/Palette.svelte';
11
+ const apiUrl = dev
12
+ ? 'http://localhost:7860/static/'
13
+ : '/embed/huggingface-projects/color-palette-generator-sd/static/';
14
+
15
+ let promptsData: ColorsPrompt[];
16
+ let prompt: string;
17
+
18
+ onMount(async () => {
19
+ fetch(apiUrl + 'data.json')
20
+ .then((d) => d.json())
21
+ .then((data) => (promptsData = data));
22
+ });
23
+
24
+ let cc = 0;
25
+
26
+ export async function generatePalette(_prompt: string) {
27
+ if (!_prompt || $isLoading == true) return;
28
+ $loadingState = 'Pending';
29
+ $isLoading = true;
30
+ const hash = crypto.randomUUID();
31
+ const payload = {
32
+ fn_index: 2,
33
+ data: [
34
+ // prompt, samples, steps, scale, seed
35
+ _prompt,
36
+ 3,
37
+ 40,
38
+ 7.5,
39
+ Number(randomSeed())
40
+ ]
41
+ };
42
+ const websocket = new WebSocket(PUBLIC_WS_ENDPOINT);
43
+ websocket.onopen = async function (event) {
44
+ console.log(event);
45
+ websocket.send(JSON.stringify({ hash: hash }));
46
+ };
47
+ websocket.onclose = (evt) => {
48
+ if (!evt.wasClean) {
49
+ $loadingState = 'Error';
50
+ $isLoading = false;
51
+ }
52
+ };
53
+ websocket.onmessage = async function (event) {
54
+ try {
55
+ const data = JSON.parse(event.data);
56
+ console.log(data);
57
+ $loadingState = '';
58
+ switch (data.msg) {
59
+ case 'send_data':
60
+ $loadingState = 'Sending Data';
61
+ websocket.send(JSON.stringify(payload));
62
+ break;
63
+ case 'queue_full':
64
+ $loadingState = 'Queue full';
65
+ websocket.close();
66
+ $isLoading = false;
67
+ return;
68
+ case 'estimation':
69
+ const { msg, rank, queue_size } = data;
70
+ $loadingState = `${msg} ${rank}/${queue_size}`;
71
+ break;
72
+ case 'process_generating':
73
+ $loadingState = data.success ? 'Generating' : 'Error';
74
+ break;
75
+ case 'process_completed':
76
+ $loadingState = data.success ? 'Complete' : 'Error';
77
+ console.log(data.output);
78
+ const images = await extractColorsImages(data.output.data[0], _prompt);
79
+ promptsData = [
80
+ {
81
+ prompt: _prompt,
82
+ images
83
+ }
84
+ ].concat(promptsData);
85
+ websocket.close();
86
+ $isLoading = false;
87
+ return;
88
+ case 'process_starts':
89
+ $loadingState = 'Process Starts';
90
+ break;
91
+ }
92
+ } catch (e) {
93
+ console.log(e);
94
+ $isLoading = false;
95
+ $loadingState = 'Error';
96
+ }
97
+ };
98
+ }
99
+ async function extractColorsImages(images: string[], _prompt: string): Promise<ColorsImage[]> {
100
+ const colorImages = [];
101
+ for (const base64img of images) {
102
+ const { colors, imgBlob } = await extractPalette(base64img);
103
+ const url = await uploadImage(imgBlob, _prompt);
104
+ const colorsImage: ColorsImage = {
105
+ colors,
106
+ imgURL: url
107
+ };
108
+ colorImages.push(colorsImage);
109
+ }
110
+ return colorImages;
111
+ }
112
+ </script>
113
+
114
+ <div class="max-w-screen-lg mx-auto px-3 py-8 relative z-0">
115
+ <div class="relative">
116
+ <form class="grid grid-cols-5" on:submit|preventDefault={() => generatePalette(prompt)}>
117
+ <input
118
+ class="disabled:opacity-50 col-span-4 flex-grow italic dark:placeholder:text-black placeholder:text-white text-white dark:text-black placeholder:text-opacity-40 dark:bg-white bg-slate-900 border-2 border-black rounded-2xl p-3 shadow-sm focus:outline-none focus:border-gray-400 focus:ring-1 "
119
+ placeholder="A photo of a beautiful sunset in San Francisco"
120
+ type="text"
121
+ name="prompt"
122
+ bind:value={prompt}
123
+ disabled={$isLoading}
124
+ />
125
+ <button
126
+ class="disabled:opacity-50 flex-1 dark:bg-white dark:text-black border-2 border-black rounded-2xl ml-2 p-3 px-6 shadow-sm font-bold focus:outline-none focus:border-gray-400"
127
+ on:click|preventDefault={() => generatePalette(prompt)}
128
+ disabled={$isLoading}
129
+ >
130
+ Create Palette
131
+ </button>
132
+ </form>
133
+ {#if $loadingState}
134
+ <h3 class="text-xs font-bold ml-3">{$loadingState}</h3>
135
+ {/if}
136
+ </div>
137
+
138
+ {#if promptsData}
139
+ <div class="pt-5">
140
+ {#each promptsData as promptData}
141
+ <Pallette {promptData} />
142
+ {/each}
143
+ </div>
144
+ {/if}
145
+ </div>
frontend/static/data.json ADDED
@@ -0,0 +1 @@
 
 
1
+ [{"prompt": "a painting of a Ferrari by Claude Monet", "images": [{"colors": ["#2f3c6e", "#deacdf", "#e2e23b", "#b923bf", "#ed68f7"], "imgURL": "static/images/00045-2327336162-a painting of a Ferrari by Claude Monet.jpg"}, {"colors": ["#f9b9a0", "#b30814", "#36102d", "#e240ab", "#680c26"], "imgURL": "static/images/00044-2327336161-a painting of a Ferrari by Claude Monet.jpg"}, {"colors": ["#13bbab", "#f7e2ba", "#cb3b37", "#062726", "#055637"], "imgURL": "static/images/00046-2327336163-a painting of a Ferrari by Claude Monet.jpg"}]}, {"prompt": "a photo of a beautiful sunset in San Francisco", "images": [{"colors": ["#e2dcf8", "#d47a0d", "#50244c", "#b63547", "#f270f2"], "imgURL": "static/images/00003-3329858470-a photo of a beautiful sunset in San Francisco.jpg"}, {"colors": ["#177405", "#d6f12d", "#7c1a04", "#b9a5a4", "#6ae61b"], "imgURL": "static/images/00001-3329858468-a photo of a beautiful sunset in San Francisco.jpg"}, {"colors": ["#766dae", "#4e2806", "#e8b878", "#1c317e", "#46c21e"], "imgURL": "static/images/00000-3329858467-a photo of a beautiful sunset in San Francisco.jpg"}]}, {"prompt": "a photo of a elmo drinking at starbucks", "images": [{"colors": ["#179209", "#eee84b", "#571208", "#b91806", "#537c91"], "imgURL": "static/images/00036-2824881702-a photo of a elmo drinking at starbucks.jpg"}, {"colors": ["#d62fec", "#281185", "#f2bbde", "#db2f23", "#8505e1"], "imgURL": "static/images/00035-2824881701-a photo of a elmo drinking at starbucks.jpg"}, {"colors": ["#19550f", "#a80e05", "#f5ce1f", "#f7d9cf", "#859721"], "imgURL": "static/images/00037-2824881703-a photo of a elmo drinking at starbucks.jpg"}]}, {"prompt": "a photo of a polar bear in the Arctic", "images": [{"colors": ["#4cdde5", "#e755ce", "#2d2a64", "#26c81d", "#d8edc5"], "imgURL": "static/images/00010-3082979826-a photo of a polar bear in the Arctic.jpg"}, {"colors": ["#66bdda", "#071ed8", "#04111b", "#1f3f78", "#7c839b"], "imgURL": "static/images/00009-3082979825-a photo of a polar bear in the Arctic.jpg"}, {"colors": ["#c4efd0", "#194286", "#6591b7", "#7e9f52", "#576a3d"], "imgURL": "static/images/00011-3082979827-a photo of a polar bear in the Arctic.jpg"}]}, {"prompt": "a paint of a polar bear in the Arctic painted by Vincent van Gogh", "images": [{"colors": ["#3de9f9", "#172eb8", "#dc44d6", "#1bd40d", "#e7d9e8"], "imgURL": "static/images/00049-3943684507-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg"}, {"colors": ["#f984fb", "#35208b", "#a9040f", "#d209ef", "#eed5f9"], "imgURL": "static/images/00050-3943684508-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg"}, {"colors": ["#143153", "#ebf521", "#7c3117", "#a8c9f2", "#66a70a"], "imgURL": "static/images/00048-3943684506-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg"}]}, {"prompt": "a photo of a polar bear in the zoo", "images": [{"colors": ["#cd1ce0", "#f397ec", "#980d30", "#3e231a", "#713789"], "imgURL": "static/images/00026-1171300814-a photo of a polar bear in the zoo.jpg"}, {"colors": ["#1f15dc", "#f6c3fb", "#92edfa", "#1d2824", "#de30f2"], "imgURL": "static/images/00013-2991374000-a photo of a polar bear in the zoo.jpg"}, {"colors": ["#0512dc", "#ceebf5", "#0c2726", "#55acb6", "#8c61fc"], "imgURL": "static/images/00027-1171300815-a photo of a polar bear in the zoo.jpg"}]}]
frontend/static/favicon.png ADDED
frontend/static/images/00000-3329858467-a photo of a beautiful sunset in San Francisco.jpg ADDED
frontend/static/images/00001-3329858468-a photo of a beautiful sunset in San Francisco.jpg ADDED
frontend/static/images/00003-3329858470-a photo of a beautiful sunset in San Francisco.jpg ADDED
frontend/static/images/00009-3082979825-a photo of a polar bear in the Arctic.jpg ADDED
frontend/static/images/00010-3082979826-a photo of a polar bear in the Arctic.jpg ADDED
frontend/static/images/00011-3082979827-a photo of a polar bear in the Arctic.jpg ADDED
frontend/static/images/00013-2991374000-a photo of a polar bear in the zoo.jpg ADDED
frontend/static/images/00026-1171300814-a photo of a polar bear in the zoo.jpg ADDED
frontend/static/images/00027-1171300815-a photo of a polar bear in the zoo.jpg ADDED
frontend/static/images/00035-2824881701-a photo of a elmo drinking at starbucks.jpg ADDED
frontend/static/images/00036-2824881702-a photo of a elmo drinking at starbucks.jpg ADDED
frontend/static/images/00037-2824881703-a photo of a elmo drinking at starbucks.jpg ADDED
frontend/static/images/00044-2327336161-a painting of a Ferrari by Claude Monet.jpg ADDED
frontend/static/images/00045-2327336162-a painting of a Ferrari by Claude Monet.jpg ADDED
frontend/static/images/00046-2327336163-a painting of a Ferrari by Claude Monet.jpg ADDED
frontend/static/images/00048-3943684506-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg ADDED
frontend/static/images/00049-3943684507-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg ADDED
frontend/static/images/00050-3943684508-a paint of a polar bear in the Arctic painted by Vincent van Gogh.jpg ADDED
frontend/static/robots.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # https://www.robotstxt.org/robotstxt.html
2
+ User-agent: *
3
+ Disallow:
frontend/static/svelte-welcome.png ADDED
frontend/static/svelte-welcome.webp ADDED