CLOUD / app.py
abhy60098's picture
Update app.py
4b00e94 verified
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()