Spaces:
Sleeping
Sleeping
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() |