datsss commited on
Commit
280874a
·
verified ·
1 Parent(s): 8a7e946

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+ from huggingface_hub import HfApi
5
+
6
+ # ===== Cấu hình =====
7
+ import gradio as gr
8
+ from huggingface_hub import HfApi
9
+
10
+ HF_TOKEN = os.getenv("HF_TOKEN", "")
11
+
12
+ def push_file_to_hf(repo, file):
13
+ if not HF_TOKEN:
14
+ return "❌ HF_TOKEN chưa thiết lập."
15
+
16
+ api = HfApi(token=HF_TOKEN)
17
+
18
+ try:
19
+ # path_in_repo = giữ nguyên tên file upload
20
+ filename = file.name.split("/")[-1]
21
+ api.upload_file(
22
+ path_or_fileobj=file.name,
23
+ path_in_repo=filename,
24
+ repo_id=repo,
25
+ repo_type="dataset",
26
+ )
27
+ return f"✅ Upload xong: {filename} vào repo {repo}"
28
+ except Exception as e:
29
+ return f"❌ Lỗi khi upload: {str(e)}"
30
+
31
+ # ===== Gradio UI =====
32
+ with gr.Blocks() as app:
33
+ gr.Markdown("## 🚀 Upload file lên Hugging Face Dataset")
34
+ gr.Markdown("Nhập repo Hugging Face (ví dụ: username/my-dataset) và chọn file để upload.")
35
+
36
+ repo_input = gr.Textbox(label="HF Dataset Repo", placeholder="username/my-dataset")
37
+ file_input = gr.File(label="Chọn file cần upload")
38
+ output_box = gr.Textbox(label="Kết quả", interactive=False)
39
+ submit_btn = gr.Button("Upload")
40
+
41
+ submit_btn.click(
42
+ push_file_to_hf,
43
+ inputs=[repo_input, file_input],
44
+ outputs=output_box
45
+ )
46
+
47
+ app.launch()
48
+