Spaces:
Runtime error
Runtime error
add ability to zip and download all images
Browse files1234538 unmodified lines44454647484943 unmodified lines93949596979810 unmodified lines109110111112113114115import osimport sysimport gradio as grfrom openai import OpenAI38 unmodified linesexcept Exception as e:print(e)def generate_images(prompts, pw, model):# add a conditional to check for a valid passwordif pw != os.getenv("PW"):43 unmodified linesexcept Exception as error:print(str(error))raise gr.Error(f"An error occurred while generating the image for: {prompt}")return image_paths10 unmodified lines# output_image = gr.Image(label="Image Output")output_images = gr.Gallery(label="Image Outputs",columns=[3], rows=[1], object_fit="contain", height="auto",allow_preview=False)text.submit(fn=generate_images, inputs=[text,pw,model], outputs=output_images, api_name="generate_image")btn.click(fn=generate_images, inputs=[text,pw,model], outputs=output_images, api_name=False)demo.launch(share=True)12345638 unmodified lines45464748495051525354555657585960616263646566676869707172737443 unmodified lines11811912012112212312410 unmodified lines135136137138139140141142143144145146147import osimport sysimport zipfileimport gradio as grfrom openai import OpenAI38 unmodified linesexcept Exception as e:print(e)image_paths_global = []def generate_images_wrapper(prompts, pw, model):global image_paths_globalimage_paths = generate_images(prompts, pw, model)image_paths_global = image_paths # Store paths globallyreturn image_paths # You might want to return something else for displaydef zip_images(image_paths):zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').namewith zipfile.ZipFile(zip_file_path, 'w') as zipf:for path in image_paths:zipf.write(path, arcname=os.path.basename(path))os.remove(path) # Clean up the temp image filereturn zip_file_pathdef download_all_images():global image_paths_globalif not image_paths_global:raise gr.Error("No images to download.")zip_path = zip_images(image_paths_global)image_paths_global = [] # Reset the global variablereturn zip_pathdef generate_images(prompts, pw, model):# add a conditional to check for a valid passwordif pw != os.getenv("PW"):43 unmodified linesexcept Exception as error:print(str(error))raise gr.Error(f"An error occurred while generating the image for: {prompt}")return image_paths10 unmodified lines# output_image = gr.Image(label="Image Output")output_images = gr.Gallery(label="Image Outputs",columns=[3], rows=[1], object_fit="contain", height="auto",allow_preview=False)text.submit(fn=generate_images_wrapper, inputs=[text,pw,model], outputs=output_images, api_name="generate_image")btn.click(fn=generate_images_wrapper, inputs=[text,pw,model], outputs=output_images, api_name=False)download_all_btn = gr.Button("Download All")download_link = gr.File(label="Download Zip")download_all_btn.click(fn=download_all_images, inputs=[], outputs=download_link)demo.launch(share=True)
app.py+34-2