import numpy as np
import pickle
import application.api.utils as utils
from typing import List
from application.api.generate import Generate


def generate_pca_image(
        generator: Generate,
        components_multiplier: List,
        seed: int
) -> str:
    components_multiplier = np.array(components_multiplier)
    zero_array = np.zeros(shape=[100])
    components_multiplier = np.concatenate([components_multiplier, zero_array[components_multiplier.shape[0]:]],
                                           axis=-1)
    latent = utils.get_random_latent(seed)
    dlatent = generator.mapping_without_labels(latent)
    with open('../../1_control_without_labels/02_pca/w_pca_100_components_1000000_iterations.pkl', 'rb') as f:
        components, mean = pickle.load(f)
        dlatent += np.sum(components * np.expand_dims(components_multiplier, axis=-1), axis=0)

    output = generator.generate_images_from_dlatents(dlatent)[0]
    return utils.convert_to_base64_str(output)

import numpy as np
import pickle
import application.api.utils as utils
from typing import List
from application.api.generate import Generate


def generate_pca_image(
        generator: Generate,
        components_multiplier: List,
        seed: int
) -> str:
    components_multiplier = np.array(components_multiplier)
    zero_array = np.zeros(shape=[100])
    components_multiplier = np.concatenate([components_multiplier, zero_array[components_multiplier.shape[0]:]],
                                           axis=-1)
    latent = utils.get_random_latent(seed)
    dlatent = generator.mapping_without_labels(latent)
    with open('../../1_control_without_labels/02_pca/w_pca_100_components_1000000_iterations.pkl', 'rb') as f:
        components, mean = pickle.load(f)
        dlatent += np.sum(components * np.expand_dims(components_multiplier, axis=-1), axis=0)

    output = generator.generate_images_from_dlatents(dlatent)[0]
    return utils.convert_to_base64_str(output)
