Spaces:
Runtime error
Runtime error
File size: 1,914 Bytes
0a06687 f7ae17e c69a04f f7ae17e 3a324bd f7ae17e 0a06687 3a324bd 0a06687 3a324bd 0a06687 59c7c2b 0a06687 59c7c2b 0a06687 f7ae17e 0a06687 59c7c2b f7ae17e 0a06687 18111bf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
import os
DOWNLOADS_PATH = '/tmp/downloads' # Adjust if necessary
# Ensure the downloads path exists
os.makedirs(DOWNLOADS_PATH, exist_ok=True)
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'
directories = list_directories()
if directory not in directories:
return 'Invalid directory selected'
filepath = os.path.join(DOWNLOADS_PATH, directory, file.name)
os.makedirs(os.path.dirname(filepath), exist_ok=True) # Ensure the directory exists
with open(filepath, 'wb') as f:
f.write(file.read())
return 'File uploaded successfully'
def get_directory_choices():
return list_directories()
# 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=get_directory_choices(), 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]
)
# Update directory choices when the dropdown is interacted with
def update_directory_dropdown():
return gr.Dropdown.update(choices=get_directory_choices())
directory_dropdown.change(fn=update_directory_dropdown, inputs=[], outputs=[directory_dropdown])
# Launch Gradio app
demo.launch(share=True, server_name="0.0.0.0",server_port=7860, debug=True) |