File size: 1,910 Bytes
c55f56b 4b00e94 c55f56b 4b00e94 36586c0 c55f56b 36586c0 c55f56b 96136f3 4b00e94 96136f3 36586c0 96136f3 4b00e94 96136f3 4b00e94 96136f3 c55f56b 36586c0 4b00e94 c55f56b 96136f3 36586c0 c55f56b 96136f3 36586c0 c55f56b 36586c0 054cce1 c55f56b 96136f3 c55f56b 96136f3 c55f56b | 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 | import gradio as gr
from huggingface_hub import HfApi, list_repo_files
import os
# 1. This grabs the Secret Key you just re-saved
api = HfApi(token=os.getenv("HF_TOKEN"))
# 2. This is the house address for your files
REPO_ID = "abhy60098/my-cloud-storage"
def upload_file(file):
if file is None:
return "β Please select a file first.", get_files()
try:
file_name = os.path.basename(file.name)
# 3. This moves the file into your dataset
api.upload_file(
path_or_fileobj=file.name,
path_in_repo=file_name,
repo_id=REPO_ID,
repo_type="dataset"
)
return f"β
Success! {file_name} is now in the cloud.", get_files()
except Exception as e:
# If the key is wrong, it tells us here
return f"β Error: {str(e)}", get_files()
def get_files():
try:
files = list_repo_files(repo_id=REPO_ID, repo_type="dataset")
# Hide the system files
clean_list = [f for f in files if not f.startswith(".") and f != "README.md"]
if not clean_list: return "The storage is currently empty."
# Create clickable links
links = [f"π [{f}](https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f})" for f in clean_list]
return "\n".join(links)
except:
return "Connecting to storage..."
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# βοΈ My Public Cloud Storage")
with gr.Row():
with gr.Column():
file_input = gr.File(label="Upload any file")
btn = gr.Button("π Upload to Dataset", variant="primary")
status = gr.Textbox(label="Status")
with gr.Column():
gr.Markdown("### π Your Files")
file_display = gr.Markdown(get_files())
btn.click(upload_file, inputs=file_input, outputs=[status, file_display])
demo.launch() |