testfiles / app.py
dunlp's picture
GPT
5fa3d68 verified
raw
history blame
2.29 kB
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()