Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,81 @@
|
|
1 |
from pathlib import Path
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
# Function to get file details and total storage usage
|
8 |
-
def get_storage():
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
total_size =
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return files, usage
|
22 |
|
23 |
# Define the Gradio interface
|
24 |
with gr.Blocks() as app:
|
25 |
# Title and description
|
26 |
-
gr.Markdown("# π File Storage Viewer\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
28 |
with gr.Row():
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
files = gr.Dataframe(
|
36 |
-
headers=["Original Name", "Path", "Size"],
|
37 |
-
interactive=False,
|
38 |
-
label="Files",
|
39 |
-
)
|
40 |
-
storage = gr.Textbox(label="Total Storage Usage", interactive=False)
|
41 |
|
42 |
# Click action to trigger the `get_storage` function
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
# Launch the Gradio app with access to
|
46 |
-
app.launch(allowed_paths=[
|
|
|
1 |
from pathlib import Path
|
2 |
import gradio as gr
|
3 |
+
import logging
|
4 |
|
5 |
+
# Configure logging
|
6 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
7 |
+
|
8 |
+
# Default directory to scan
|
9 |
+
DEFAULT_DATA_PATH = Path("/data")
|
10 |
|
11 |
# Function to get file details and total storage usage
|
12 |
+
def get_storage(data_path: str):
|
13 |
+
# Convert to Path object
|
14 |
+
data_path = Path(data_path)
|
15 |
+
|
16 |
+
# Validate directory
|
17 |
+
if not data_path.exists() or not data_path.is_dir():
|
18 |
+
logging.error(f"Directory not found: {data_path}")
|
19 |
+
return [], "Error: Directory not found or inaccessible."
|
20 |
+
|
21 |
+
# Collect file details and calculate total size
|
22 |
+
files = []
|
23 |
+
total_size = 0
|
24 |
+
for file in data_path.glob("**/*"):
|
25 |
+
if file.is_file():
|
26 |
+
try:
|
27 |
+
stats = file.stat()
|
28 |
+
files.append({
|
29 |
+
"Original Name": file.name,
|
30 |
+
"Path": str(file.resolve()),
|
31 |
+
"Size (MB)": f"{stats.st_size / (1024.0 ** 2):.2f} MB",
|
32 |
+
})
|
33 |
+
total_size += stats.st_size
|
34 |
+
except Exception as e:
|
35 |
+
logging.warning(f"Failed to process file: {file}. Error: {e}")
|
36 |
+
|
37 |
+
if not files:
|
38 |
+
logging.info(f"No files found in directory: {data_path}")
|
39 |
+
return [], "No files found in the specified directory."
|
40 |
+
|
41 |
+
# Total size in GB
|
42 |
+
usage = f"{total_size / (1024.0 ** 3):.3f} GB"
|
43 |
+
logging.info(f"Scanned {len(files)} files. Total size: {usage}")
|
44 |
return files, usage
|
45 |
|
46 |
# Define the Gradio interface
|
47 |
with gr.Blocks() as app:
|
48 |
# Title and description
|
49 |
+
gr.Markdown("# π File Storage Viewer\n"
|
50 |
+
"Easily view files and calculate storage usage in a specified directory.")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
# Input field for directory path
|
54 |
+
dir_input = gr.Textbox(
|
55 |
+
value=str(DEFAULT_DATA_PATH),
|
56 |
+
label="Directory Path",
|
57 |
+
placeholder="Enter the directory path to scan.",
|
58 |
+
)
|
59 |
+
|
60 |
+
# Button to trigger file listing
|
61 |
+
fetch_btn = gr.Button("Fetch Files")
|
62 |
|
63 |
+
# Outputs: File list and total storage usage
|
64 |
with gr.Row():
|
65 |
+
file_table = gr.Dataframe(
|
66 |
+
headers=["Original Name", "Path", "Size (MB)"],
|
67 |
+
interactive=True,
|
68 |
+
label="Files",
|
69 |
+
)
|
70 |
+
storage_usage = gr.Textbox(label="Total Storage Usage", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Click action to trigger the `get_storage` function
|
73 |
+
fetch_btn.click(
|
74 |
+
get_storage,
|
75 |
+
inputs=[dir_input],
|
76 |
+
outputs=[file_table, storage_usage],
|
77 |
+
show_progress=True
|
78 |
+
)
|
79 |
|
80 |
+
# Launch the Gradio app with access to custom paths
|
81 |
+
app.launch(allowed_paths=[str(DEFAULT_DATA_PATH)], enable_queue=True)
|