Spaces:
Runtime error
Runtime error
ChandimaPrabath
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ import os
|
|
3 |
|
4 |
DOWNLOADS_PATH = '/tmp/downloads' # Adjust if necessary
|
5 |
|
|
|
|
|
|
|
6 |
def list_directories():
|
7 |
return [d for d in os.listdir(DOWNLOADS_PATH) if os.path.isdir(os.path.join(DOWNLOADS_PATH, d))]
|
8 |
|
@@ -10,10 +13,12 @@ def upload_file(file, directory):
|
|
10 |
if not file:
|
11 |
return 'No file selected'
|
12 |
|
13 |
-
|
|
|
14 |
return 'Invalid directory selected'
|
15 |
|
16 |
filepath = os.path.join(DOWNLOADS_PATH, directory, file.name)
|
|
|
17 |
with open(filepath, 'wb') as f:
|
18 |
f.write(file.read())
|
19 |
|
@@ -34,6 +39,12 @@ with gr.Blocks() as demo:
|
|
34 |
upload_btn = gr.Button("Upload")
|
35 |
upload_status = gr.Textbox(label="Status")
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# Set up the button click action
|
38 |
upload_btn.click(
|
39 |
fn=upload_file,
|
@@ -42,4 +53,4 @@ with gr.Blocks() as demo:
|
|
42 |
)
|
43 |
|
44 |
# Launch Gradio app
|
45 |
-
demo.launch(server_name="0.0.0.0",share=True,
|
|
|
3 |
|
4 |
DOWNLOADS_PATH = '/tmp/downloads' # Adjust if necessary
|
5 |
|
6 |
+
# Ensure the downloads path exists
|
7 |
+
os.makedirs(DOWNLOADS_PATH, exist_ok=True)
|
8 |
+
|
9 |
def list_directories():
|
10 |
return [d for d in os.listdir(DOWNLOADS_PATH) if os.path.isdir(os.path.join(DOWNLOADS_PATH, d))]
|
11 |
|
|
|
13 |
if not file:
|
14 |
return 'No file selected'
|
15 |
|
16 |
+
directories = list_directories()
|
17 |
+
if directory not in directories:
|
18 |
return 'Invalid directory selected'
|
19 |
|
20 |
filepath = os.path.join(DOWNLOADS_PATH, directory, file.name)
|
21 |
+
os.makedirs(os.path.dirname(filepath), exist_ok=True) # Ensure the directory exists
|
22 |
with open(filepath, 'wb') as f:
|
23 |
f.write(file.read())
|
24 |
|
|
|
39 |
upload_btn = gr.Button("Upload")
|
40 |
upload_status = gr.Textbox(label="Status")
|
41 |
|
42 |
+
# Update dropdown choices when interface is loaded
|
43 |
+
def update_directories():
|
44 |
+
return list_directories()
|
45 |
+
|
46 |
+
directory_dropdown.update(choices=update_directories())
|
47 |
+
|
48 |
# Set up the button click action
|
49 |
upload_btn.click(
|
50 |
fn=upload_file,
|
|
|
53 |
)
|
54 |
|
55 |
# Launch Gradio app
|
56 |
+
demo.launch(server_name="0.0.0.0", share=True,server_port=7860, debug=True)
|