File size: 1,366 Bytes
6e9731a | 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 | import gradio as gr
import os
import requests
from huggingface_hub import HfApi
# ===== Cấu hình =====
import gradio as gr
from 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()
|