|
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() |