import gradio as gr from uuid import uuid4 from fileManager import FileManager # Assuming fileManager is defined elsewhere import re import os import random import string import glob ############################################################## # File Upload and Storage Functionality ############################################################## 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']}" ############################################################## # Random File Generation Functionality (Optional) ############################################################## 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 def generate_random_files(directory): if not os.path.exists(directory): return 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 Combined Functionality ############################################################## with gr.Blocks() as demo: # File Upload for Saving with gr.Column(): uploaded_file = gr.File(label="Upload File") save_button = gr.Button(value="Save File") output_text = gr.Textbox(label="File URL") # Random File Generation (Optional) with gr.Column(): generate_button = gr.Button(value="Generate Random Files") save_button.click(lambda: output_text.setValue(saveFile(uploaded_file)) if uploaded_file else None) generate_button.click(lambda: [generate_random_files(d) for d in ["/home/user/app", "/data"]]) demo.launch()