import gradio as gr import os DOWNLOADS_PATH = '/tmp/downloads' # Adjust if necessary def list_directories(): return [d for d in os.listdir(DOWNLOADS_PATH) if os.path.isdir(os.path.join(DOWNLOADS_PATH, d))] def upload_file(file, directory): if not file: return 'No file selected' if directory not in list_directories(): return 'Invalid directory selected' filepath = os.path.join(DOWNLOADS_PATH, directory, file.name) with open(filepath, 'wb') as f: f.write(file.read()) return 'File uploaded successfully' # Create Gradio interface with gr.Blocks() as demo: gr.Markdown("# File Upload with Directory Browser") with gr.Row(): with gr.Column(): gr.Markdown("## Select Directory") directory_dropdown = gr.Dropdown(choices=list_directories(), label="Directory") with gr.Column(): gr.Markdown("## Upload File") file_upload = gr.File(label="Choose File") upload_btn = gr.Button("Upload") upload_status = gr.Textbox(label="Status") # Set up the button click action upload_btn.click( fn=upload_file, inputs=[file_upload, directory_dropdown], outputs=[upload_status] ) # Launch Gradio app demo.launch(server_name="0.0.0.0",share=True, server_port=7860, debug=True)