File size: 1,248 Bytes
99ad777 |
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 |
import gradio as gr
import zipfile
import os
from typing import List
def create_zip(files: List[str], zip_name: str) -> str:
if not zip_name.endswith('.zip'):
zip_name += '.zip'
with zipfile.ZipFile(zip_name, 'w') as zipf:
for file in files:
zipf.write(file, os.path.basename(file))
return zip_name
def zip_files(files: List[str], zip_name: str):
if not files:
return "No files uploaded!"
zip_path = create_zip(files, zip_name)
return f"Zip file created: {zip_path}"
def process_files(files: List[str], zip_name: str):
file_paths = [file.name for file in files]
return zip_files(file_paths, zip_name)
with gr.Blocks() as demo:
gr.Markdown("## ZipMaker: Upload files and create a zip archive")
with gr.Row():
file_gallery = gr.Gallery(label="Upload Files", type="file")
zip_name_input = gr.Textbox(label="Enter Zip File Name", placeholder="example.zip")
with gr.Row():
submit_button = gr.Button("Create Zip")
output = gr.Textbox(label="Output", interactive=False)
submit_button.click(
process_files,
inputs=[file_gallery, zip_name_input],
outputs=output
)
demo.launch() |