Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import zipfile
|
| 3 |
+
import os
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
def create_zip(files: List[str], zip_name: str) -> str:
|
| 7 |
+
if not zip_name.endswith('.zip'):
|
| 8 |
+
zip_name += '.zip'
|
| 9 |
+
|
| 10 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 11 |
+
for file in files:
|
| 12 |
+
zipf.write(file, os.path.basename(file))
|
| 13 |
+
|
| 14 |
+
return zip_name
|
| 15 |
+
|
| 16 |
+
def zip_files(files: List[str], zip_name: str):
|
| 17 |
+
if not files:
|
| 18 |
+
return "No files uploaded!"
|
| 19 |
+
|
| 20 |
+
zip_path = create_zip(files, zip_name)
|
| 21 |
+
return f"Zip file created: {zip_path}"
|
| 22 |
+
|
| 23 |
+
def process_files(files: List[str], zip_name: str):
|
| 24 |
+
file_paths = [file.name for file in files]
|
| 25 |
+
return zip_files(file_paths, zip_name)
|
| 26 |
+
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("## ZipMaker: Upload files and create a zip archive")
|
| 29 |
+
|
| 30 |
+
with gr.Row():
|
| 31 |
+
file_gallery = gr.Gallery(label="Upload Files", type="file")
|
| 32 |
+
zip_name_input = gr.Textbox(label="Enter Zip File Name", placeholder="example.zip")
|
| 33 |
+
|
| 34 |
+
with gr.Row():
|
| 35 |
+
submit_button = gr.Button("Create Zip")
|
| 36 |
+
|
| 37 |
+
output = gr.Textbox(label="Output", interactive=False)
|
| 38 |
+
|
| 39 |
+
submit_button.click(
|
| 40 |
+
process_files,
|
| 41 |
+
inputs=[file_gallery, zip_name_input],
|
| 42 |
+
outputs=output
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|