File size: 4,175 Bytes
e0336bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 11 19:08:55 2025

@author: blyss
"""
import os
import json


class SingletonMeta(type):
    """
    The SingletonMeta class is useful for creating objects that persist as a single instance across the whole program. Basically a global class.
    """
    _instances = {}

    def __call__(cls, *Parameters, **kwParameters):
        if cls not in cls._instances:
            cls._instances[cls] = super(SingletonMeta, cls).__call__(*Parameters, **kwParameters)
        return cls._instances[cls]


class BlissfulSettings(metaclass=SingletonMeta):
    def __init__(self):
        """
        Loads the settings from a 'settings.json' file, creating it with default settings if it doesn't exist.

        This method attempts to read the program's settings from a JSON file. If the file does not exist,
        it creates a new file with default settings. This ensures that the program can start with a known
        set of configurations and modify them as needed.

        This class is a SingletonMeta so even if we reinstantiate the class, this only happens the first time
        """
        # These are globals that do not persist
        self.generating = 0
        self.last_preview_file = ""

        default_settings = {
            "prompt": "a cat walks on the grass, realistic style",
            "resolution_x": 960,
            "resolution_y": 544,
            "fps": 24,
            "embedded_guidance": 6.0,
            "flow_shift": 7.0,
            "infer_steps": 50,
            "seed": 42,
            "video_length": 129,
            "attention": "sage",
            "blocks_to_swap": 0,
            "hidden_state_skip_layer": 2,
            "apply_final_norm": False,
            "reproduce": False,
            "fp8": True,
            "fp8_fast": False,
            "do_compile": False,
            "transformer_path": "",
            "text_encoder_1_path": "",
            "text_encoder_2_path": "",
            "vae_path": "",
            "lora_path": "",
        }

        if not os.path.exists("./settings.json"):
            with open("./settings.json", "w", encoding="utf-8") as file:
                json.dump(default_settings, file, indent=4)
            print("No existing settings found. Created default settings file.")

        with open("./settings.json", "r", encoding="utf-8") as file:
            data = json.load(file)

        for key, default_value in default_settings.items():
            setattr(self, key, data.get(key, default_value))

    def save_to_file(self):
        """
        Saves the current settings to a JSON file named 'settings.json'.
        """
        settings = {
            "prompt": self.prompt,
            "resolution_x": self.resolution_x,
            "resolution_y": self.resolution_y,
            "fps": self.fps,
            "embedded_guidance": self.embedded_guidance,
            "flow_shift": self.flow_shift,
            "infer_steps": self.infer_steps,
            "seed": self.seed,
            "video_length": self.video_length,
            "attention": self.attention,
            "blocks_to_swap": self.blocks_to_swap,
            "hidden_state_skip_layer": self.hidden_state_skip_layer,
            "apply_final_norm": self.apply_final_norm,
            "reproduce": self.reproduce,
            "fp8": self.fp8,
            "fp8_fast": self.fp8_fast,
            "do_compile": self.do_compile,
            "transformer_path": self.transformer_path,
            "text_encoder_1_path": self.text_encoder_1_path,
            "text_encoder_2_path": self.text_encoder_2_path,
            "vae_path": self.vae_path,
            "lora_path": self.lora_path,
        }

        with open("./settings.json", "w", encoding="utf-8") as file:
            json.dump(settings, file, indent=4)

    def update(self, option, value, label_target=None, label_value=None):
        """Method for updating various settings called via QT connection and may update an associated label/value"""
        setattr(self, option, value)
        if label_target is not None and label_value is not None:
            label_target.setText(str(label_value))