Muhammad Taqi Raza
commited on
Commit
·
d2d7c02
1
Parent(s):
8e19ca1
adding gradio app
Browse files- app.py +82 -0
- requirements.txt +2 -1
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
from pathlib import Path
|
5 |
+
|
6 |
+
# The base directory that will be explored
|
7 |
+
BASE_DIR = Path(__file__).resolve().parent / "your_target_folder"
|
8 |
+
|
9 |
+
# Ensure BASE_DIR exists
|
10 |
+
BASE_DIR.mkdir(parents=True, exist_ok=True)
|
11 |
+
|
12 |
+
def resolve_path(current_rel_path: str) -> Path:
|
13 |
+
"""Secure path resolution within BASE_DIR"""
|
14 |
+
resolved_path = (BASE_DIR / current_rel_path).resolve()
|
15 |
+
if BASE_DIR not in resolved_path.parents and resolved_path != BASE_DIR:
|
16 |
+
raise ValueError("Access outside base directory is not allowed.")
|
17 |
+
return resolved_path
|
18 |
+
|
19 |
+
def list_dir(current_rel_path: str = ""):
|
20 |
+
current_path = resolve_path(current_rel_path)
|
21 |
+
|
22 |
+
# Parent folder navigation
|
23 |
+
parent_rel = str(Path(current_rel_path).parent) if current_rel_path else ""
|
24 |
+
entries = []
|
25 |
+
|
26 |
+
if current_path != BASE_DIR:
|
27 |
+
entries.append(("..", "⬆️ Parent Folder"))
|
28 |
+
|
29 |
+
# List directories and files
|
30 |
+
for item in sorted(current_path.iterdir()):
|
31 |
+
rel_item = os.path.relpath(item, BASE_DIR)
|
32 |
+
label = f"📁 {item.name}" if item.is_dir() else f"📄 {item.name}"
|
33 |
+
entries.append((rel_item, label))
|
34 |
+
|
35 |
+
return gr.update(choices=entries, value=None), f"Currently in: /{current_rel_path}"
|
36 |
+
|
37 |
+
def download_entry(selected_rel_path: str):
|
38 |
+
selected_path = resolve_path(selected_rel_path)
|
39 |
+
|
40 |
+
if selected_path.is_file():
|
41 |
+
return selected_path
|
42 |
+
elif selected_path.is_dir():
|
43 |
+
zip_path = f"/tmp/{selected_path.name}.zip"
|
44 |
+
with zipfile.ZipFile(zip_path, "w") as zipf:
|
45 |
+
for root, dirs, files in os.walk(selected_path):
|
46 |
+
for file in files:
|
47 |
+
abs_file = os.path.join(root, file)
|
48 |
+
arc_file = os.path.relpath(abs_file, selected_path)
|
49 |
+
zipf.write(abs_file, arc_file)
|
50 |
+
return zip_path
|
51 |
+
else:
|
52 |
+
return None
|
53 |
+
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
current_path = gr.State("")
|
56 |
+
|
57 |
+
gr.Markdown("# 📁 Folder Browser")
|
58 |
+
|
59 |
+
with gr.Row():
|
60 |
+
folder_dropdown = gr.Dropdown(label="Select File or Folder", choices=[])
|
61 |
+
refresh_btn = gr.Button("🔄 Refresh")
|
62 |
+
|
63 |
+
status_text = gr.Textbox(label="Current Path", interactive=False)
|
64 |
+
download_btn = gr.Button("⬇️ Download Selected")
|
65 |
+
file_output = gr.File(label="Download Result")
|
66 |
+
|
67 |
+
# Events
|
68 |
+
refresh_btn.click(fn=list_dir, inputs=current_path, outputs=[folder_dropdown, status_text])
|
69 |
+
|
70 |
+
folder_dropdown.change(
|
71 |
+
fn=lambda x: (x, *list_dir(x)), # update path, refresh list
|
72 |
+
inputs=folder_dropdown,
|
73 |
+
outputs=[current_path, folder_dropdown, status_text],
|
74 |
+
)
|
75 |
+
|
76 |
+
download_btn.click(fn=download_entry, inputs=folder_dropdown, outputs=file_output)
|
77 |
+
|
78 |
+
# Initial trigger
|
79 |
+
demo.load(fn=list_dir, inputs=current_path, outputs=[folder_dropdown, status_text])
|
80 |
+
|
81 |
+
demo.launch()
|
82 |
+
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
accelerate>=1.1.1
|
2 |
transformers>=4.46.2
|
3 |
numpy==1.26.0
|
@@ -17,4 +18,4 @@ peft
|
|
17 |
opencv-python
|
18 |
decord
|
19 |
av
|
20 |
-
torchdiffeq
|
|
|
1 |
+
gradio
|
2 |
accelerate>=1.1.1
|
3 |
transformers>=4.46.2
|
4 |
numpy==1.26.0
|
|
|
18 |
opencv-python
|
19 |
decord
|
20 |
av
|
21 |
+
torchdiffeq
|