nx-storage / app.py
AstraOS's picture
Update app.py
4e076ab verified
raw
history blame
2.11 kB
import gradio as gr
from uuid import uuid4
from fileManager import FileManager
import re
import os
import random
import string
import glob
##############################################################
# Generate some text files and save them in persistent storage
##############################################################
def generate_random_string(length=100):
"""Generate a random string of fixed length."""
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
num_files=10
file_length=1000
for directory in ["/home/user/app", "/data"]:
if not os.path.exists(directory):
break
for i in range(num_files):
file_name = os.path.join(directory, f'random_file_{i}.txt')
with open(file_name, 'w') as f:
for _ in range(file_length):
f.write(generate_random_string() + '\n')
##############################################################
# The Gradio app
##############################################################
"""fileManager = FileManager()
fileManager.start()
def saveFile(file):
with open(file.name, "rb") as rfile:
file_infor = fileManager.saveFile(rfile.read(), file.orig_name)
return f"https://emee-nx-storage.hf.space/file=./{file_infor['path']}"
nxapp = gr.Interface(fn=saveFile, inputs="file", outputs="text")
nxapp.launch()
with gr.Blocks() as demo:
gr.FileExplorer(label="Working directory")
gr.FileExplorer(root="/home/user/app", label="Persistent storage")
demo.launch()"""
with gr.Blocks() as demo:
# File explorer for working directory
working_dir = gr.FileExplorer(label="Working directory")
# File explorer with persistent storage (optional)
persistent_storage = gr.FileExplorer(root="/home/user/app", label="Persistent storage")
# Connect file explorer output to saveFile function input
demo.launch(fn=saveFile, inputs=working_dir.outputs, outputs="text")
# Optionally, if you want to use persistent storage:
# demo.launch(fn=saveFile, inputs=[working_dir.outputs, persistent_storage.outputs], outputs="text")