| 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() | |