test34 / app.py
webtest1s's picture
Update app.py
19ec90f verified
import os
import gradio as gr
def list_files():
result = {}
# Check /data
try:
result["/data"] = os.listdir("/data")
except Exception as e:
result["/data"] = str(e)
# Check /1
try:
result["/1"] = os.listdir("/1")
except Exception as e:
result["/1"] = str(e)
return result
def upload_file(file):
import os
responses = {}
filename = os.path.basename(file.name) # ✅ FIX
# Try writing to /data
try:
path_data = f"/data/{filename}"
with open(file.name, "rb") as src:
with open(path_data, "wb") as dst:
dst.write(src.read())
responses["/data"] = f"Uploaded to {path_data}"
except Exception as e:
responses["/data"] = f"Failed: {str(e)}"
# Try writing to /1
try:
os.makedirs("/1", exist_ok=True) # ensure folder exists
path_1 = f"/1/{filename}"
with open(file.name, "rb") as src:
with open(path_1, "wb") as dst:
dst.write(src.read())
responses["/1"] = f"Uploaded to {path_1}"
except Exception as e:
responses["/1"] = f"Failed: {str(e)}"
return responses
with gr.Blocks() as app:
gr.Markdown("## 📂 Storage Debugger")
list_btn = gr.Button("List Files")
output_list = gr.JSON()
list_btn.click(fn=list_files, outputs=output_list)
gr.Markdown("## ⬆️ Upload Test")
file_input = gr.File()
upload_btn = gr.Button("Upload File")
upload_output = gr.JSON()
upload_btn.click(fn=upload_file, inputs=file_input, outputs=upload_output)
app.launch()