File size: 2,294 Bytes
805d72c
5fa3d68
a996c52
 
5fa3d68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
import os
import shutil
import gradio as gr

def process(phase, target_dir, template_dir, object_file):
    try:
        if not os.path.isdir(target_dir):
            return "Error: Target directory does not exist."
        if not os.path.isdir(template_dir):
            return "Error: Template directory does not exist."
        
        # Read object names from the txt file
        object_names = object_file.read().decode('utf-8').splitlines()
        
        for object_name in object_names:
            if not object_name.strip():
                continue  # Skip empty lines
            # Create a directory for the object under target_dir
            object_dir = os.path.join(target_dir, f"{phase}_{object_name}")
            os.makedirs(object_dir, exist_ok=True)
            
            # Copy files from template_dir to object_dir
            for filename in os.listdir(template_dir):
                template_file = os.path.join(template_dir, filename)
                if os.path.isfile(template_file):
                    # Get the file extension
                    name, ext = os.path.splitext(filename)
                    # Create new filename with object name appended
                    new_filename = f"{name}_{object_name}{ext}"
                    new_file = os.path.join(object_dir, new_filename)
                    shutil.copy(template_file, new_file)
        return f"Created directories and files for {len(object_names)} objects."
    except Exception as e:
        return f"Error: {str(e)}"

phase_dropdown = gr.Dropdown(choices=['FAT', 'iFAT', 'iSAT'], label='Select Phase')

target_dir_input = gr.Textbox(label='Target Directory Path', placeholder='Enter target directory path (must exist)')

template_dir_input = gr.Textbox(label='Template Directory Path', placeholder='Enter template directory path (must exist)')

object_file_input = gr.File(label='Object Names File (txt)', file_types=['.txt'])

output_text = gr.Textbox(label='Output')

gr.Interface(
    fn=process,
    inputs=[phase_dropdown, target_dir_input, template_dir_input, object_file_input],
    outputs=output_text,
    title='Directory and File Creator',
    description='Enter the required information and upload the object names txt file. Ensure the directory paths are valid.'
).launch()