Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ WORKFLOW_FILENAME = 'sd3.json'
|
|
| 14 |
ROOT_DIR = os.getcwd() # Root directory of the project
|
| 15 |
OUTPUT_DIR = os.path.join(ROOT_DIR, 'output')
|
| 16 |
USER_OUTPUTS_DIR = os.path.join(OUTPUT_DIR, 'user_outputs')
|
| 17 |
-
URL = "https://
|
| 18 |
|
| 19 |
# Create output directories if they don't exist
|
| 20 |
os.makedirs(USER_OUTPUTS_DIR, exist_ok=True)
|
|
@@ -92,22 +92,28 @@ def monitor_output_images(output_dir, previous_images, timeout=60):
|
|
| 92 |
|
| 93 |
def copy_file_with_retry(src, dst_dir, file_index, retries=5, delay=1):
|
| 94 |
dst = os.path.join(dst_dir, f"SD3_{file_index:05d}.png")
|
|
|
|
| 95 |
for _ in range(retries):
|
| 96 |
try:
|
| 97 |
shutil.copy(src, dst)
|
|
|
|
| 98 |
return dst
|
| 99 |
except PermissionError:
|
| 100 |
time.sleep(delay)
|
|
|
|
|
|
|
| 101 |
raise PermissionError(f"Failed to copy {src} to {dst} after {retries} retries")
|
| 102 |
|
| 103 |
def zip_files(output_images: List[str], zip_interval: int, zip_folder: str):
|
| 104 |
zip_files = []
|
| 105 |
for i in range(0, len(output_images), zip_interval):
|
| 106 |
zip_filename = os.path.join(zip_folder, f"images_{i//zip_interval + 1}_{time.time_ns()}.zip")
|
|
|
|
| 107 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
| 108 |
for img in output_images[i:i+zip_interval]:
|
| 109 |
zipf.write(img, os.path.basename(img))
|
| 110 |
zip_files.append(zip_filename)
|
|
|
|
| 111 |
return zip_files
|
| 112 |
|
| 113 |
def process_prompts(prompts_text, negative_prompt_text, user_folder, zip_interval):
|
|
@@ -167,6 +173,7 @@ def process_prompts(prompts_text, negative_prompt_text, user_folder, zip_interva
|
|
| 167 |
continue
|
| 168 |
|
| 169 |
new_image_path = os.path.join(OUTPUT_DIR, new_image)
|
|
|
|
| 170 |
try:
|
| 171 |
copied_image_path = copy_file_with_retry(new_image_path, user_folder, file_index)
|
| 172 |
log_message(f"New image generated and copied to user folder: {copied_image_path}")
|
|
@@ -251,4 +258,65 @@ def main():
|
|
| 251 |
with gr.Accordion("Instructions", open=True):
|
| 252 |
gr.Markdown("""
|
| 253 |
**SD3 Batch Imagine Instructions**
|
| 254 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
ROOT_DIR = os.getcwd() # Root directory of the project
|
| 15 |
OUTPUT_DIR = os.path.join(ROOT_DIR, 'output')
|
| 16 |
USER_OUTPUTS_DIR = os.path.join(OUTPUT_DIR, 'user_outputs')
|
| 17 |
+
URL = "https://your-ngrok-url.ngrok-free.app/prompt" # Replace with your ngrok URL
|
| 18 |
|
| 19 |
# Create output directories if they don't exist
|
| 20 |
os.makedirs(USER_OUTPUTS_DIR, exist_ok=True)
|
|
|
|
| 92 |
|
| 93 |
def copy_file_with_retry(src, dst_dir, file_index, retries=5, delay=1):
|
| 94 |
dst = os.path.join(dst_dir, f"SD3_{file_index:05d}.png")
|
| 95 |
+
log_message(f"Attempting to copy file from {src} to {dst}")
|
| 96 |
for _ in range(retries):
|
| 97 |
try:
|
| 98 |
shutil.copy(src, dst)
|
| 99 |
+
log_message(f"File copied successfully to {dst}")
|
| 100 |
return dst
|
| 101 |
except PermissionError:
|
| 102 |
time.sleep(delay)
|
| 103 |
+
log_message(f"PermissionError encountered. Retrying...")
|
| 104 |
+
log_message(f"Failed to copy {src} to {dst} after {retries} retries")
|
| 105 |
raise PermissionError(f"Failed to copy {src} to {dst} after {retries} retries")
|
| 106 |
|
| 107 |
def zip_files(output_images: List[str], zip_interval: int, zip_folder: str):
|
| 108 |
zip_files = []
|
| 109 |
for i in range(0, len(output_images), zip_interval):
|
| 110 |
zip_filename = os.path.join(zip_folder, f"images_{i//zip_interval + 1}_{time.time_ns()}.zip")
|
| 111 |
+
log_message(f"Creating zip file {zip_filename}")
|
| 112 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
| 113 |
for img in output_images[i:i+zip_interval]:
|
| 114 |
zipf.write(img, os.path.basename(img))
|
| 115 |
zip_files.append(zip_filename)
|
| 116 |
+
log_message(f"Zip files created: {zip_files}")
|
| 117 |
return zip_files
|
| 118 |
|
| 119 |
def process_prompts(prompts_text, negative_prompt_text, user_folder, zip_interval):
|
|
|
|
| 173 |
continue
|
| 174 |
|
| 175 |
new_image_path = os.path.join(OUTPUT_DIR, new_image)
|
| 176 |
+
log_message(f"New image generated: {new_image_path}")
|
| 177 |
try:
|
| 178 |
copied_image_path = copy_file_with_retry(new_image_path, user_folder, file_index)
|
| 179 |
log_message(f"New image generated and copied to user folder: {copied_image_path}")
|
|
|
|
| 258 |
with gr.Accordion("Instructions", open=True):
|
| 259 |
gr.Markdown("""
|
| 260 |
**SD3 Batch Imagine Instructions**
|
| 261 |
+
- Enter your prompts below, one per empty line.
|
| 262 |
+
- Enter your negative prompts below, one per line. (Optional)
|
| 263 |
+
- Set the zip interval to determine how many images will be included in each zip file.
|
| 264 |
+
- Click "Process Prompts" to start generating images.
|
| 265 |
+
- Click "Cancel Processing" to stop the current batch run.
|
| 266 |
+
- Watch the progress as images are generated in real-time.
|
| 267 |
+
- At the end of the process, zip files containing your images will be available for download.
|
| 268 |
+
""")
|
| 269 |
+
|
| 270 |
+
with gr.Column(scale=2):
|
| 271 |
+
gr.Markdown("### Enter Prompts")
|
| 272 |
+
prompts_text = gr.Textbox(lines=20, placeholder="Enter your prompts here, one per empty line.", label="Prompts")
|
| 273 |
+
negative_prompts_text = gr.Textbox(lines=5, placeholder="Enter your negative prompts here, one per line.", label="Negative Prompts")
|
| 274 |
+
zip_interval = gr.Number(value=10, label="Zip Interval", precision=0)
|
| 275 |
+
process_btn = gr.Button("Process Prompts")
|
| 276 |
+
cancel_btn = gr.Button("Cancel Processing")
|
| 277 |
+
progress_text = gr.Markdown("Progress")
|
| 278 |
+
gallery_output = gr.Gallery(label="Generated Images")
|
| 279 |
+
zip_files_output = gr.Files(label="Zip Files")
|
| 280 |
+
|
| 281 |
+
with gr.Column(scale=1):
|
| 282 |
+
gr.Markdown("### Detailed Logs")
|
| 283 |
+
logs_output = gr.Textbox(lines=10, interactive=False, label="Logs")
|
| 284 |
+
|
| 285 |
+
def generate_user_folder():
|
| 286 |
+
user_folder = os.path.normpath(os.path.join(USER_OUTPUTS_DIR, f'SD3{random.randint(1000, 9999)}'))
|
| 287 |
+
os.makedirs(user_folder, exist_ok=True)
|
| 288 |
+
log_message(f"Generated user folder: {user_folder}")
|
| 289 |
+
return user_folder
|
| 290 |
+
|
| 291 |
+
def on_click(prompts_text, negative_prompts_text, zip_interval):
|
| 292 |
+
reset_cancel_processing_fn()
|
| 293 |
+
user_folder = generate_user_folder()
|
| 294 |
+
output_images, zip_files_list = [], []
|
| 295 |
+
logs = ""
|
| 296 |
+
try:
|
| 297 |
+
for images, zip_files, log_msg in process_prompts(prompts_text, negative_prompts_text, user_folder, zip_interval):
|
| 298 |
+
output_images = images
|
| 299 |
+
zip_files_list = zip_files
|
| 300 |
+
logs = log_msg
|
| 301 |
+
yield images, zip_files_list, logs + log_messages
|
| 302 |
+
except Exception as e:
|
| 303 |
+
log_message(f"Error during prompt processing: {e}")
|
| 304 |
+
logs += f"Error: {e}\n"
|
| 305 |
+
yield output_images, zip_files_list, logs + log_messages
|
| 306 |
+
|
| 307 |
+
process_btn.click(
|
| 308 |
+
fn=on_click,
|
| 309 |
+
inputs=[prompts_text, negative_prompts_text, zip_interval],
|
| 310 |
+
outputs=[gallery_output, zip_files_output, logs_output]
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
cancel_btn.click(
|
| 314 |
+
fn=cancel_processing_fn,
|
| 315 |
+
inputs=[],
|
| 316 |
+
outputs=[]
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
demo.launch()
|
| 320 |
+
|
| 321 |
+
if __name__ == "__main__":
|
| 322 |
+
main()
|