Commit
·
f4fb2f2
1
Parent(s):
6034b35
use the model
Browse files- requirements.txt +1 -0
- utils/color_model.py +35 -40
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
gradio
|
| 2 |
numpy
|
| 3 |
matplotlib
|
|
|
|
| 4 |
huggingface_hub
|
| 5 |
|
|
|
|
| 1 |
gradio
|
| 2 |
numpy
|
| 3 |
matplotlib
|
| 4 |
+
tensorflow
|
| 5 |
huggingface_hub
|
| 6 |
|
utils/color_model.py
CHANGED
|
@@ -1,60 +1,55 @@
|
|
| 1 |
-
import json
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
-
# Download
|
| 6 |
-
|
| 7 |
repo_id="danielritchie/vibe-color-model",
|
| 8 |
-
filename="
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Extract layers
|
| 15 |
-
W0 = np.array(weights["layer_0"]["weights"], dtype=np.float32)
|
| 16 |
-
b0 = np.array(weights["layer_0"]["biases"], dtype=np.float32)
|
| 17 |
-
|
| 18 |
-
W1 = np.array(weights["layer_1"]["weights"], dtype=np.float32)
|
| 19 |
-
b1 = np.array(weights["layer_1"]["biases"], dtype=np.float32)
|
| 20 |
-
|
| 21 |
-
W2 = np.array(weights["layer_2"]["weights"], dtype=np.float32)
|
| 22 |
-
b2 = np.array(weights["layer_2"]["biases"], dtype=np.float32)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def relu(x):
|
| 26 |
-
return np.maximum(0, x)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def sigmoid(x):
|
| 30 |
-
return 1 / (1 + np.exp(-x))
|
| 31 |
-
|
| 32 |
|
| 33 |
def infer_color(vad):
|
| 34 |
-
|
| 35 |
vad["V"],
|
| 36 |
vad["A"],
|
| 37 |
vad["D"],
|
| 38 |
vad["Cx"],
|
| 39 |
vad["Co"]
|
| 40 |
-
], dtype=np.float32)
|
| 41 |
-
|
| 42 |
-
# Layer 0
|
| 43 |
-
x = relu(np.dot(x, W0) + b0)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
x = relu(np.dot(x, W1) + b1)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
x = sigmoid(np.dot(x, W2) + b2)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
return {
|
| 54 |
-
"R":
|
| 55 |
-
"G":
|
| 56 |
-
"B":
|
| 57 |
-
"E":
|
| 58 |
-
"I":
|
| 59 |
}
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import tensorflow as tf
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
|
| 5 |
+
# Download .h5 model from HF
|
| 6 |
+
model_path = hf_hub_download(
|
| 7 |
repo_id="danielritchie/vibe-color-model",
|
| 8 |
+
filename="vibe_model.h5"
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Load Keras model
|
| 12 |
+
model = tf.keras.models.load_model(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def infer_color(vad):
|
| 15 |
+
input_data = np.array([[
|
| 16 |
vad["V"],
|
| 17 |
vad["A"],
|
| 18 |
vad["D"],
|
| 19 |
vad["Cx"],
|
| 20 |
vad["Co"]
|
| 21 |
+
]], dtype=np.float32)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
output = model.predict(input_data, verbose=0)[0]
|
|
|
|
| 24 |
|
| 25 |
+
r, g, b, e, i = output
|
|
|
|
| 26 |
|
| 27 |
+
return {
|
| 28 |
+
"R": float(r),
|
| 29 |
+
"G": float(g),
|
| 30 |
+
"B": float(b),
|
| 31 |
+
"E": float(e),
|
| 32 |
+
"I": float(i)
|
| 33 |
+
}
|
| 34 |
|
| 35 |
+
def scale_rgb(rgb):
|
| 36 |
return {
|
| 37 |
+
"R": int(max(0, min(255, rgb["R"] * 255))),
|
| 38 |
+
"G": int(max(0, min(255, rgb["G"] * 255))),
|
| 39 |
+
"B": int(max(0, min(255, rgb["B"] * 255))),
|
| 40 |
+
"E": rgb["E"],
|
| 41 |
+
"I": rgb["I"]
|
| 42 |
}
|
| 43 |
|
| 44 |
+
def render_color(rgb):
|
| 45 |
+
return f"""
|
| 46 |
+
<div style="
|
| 47 |
+
width:100%;
|
| 48 |
+
height:240px;
|
| 49 |
+
border-radius:18px;
|
| 50 |
+
background: rgb({rgb['R']},{rgb['G']},{rgb['B']});
|
| 51 |
+
box-shadow: 0px 6px 32px rgba(0,0,0,0.25);
|
| 52 |
+
transition: all 0.3s ease-in-out;
|
| 53 |
+
"></div>
|
| 54 |
+
"""
|
| 55 |
+
|