Skip to content

Commit

Permalink
Add support for showing messages during generation
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed May 5, 2023
1 parent db77f56 commit e8113d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
28 changes: 26 additions & 2 deletions api/models/generation_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,38 @@ class GenerationResult:
```python
result = GenerationResult(
progress=3,
total=5,
image=np.zeros((512, 512, 3)),
seed=42
)
```
Alternatively, create a result with just a `title` and progress values.
```python
result = GenerationResult(
progress=3,
total=5,
title="Loading model"
)
```
"""
image: NDArray

progress: int
"""The amount out of `total` that has been completed"""

total: int
"""The number of steps to complete"""

title: str | None = None
"""The name of the currently executing task"""

image: NDArray | None = None
"""The generated image as a Numpy array.
The shape should be `(height, width, channels)`, where `channels` is 3 or 4.
"""
seed: int

seed: int | None = None
"""The seed used to generate the image."""
4 changes: 2 additions & 2 deletions diffusers_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ def generate(self, task: Task, model: Model, prompt: Prompt, size: Tuple[int, in
case _:
raise NotImplementedError()
def on_step(_, step_image: ImageGenerationResult):
step_callback(GenerationResult(image=step_image.images[-1], seed=step_image.seeds[-1]))
step_callback(GenerationResult(progress=step_image.step, total=steps, image=step_image.images[-1], seed=step_image.seeds[-1]))
def on_done(future: Future):
result: ImageGenerationResult = future.result(last_only=True)
callback([
GenerationResult(image=result.images[i], seed=result.seeds[i])
GenerationResult(progress=result.step, total=steps, image=result.images[i], seed=result.seeds[i])
for i in range(len(result.images))
])
def on_exception(_, exception):
Expand Down
17 changes: 11 additions & 6 deletions operators/dream_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ def step_progress_update(self, context):
execution_start = time.time()
def step_callback(progress: api.GenerationResult):
nonlocal last_data_block
scene.dream_textures_last_execution_time = f"{time.time() - execution_start:.2f} seconds"
scene.dream_textures_progress = scene.dream_textures_progress + 1
last_data_block = bpy_image(f"Step {scene.dream_textures_progress}/{prompt.steps}", progress.image.shape[1], progress.image.shape[0], progress.image.ravel(), last_data_block)
for area in screen.areas:
if area.type == 'IMAGE_EDITOR':
area.spaces.active.image = last_data_block
scene.dream_textures_last_execution_time = progress.title or f"{time.time() - execution_start:.2f} seconds"
bpy.types.Scene.dream_textures_progress = bpy.props.IntProperty(name="", min=0, max=progress.total, update=step_progress_update)
scene.dream_textures_progress = progress.progress
if progress.image is not None:
last_data_block = bpy_image(f"Step {scene.dream_textures_progress}/{prompt.steps}", progress.image.shape[1], progress.image.shape[0], progress.image.ravel(), last_data_block)
for area in screen.areas:
if area.type == 'IMAGE_EDITOR':
area.spaces.active.image = last_data_block

def callback(result: List[api.GenerationResult] | Exception):
if isinstance(result, Exception):
Expand All @@ -90,6 +92,9 @@ def callback(result: List[api.GenerationResult] | Exception):
else:
nonlocal last_data_block
for i, generation in enumerate(result):
if generation.image is None or generation.seed is None:
continue

# Create a trimmed image name
prompt_string = context.scene.dream_textures_prompt.prompt_structure_token_subject
seed_str_length = len(str(generation.seed))
Expand Down

0 comments on commit e8113d0

Please sign in to comment.