import gradio as gr import logging from pathlib import Path # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") # Default directory to scan DEFAULT_DATA_PATH = Path.home() # Use home directory as a dynamic default # Function to get file details and total storage usage def get_storage(data_path: str): # Convert to Path object data_path = Path(data_path) # Validate directory if not data_path.exists() or not data_path.is_dir(): logging.error(f"Directory not found: {data_path}") return [], f"Error: Directory not found or inaccessible: {data_path}" # Collect file details and calculate total size files = [] total_size = 0 for file in data_path.glob("**/*"): if file.is_file(): try: stats = file.stat() files.append({ "Original Name": file.name, "Path": str(file.resolve()), "Size (MB)": f"{stats.st_size / (1024.0 ** 2):.2f} MB", }) total_size += stats.st_size except Exception as e: logging.warning(f"Failed to process file: {file}. Error: {e}") if not files: logging.info(f"No files found in directory: {data_path}") return [], "No files found in the specified directory." # Total size in GB usage = f"{total_size / (1024.0 ** 3):.3f} GB" logging.info(f"Scanned {len(files)} files. Total size: {usage}") return files, usage # Define the Gradio interface with gr.Blocks() as app: # Title and description gr.Markdown("# 📁 File Storage Viewer\n" "Easily view files and calculate storage usage in a specified directory.") with gr.Row(): # Input field for directory path dir_input = gr.Textbox( value=str(DEFAULT_DATA_PATH), label="Directory Path", placeholder="Enter the directory path to scan.", ) # Button to trigger file listing fetch_btn = gr.Button("Fetch Files") # Outputs: File list and total storage usage with gr.Row(): file_table = gr.Dataframe( headers=["Original Name", "Path", "Size (MB)"], interactive=False, label="Files", ) storage_usage = gr.Textbox(label="Total Storage Usage", interactive=False) # Click action to trigger the `get_storage` function fetch_btn.click( get_storage, inputs=[dir_input], outputs=[file_table, storage_usage], show_progress=True ) # Launch the Gradio app app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True)