File size: 4,321 Bytes
32cd713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Utility functions for the image generation application.

This module provides helper functions for tasks like image saving,
timestamp generation, and other common operations.
"""

import os
import time
from datetime import datetime
from pathlib import Path
from typing import Optional

import gradio as gr
from PIL import Image

# Create output directory for saved images
OUTPUTS_DIR = Path("outputs")
OUTPUTS_DIR.mkdir(exist_ok=True)


def save_image(image: Image.Image, prompt: str) -> str:
    """
    Save the generated image to disk with a filename based on timestamp and prompt.
    
    Args:
        image: The PIL Image to save
        prompt: The prompt used to generate the image
        
    Returns:
        Path to the saved image
    """
    if image is None:
        return ""
        
    # Create a filename from the timestamp and a shortened version of the prompt
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    # Clean prompt for filename use (first 20 chars, alphanumeric only)
    clean_prompt = "".join(c for c in prompt if c.isalnum() or c.isspace())[:20].strip()
    clean_prompt = clean_prompt.replace(" ", "_")
    
    filename = f"{timestamp}_{clean_prompt}.png"
    filepath = OUTPUTS_DIR / filename
    
    # Save the image
    image.save(filepath)
    return str(filepath)


def format_generation_info(
    prompt: str, 
    negative_prompt: str, 
    seed: int, 
    width: int, 
    height: int, 
    guidance_scale: float, 
    steps: int
) -> str:
    """
    Format generation parameters into a readable string.
    
    Args:
        prompt: Text prompt used
        negative_prompt: Negative prompt used
        seed: Random seed used
        width: Image width
        height: Image height
        guidance_scale: Guidance scale value
        steps: Number of inference steps
        
    Returns:
        Formatted string with generation parameters
    """
    info = f"**Prompt:** {prompt}\n"
    if negative_prompt:
        info += f"**Negative prompt:** {negative_prompt}\n"
    info += f"**Seed:** {seed}\n"
    info += f"**Size:** {width}x{height}\n"
    info += f"**Guidance scale:** {guidance_scale}\n"
    info += f"**Steps:** {steps}\n"
    return info


class GenerationHistory:
    """Manages a history of generated images and their parameters."""
    
    def __init__(self, max_history: int = 10):
        """
        Initialize the generation history.
        
        Args:
            max_history: Maximum number of items to keep in history
        """
        self.history = []
        self.max_history = max_history
        
    def add(
        self, 
        image: Image.Image, 
        prompt: str, 
        negative_prompt: str, 
        seed: int, 
        width: int, 
        height: int, 
        guidance_scale: float, 
        steps: int
    ) -> None:
        """
        Add a new generation to the history.
        
        Args:
            image: Generated image
            prompt: Text prompt used
            negative_prompt: Negative prompt used
            seed: Random seed used
            width: Image width
            height: Image height
            guidance_scale: Guidance scale value
            steps: Number of inference steps
        """
        if image is None:
            return
            
        # Create entry with all relevant information
        entry = {
            "image": image,
            "prompt": prompt,
            "negative_prompt": negative_prompt,
            "seed": seed,
            "width": width,
            "height": height,
            "guidance_scale": guidance_scale,
            "steps": steps,
            "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }
        
        # Add to history and maintain max size
        self.history.append(entry)
        if len(self.history) > self.max_history:
            self.history.pop(0)
            
    def get_latest(self, n: int = 1) -> list:
        """
        Get the latest n entries from history.
        
        Args:
            n: Number of entries to retrieve
            
        Returns:
            List of history entries
        """
        return self.history[-n:] if self.history else []
        
    def clear(self) -> None:
        """Clear the generation history."""
        self.history = []