danielritchie/generative-affect-engine
0
1import numpy as np2import tensorflow as tf3from huggingface_hub import hf_hub_download4 5# Download .h5 model from HF6model_path = hf_hub_download(7 repo_id="danielritchie/vibe-color-model",8 filename="vibe_model.h5"9)10 11# Load Keras model12model = tf.keras.models.load_model(model_path, compile=False)13 14 15# -------------------------------------------------16# Model Inference17# -------------------------------------------------18 19def infer_color(vad):20 input_data = np.array([[21 vad["V"],22 vad["A"],23 vad["D"],24 vad["Cx"],25 vad["Co"]26 ]], dtype=np.float32)27 28 output = model.predict(input_data, verbose=0)[0]29 r, g, b, e, i = output30 31 return {32 "R": float(r),33 "G": float(g),34 "B": float(b),35 "E": float(e), # Energy36 "I": float(i) # Intensity37 }38 39 40# -------------------------------------------------41# Cinematic Drama Rendering42# -------------------------------------------------43 44def apply_cinematic_blend(model_output, drama):45 """46 drama:47 0.0 → near white48 1.0 → cinematic target49 1.5 → expressive peak50 """51 52 color = np.array([53 model_output["R"],54 model_output["G"],55 model_output["B"]56 ])57 58 energy = model_output["E"]59 intensity = model_output["I"]60 61 white = np.array([1.0, 1.0, 1.0])62 63 # Nonlinear cinematic curve64 curve = drama ** 2.265 66 # Blend strength influenced by model intensity67 strength = np.clip(curve * intensity, 0.0, 1.0)68 69 # Blend toward white baseline70 blended = white * (1 - strength) + color * strength71 72 # Subtle energy-based brightness shaping73 brightness_boost = 0.9 + (0.25 * energy)74 blended = blended * brightness_boost75 76 blended = np.clip(blended, 0.0, 1.0)77 78 return {79 "R": int(blended[0] * 255),80 "G": int(blended[1] * 255),81 "B": int(blended[2] * 255),82 "E": energy,83 "I": intensity84 }85 86 87# -------------------------------------------------88# Render HTML Block89# -------------------------------------------------90 91def render_color(model_output):92 93 # Convert 0–1 floats to 0–255 integers94 r = int(max(0, min(255, model_output["R"] * 255)))95 g = int(max(0, min(255, model_output["G"] * 255)))96 b = int(max(0, min(255, model_output["B"] * 255)))97 98 return f"""99 <div style="100 width:100%;101 height:240px;102 border-radius:18px;103 background: rgb({r},{g},{b});104 box-shadow: 0px 6px 32px rgba(0,0,0,0.25);105 transition: all 0.35s cubic-bezier(.4,0,.2,1);106 "></div>107 """108 109 110 