dunlp commited on
Commit
9a7c444
1 Parent(s): 5fa3d68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -33
app.py CHANGED
@@ -1,52 +1,73 @@
1
  import os
2
  import shutil
 
 
3
  import gradio as gr
4
 
5
- def process(phase, target_dir, template_dir, object_file):
6
  try:
7
- if not os.path.isdir(target_dir):
8
- return "Error: Target directory does not exist."
9
- if not os.path.isdir(template_dir):
10
- return "Error: Template directory does not exist."
11
 
12
- # Read object names from the txt file
13
- object_names = object_file.read().decode('utf-8').splitlines()
14
-
15
- for object_name in object_names:
16
- if not object_name.strip():
17
- continue # Skip empty lines
18
- # Create a directory for the object under target_dir
19
- object_dir = os.path.join(target_dir, f"{phase}_{object_name}")
20
- os.makedirs(object_dir, exist_ok=True)
 
 
 
 
21
 
22
- # Copy files from template_dir to object_dir
23
- for filename in os.listdir(template_dir):
24
- template_file = os.path.join(template_dir, filename)
25
- if os.path.isfile(template_file):
26
- # Get the file extension
27
- name, ext = os.path.splitext(filename)
28
- # Create new filename with object name appended
29
- new_filename = f"{name}_{object_name}{ext}"
30
- new_file = os.path.join(object_dir, new_filename)
31
- shutil.copy(template_file, new_file)
32
- return f"Created directories and files for {len(object_names)} objects."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- target_dir_input = gr.Textbox(label='Target Directory Path', placeholder='Enter target directory path (must exist)')
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 (txt)', file_types=['.txt'])
43
 
44
- output_text = gr.Textbox(label='Output')
45
 
46
  gr.Interface(
47
  fn=process,
48
- inputs=[phase_dropdown, target_dir_input, template_dir_input, object_file_input],
49
- outputs=output_text,
50
  title='Directory and File Creator',
51
- description='Enter the required information and upload the object names txt file. Ensure the directory paths are valid.'
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()