Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import numpy as np
|
|
| 3 |
import random
|
| 4 |
import spaces
|
| 5 |
import torch
|
|
|
|
|
|
|
| 6 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
|
| 7 |
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
| 8 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
|
@@ -40,7 +42,22 @@ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidan
|
|
| 40 |
):
|
| 41 |
final_img = img
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
examples = [
|
| 46 |
"a tiny astronaut hatching from an egg on the moon",
|
|
@@ -77,6 +94,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 77 |
|
| 78 |
result = gr.Image(label="Result", show_label=False)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
with gr.Accordion("Advanced Settings", open=False):
|
| 81 |
|
| 82 |
seed = gr.Slider(
|
|
@@ -129,7 +154,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 129 |
examples = examples,
|
| 130 |
fn = infer,
|
| 131 |
inputs = [prompt],
|
| 132 |
-
outputs = [result, seed],
|
| 133 |
cache_examples="lazy"
|
| 134 |
)
|
| 135 |
|
|
@@ -137,7 +162,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 137 |
triggers=[run_button.click, prompt.submit],
|
| 138 |
fn = infer,
|
| 139 |
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 140 |
-
outputs = [result, seed]
|
| 141 |
)
|
| 142 |
|
| 143 |
demo.launch()
|
|
|
|
| 3 |
import random
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
+
import os
|
| 7 |
+
import time
|
| 8 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
|
| 9 |
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
| 10 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
|
|
|
| 42 |
):
|
| 43 |
final_img = img
|
| 44 |
|
| 45 |
+
# Save image to /tmp directory and get the path
|
| 46 |
+
if final_img is not None:
|
| 47 |
+
# Create a unique filename with timestamp
|
| 48 |
+
timestamp = int(time.time())
|
| 49 |
+
filename = f"flux_generated_{timestamp}_{seed}.png"
|
| 50 |
+
image_path = os.path.join("/tmp", filename)
|
| 51 |
+
|
| 52 |
+
# Ensure /tmp directory exists (it should by default on Linux)
|
| 53 |
+
os.makedirs("/tmp", exist_ok=True)
|
| 54 |
+
|
| 55 |
+
# Save the image
|
| 56 |
+
final_img.save(image_path)
|
| 57 |
+
|
| 58 |
+
return final_img, seed, image_path
|
| 59 |
+
|
| 60 |
+
return final_img, seed, ""
|
| 61 |
|
| 62 |
examples = [
|
| 63 |
"a tiny astronaut hatching from an egg on the moon",
|
|
|
|
| 94 |
|
| 95 |
result = gr.Image(label="Result", show_label=False)
|
| 96 |
|
| 97 |
+
# Add a text box to display the saved image path
|
| 98 |
+
image_path_output = gr.Textbox(
|
| 99 |
+
label="Saved Image Path",
|
| 100 |
+
placeholder="Generated image path will appear here...",
|
| 101 |
+
interactive=False,
|
| 102 |
+
show_copy_button=True
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
with gr.Accordion("Advanced Settings", open=False):
|
| 106 |
|
| 107 |
seed = gr.Slider(
|
|
|
|
| 154 |
examples = examples,
|
| 155 |
fn = infer,
|
| 156 |
inputs = [prompt],
|
| 157 |
+
outputs = [result, seed, image_path_output],
|
| 158 |
cache_examples="lazy"
|
| 159 |
)
|
| 160 |
|
|
|
|
| 162 |
triggers=[run_button.click, prompt.submit],
|
| 163 |
fn = infer,
|
| 164 |
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
| 165 |
+
outputs = [result, seed, image_path_output]
|
| 166 |
)
|
| 167 |
|
| 168 |
demo.launch()
|