File size: 3,059 Bytes
9a9513f
 
 
89d1d2c
9a9513f
c11c6ac
 
 
f0f051d
 
c11c6ac
f42f0bd
 
 
 
 
 
 
 
 
 
 
 
 
c11c6ac
 
 
 
 
 
 
 
 
 
0a9f5a4
c11c6ac
 
 
f42f0bd
0a9f5a4
 
770c132
0a9f5a4
 
c11c6ac
 
 
 
 
 
0a9f5a4
c11c6ac
 
 
9a9513f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfdbf8a
 
f2b7407
 
 
9a9513f
 
8120c3f
9a9513f
8aaa394
9a9513f
71f2248
1f71a96
c11c6ac
 
 
 
 
 
dfdbf8a
c11c6ac
0a9f5a4
9a9513f
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import gradio as gr
import tempfile
from PIL import Image
import uuid 
import os
import shutil
from pathlib import Path
import random
import string
import glob


# Set the environment variable
os.environ['GRADIO_TEMP_DIR'] = '/data'
gradio_temp_dir = os.environ['GRADIO_TEMP_DIR'] 

#print(f'GRADIO_TEMP_DIR is = {gradio_temp_dir}')
#absolute_path1 = os.path.abspath(gradio_temp_dir)
#print(f"The absolute path of gradio_temp_dir is = {absolute_path1}")

# Get the path of the system's temporary directory
temp_directory = tempfile.gettempdir()
print(f"System's temporary directory is: {temp_directory}")

def create_local():
    length = 100
    text_string = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))
    #rand_num = random.randint(1, 10000)
    file_name = os.path.join('temp_dir', f'random_file_1.txt')
    with open(file_name, 'w') as f:
        for _ in range(10):
            f.write(text_string + '\n')
    

def movefiles():
    # Get the path of the system's temporary directory
    temp_directory = tempfile.gettempdir()
    
    # Specify the path of the file to be moved
    directory_name = 'temp_dir'
    absolute_path = os.path.abspath(directory_name)
    file_path = 'random_file_1.txt'
    old_path = os.path.join(absolute_path, os.path.basename(file_path))

    # Construct the new path in the temporary directory with the same filename
    new_path = os.path.join(temp_directory, os.path.basename(file_path))
    
    print(f'new_path - {new_path}')
    
    # Move the file
    shutil.move(old_path, new_path)
    
    print(f"Moved the file to: {new_path}")


def create():
    """
    Create a blank image with the specified dimensions, color, and filename.
    """
    color='blue'
    width=512
    height=512
    # Create a temporary file
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    print(f"Temporary file created at: {temp_file.name}")
    # Create a new image with the given mode and size
    image = Image.new("RGB", (width, height), color)
    # Save the image to disk
    image.save(temp_file, format='PNG')

    # List all entries in the directory
    temp_directory_file_list = os.listdir(temp_directory)
    print(f'temp_directory_file_list is = {temp_directory_file_list}')
    return temp_file.name, temp_directory_file_list


with gr.Blocks(delete_cache=(5,10),) as demo:
    with gr.Row():
        inp = gr.Textbox(placeholder="Temporary files at location specified with GRADIO_TEMP_DIR ")
        out = gr.Image(type='filepath')
        gr.FileExplorer(root="/data", label="Persistent storage")
    with gr.Row(equal_height=False):
        with gr.Column():
            btn = gr.Button("Generate temp files!")
            btn1 = gr.Button("Generate local files!")
        gr.FileExplorer(label="local storage")
        btn2 = gr.Button("Move files to temp dir!")
        
    btn.click(create,inputs=[],outputs=[out, inp])
    btn1.click(create_local,inputs=[],outputs=[])
    btn2.click(movefiles,inputs=[],outputs=[])

demo.launch(debug=False)