Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,73 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
def process(phase,
|
| 6 |
try:
|
| 7 |
-
if
|
| 8 |
-
return "Error:
|
| 9 |
-
if
|
| 10 |
-
return "Error:
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
| 36 |
phase_dropdown = gr.Dropdown(choices=['FAT', 'iFAT', 'iSAT'], label='Select Phase')
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
template_dir_input = gr.Textbox(label='Template Directory Path', placeholder='Enter template directory path (must exist)')
|
| 41 |
|
| 42 |
-
object_file_input = gr.File(label='Object Names File (
|
| 43 |
|
| 44 |
-
|
| 45 |
|
| 46 |
gr.Interface(
|
| 47 |
fn=process,
|
| 48 |
-
inputs=[phase_dropdown,
|
| 49 |
-
outputs=
|
| 50 |
title='Directory and File Creator',
|
| 51 |
-
description='
|
| 52 |
).launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
+
import zipfile
|
| 4 |
+
import tempfile
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
def process(phase, template_zip, object_file):
|
| 8 |
try:
|
| 9 |
+
if template_zip is None:
|
| 10 |
+
return "Error: Please upload the template directory as a ZIP file."
|
| 11 |
+
if object_file is None:
|
| 12 |
+
return "Error: Please upload the object names TXT file."
|
| 13 |
|
| 14 |
+
# Create a temporary directory for processing
|
| 15 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 16 |
+
# Unzip the template directory
|
| 17 |
+
template_dir = os.path.join(tmpdir, 'template')
|
| 18 |
+
with zipfile.ZipFile(template_zip.name, 'r') as zip_ref:
|
| 19 |
+
zip_ref.extractall(template_dir)
|
| 20 |
+
|
| 21 |
+
# Read object names from the txt file
|
| 22 |
+
object_names = object_file.read().decode('utf-8').splitlines()
|
| 23 |
+
|
| 24 |
+
# Create a directory to store the results
|
| 25 |
+
result_dir = os.path.join(tmpdir, 'results')
|
| 26 |
+
os.makedirs(result_dir, exist_ok=True)
|
| 27 |
|
| 28 |
+
for object_name in object_names:
|
| 29 |
+
if not object_name.strip():
|
| 30 |
+
continue # Skip empty lines
|
| 31 |
+
# Create a directory for the object under result_dir
|
| 32 |
+
object_dir = os.path.join(result_dir, f"{phase}_{object_name}")
|
| 33 |
+
os.makedirs(object_dir, exist_ok=True)
|
| 34 |
+
|
| 35 |
+
# Copy files from template_dir to object_dir
|
| 36 |
+
for root, dirs, files in os.walk(template_dir):
|
| 37 |
+
rel_path = os.path.relpath(root, template_dir)
|
| 38 |
+
dest_dir = os.path.join(object_dir, rel_path)
|
| 39 |
+
os.makedirs(dest_dir, exist_ok=True)
|
| 40 |
+
for filename in files:
|
| 41 |
+
template_file = os.path.join(root, filename)
|
| 42 |
+
if os.path.isfile(template_file):
|
| 43 |
+
# Get the file extension
|
| 44 |
+
name, ext = os.path.splitext(filename)
|
| 45 |
+
# Create new filename with object name appended
|
| 46 |
+
new_filename = f"{name}_{object_name}{ext}"
|
| 47 |
+
new_file = os.path.join(dest_dir, new_filename)
|
| 48 |
+
shutil.copy(template_file, new_file)
|
| 49 |
+
|
| 50 |
+
# Zip the result_dir
|
| 51 |
+
result_zip_path = os.path.join(tmpdir, 'result.zip')
|
| 52 |
+
shutil.make_archive(os.path.splitext(result_zip_path)[0], 'zip', result_dir)
|
| 53 |
+
|
| 54 |
+
# Return the zip file for download
|
| 55 |
+
return result_zip_path
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error: {str(e)}"
|
| 58 |
|
| 59 |
phase_dropdown = gr.Dropdown(choices=['FAT', 'iFAT', 'iSAT'], label='Select Phase')
|
| 60 |
|
| 61 |
+
template_zip_input = gr.File(label='Template Directory (ZIP file)', file_types=['.zip'])
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
object_file_input = gr.File(label='Object Names File (TXT)', file_types=['.txt'])
|
| 64 |
|
| 65 |
+
output_file = gr.File(label='Download Result ZIP')
|
| 66 |
|
| 67 |
gr.Interface(
|
| 68 |
fn=process,
|
| 69 |
+
inputs=[phase_dropdown, template_zip_input, object_file_input],
|
| 70 |
+
outputs=output_file,
|
| 71 |
title='Directory and File Creator',
|
| 72 |
+
description='Upload the template directory as a ZIP file and the object names TXT file. The processed files will be provided as a downloadable ZIP.'
|
| 73 |
).launch()
|