Spaces:
Running
Running
File size: 2,080 Bytes
1db582e af7c474 a924ad2 af7c474 c67ad58 a924ad2 c67ad58 1db582e af7c474 1db582e af7c474 c67ad58 737d007 3cb650b a924ad2 3cb650b a924ad2 3cb650b c67ad58 a924ad2 3cb650b a924ad2 3cb650b c67ad58 a924ad2 3cb650b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import gradio as gr
from uuid import uuid4
from fileManager import FileManager
import re
import os
import random
import string
import glob
##############################################################
# File Manager Integration
##############################################################
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 Text File Generation Function
##############################################################
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_text_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 File Management and Text Generation
##############################################################
with gr.Blocks() as demo:
# File Explorer for Upload (using fileManager integration)
gr.File(label="Upload File", upload=True, fn=saveFile)
# File Explorer for Existing Files (persistent storage)
working_dir = gr.FileExplorer(label="Working directory")
# Generate Random Text Files Button
generate_button = gr.Button("Generate Random Text Files")
@generate_button.click
def generate_files(event):
# Generate text files in working directory chosen by user
generate_text_files(working_dir.value)
gr.FileExplorer(root=working_dir.value, label="Persistent storage")
demo.launch()
|