Upload folder using huggingface_hub
Browse files123456789101112---title: Civiai Downloaderemoji: 🚀colorFrom: redcolorTo: redsdk: gradiosdk_version: 5.49.1app_file: app.pypinned: false---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import gradio as grimport osimport requestsfrom huggingface_hub import HfApi
# ===== Cấu hình =====import gradio as grfrom huggingface_hub import HfApi
HF_TOKEN = os.getenv("HF_TOKEN", "")
def push_file_to_hf(repo, file): if not HF_TOKEN: return "❌ HF_TOKEN chưa thiết lập."
api = HfApi(token=HF_TOKEN)
try: # path_in_repo = giữ nguyên tên file upload filename = file.name.split("/")[-1] api.upload_file( path_or_fileobj=file.name, path_in_repo=filename, repo_id=repo, repo_type="dataset", ) return f"✅ Upload xong: {filename} vào repo {repo}" except Exception as e: return f"❌ Lỗi khi upload: {str(e)}"
# ===== Gradio UI =====with gr.Blocks() as app: gr.Markdown("## 🚀 Upload file lên Hugging Face Dataset") gr.Markdown("Nhập repo Hugging Face (ví dụ: username/my-dataset) và chọn file để upload.")
repo_input = gr.Textbox(label="HF Dataset Repo", placeholder="username/my-dataset") file_input = gr.File(label="Chọn file cần upload") output_box = gr.Textbox(label="Kết quả", interactive=False) submit_btn = gr.Button("Upload")
submit_btn.click( push_file_to_hf, inputs=[repo_input, file_input], outputs=output_box )
app.launch()
README.mdAdded+12
app.pyAdded+48