nx-storage / app.py
AstraOS's picture
Update app.py
737d007 verified
raw
history blame
1.53 kB
import gradio as gr
from uuid import uuid4
from fileManager import FileManager
import re
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://astraos-nx-storage.hf.space/file=./{file_infor['path']}"
import gradio as gr
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
##############################################################
with gr.Blocks() as demo:
gr.Interface(fn=saveFile, inputs="file", outputs="text")
gr.FileExplorer(label="Working directory")
gr.FileExplorer(root="/home/user/app", label="Persistent storage")
demo.launch()