import gradio as gr import os REPO_DIR = "repository" # ---------- CORE FUNCTION ---------- def list_files_by_folder(folder_id): folder_id = str(folder_id).strip() folder_path = os.path.join(REPO_DIR, folder_id) if not os.path.exists(folder_path): return f"❌ Folder '{folder_id}' does not exist.", [] files = [ os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) ] if not files: return f"⚠️ Folder '{folder_id}' exists but has no files.", [] return f"✅ Found {len(files)} files in folder {folder_id}", files # ---------- GRADIO UI ---------- with gr.Blocks() as app: gr.Markdown("## 📁 Repository Directory Access System") gr.Markdown("Enter a **folder number** (e.g., 1, 2, 3) to view and download all files inside it.") input_folder = gr.Textbox(label="Enter Folder Number", placeholder="Example: 1") search_btn = gr.Button("Fetch Files") status_output = gr.Markdown() # ✅ This is the ONLY safe way to serve files on HuggingFace files_output = gr.Files( label="Click any file to View or Download", file_types=[".pdf", ".mp3", ".mp4", ".jpg", ".jpeg", ".png", ".xls", ".xlsx", ".ppt", ".pptx", ".doc", ".docx"] ) search_btn.click( fn=list_files_by_folder, inputs=input_folder, outputs=[status_output, files_output] ) app.launch()