| import gradio as gr |
| from huggingface_hub import HfApi, list_repo_files |
| import os |
|
|
| |
| api = HfApi(token=os.getenv("HF_TOKEN")) |
|
|
| |
| 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) |
| |
| 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: |
| |
| return f"β Error: {str(e)}", get_files() |
|
|
| def get_files(): |
| try: |
| files = list_repo_files(repo_id=REPO_ID, repo_type="dataset") |
| |
| 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." |
| |
| 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() |