File size: 3,378 Bytes
7a1d414
 
 
 
 
 
a8704d0
7a1d414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bc957e
7a1d414
9bc957e
 
 
 
7a1d414
 
9bc957e
 
 
7a1d414
 
 
 
 
a8704d0
7a1d414
9bc957e
 
 
 
 
 
a8704d0
 
9bc957e
 
 
 
 
 
 
 
 
 
 
7a1d414
9bc957e
 
7a1d414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
"""Configuration dataclasses for Stable Diffusion execution and history storage."""

from __future__ import annotations

from dataclasses import asdict, dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional


@dataclass
class Txt2ImgConfig:
    """Configuration for text-to-image generation.

    Attributes:
        prompt: Positive prompt text.
        negative_prompt: Negative prompt text.
        steps: Number of diffusion steps.
        guidance_scale: Classifier-free guidance scale.
        width: Requested image width.
        height: Requested image height.
        seed: Optional random seed.
        device: Target torch device ("cuda" or "cpu").
    """

    prompt: str
    negative_prompt: str = ""
    steps: int = 30
    guidance_scale: float = 7.5
    width: int = 512
    height: int = 512
    seed: Optional[int] = None
    device: str = "cuda"


@dataclass
class Img2ImgConfig:
    """Configuration for image-to-image generation.

    Attributes:
        prompt: Positive prompt text.
        init_image_path: Optional file path to source image.
        negative_prompt: Negative prompt text.
        strength: Img2Img blend strength in (0, 1].
        steps: Number of diffusion steps.
        guidance_scale: CFG scale.
        width: Requested image width.
        height: Requested image height.
        seed: Optional random seed.
        device: Target device.
    """

    prompt: str
    init_image_path: Optional[str] = None
    negative_prompt: str = ""
    strength: float = 0.7
    steps: int = 30
    guidance_scale: float = 7.5
    width: int = 512
    height: int = 512
    seed: Optional[int] = None
    device: str = "cuda"


@dataclass
class GenerationMetadata:
    """Generic metadata for any generation mode.

    Fields are optional depending on mode:
    - Txt2Img: prompt, negative, steps, guidance
    - Img2Img: prompt, negative, strength, steps, guidance
    - Upscale: scale, original size, final size
    """

    mode: str  # "txt2img", "img2img", "upscale"

    # Shared
    elapsed_seconds: float = 0.0
    timestamp: str = field(default_factory=lambda: datetime.utcnow().isoformat())
    id: Optional[str] = None
    thumbnail: Optional[str] = None
    full_image: Optional[str] = None
    model_id: Optional[str] = None

    # Txt2Img / Img2Img
    prompt: Optional[str] = None
    negative_prompt: Optional[str] = None
    steps: Optional[int] = None
    guidance_scale: Optional[float] = None
    seed: Optional[int] = None
    lora_names: Optional[List[str]] = None
    lora_alphas: Optional[List[float]] = None

    # Img2Img only
    strength: Optional[float] = None

    # Upscale only
    scale: Optional[float] = None
    original_width: Optional[int] = None
    original_height: Optional[int] = None
    width: Optional[int] = None
    height: Optional[int] = None

    def to_dict(self) -> Dict[str, Any]:
        """Drop None values for clean JSON."""
        return {k: v for k, v in asdict(self).items() if v is not None}


@dataclass
class HistorySummary:
    """Minimal entry used for UI history lists."""

    id: str
    prompt: str
    mode: str
    seed: Optional[int]
    width: int
    height: int
    timestamp: str
    thumbnail: str

    def to_dict(self) -> Dict[str, Any]:
        """Return a serializable dict representation."""
        return asdict(self)