File size: 1,243 Bytes
6df2409
 
 
 
 
 
 
 
 
5a64982
6df2409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil

from apscheduler.schedulers.background import BackgroundScheduler
import gradio as gr
from huggingface_hub import HfApi


api = HfApi(token=os.environ.get("HF_TOKEN"))
os.makedirs("tmp/test_data")

counter = 0   # For name of file within one upload
counter_2 = 0 # For name of each zip file

def create_file():
    """Stores a file locally
    """
    global counter 
    path = f"tmp/test_data/f{counter}.txt"
    f = open(path, "w")
    f.write("Test")
    f.close()
    counter += 1
    return f"File saved! {path}"

def upload_files():
    """Zips files and uploads to dataset. Local data is deleted
    """
    global counter, counter_2
    shutil.make_archive("tmp/result", 'zip', "tmp/test_data")
    api.upload_file(
        path_or_fileobj="tmp/result.zip",
        path_in_repo=f"result{counter_2}.zip",
        repo_id="osanseviero/space_sync_batch",
        repo_type="dataset",
    )
    counter_2 += 1
    shutil.rmtree("tmp")
    os.makedirs("tmp/test_data")
    
    counter = 0


scheduler = BackgroundScheduler()
# Refresh every minute
scheduler.add_job(func=upload_files, trigger="interval", seconds=60)
scheduler.start()

demo = gr.Interface(fn=create_file, inputs=[], outputs="text")
demo.launch()