Suzana commited on
Commit
d18e6c8
·
verified ·
1 Parent(s): 1d6c7cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -27
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import pandas as pd
3
  import io
4
  import os
 
5
  from huggingface_hub import HfApi, Repository
6
 
7
  # Global DataFrame
@@ -11,12 +12,12 @@ def upload_csv(file):
11
  global df
12
  df = pd.read_csv(file.name)
13
  if "text" not in df.columns or "label" not in df.columns:
14
- return gr.update(visible=False), "❌ CSV must have 'text' and 'label' columns."
15
  df["label"] = df["label"].fillna("")
16
- # Show the dataframe and status
17
  return (
18
  gr.update(value=df[["text","label"]], visible=True),
19
- "✅ File uploaded — you can now edit the labels below."
20
  )
21
 
22
  def save_changes(edited_table):
@@ -26,20 +27,41 @@ def save_changes(edited_table):
26
 
27
  def download_csv():
28
  global df
29
- csv_bytes = df.to_csv(index=False).encode("utf-8")
30
- return gr.File.update(value=io.BytesIO(csv_bytes), filename="annotated_data.csv")
 
31
 
32
- def push_to_hub(repo_name, hf_token):
33
  global df
34
- repo_url = f"https://huggingface.co/datasets/{repo_name}"
35
- local_path = f"./{repo_name.replace('/', '_')}"
36
- if os.path.exists(local_path):
37
- os.system(f"rm -rf {local_path}")
38
- HfApi().create_repo(repo_id=repo_name, token=hf_token, repo_type="dataset", exist_ok=True)
39
- repo = Repository(local_dir=local_path, clone_from=repo_url, token=hf_token)
40
- df.to_csv(f"{local_path}/data.csv", index=False)
41
- repo.push_to_hub()
42
- return f"🚀 Data pushed to {repo_url}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  with gr.Blocks(theme=gr.themes.Default()) as app:
45
  gr.Markdown("## 🏷️ Label It! Text Annotation Tool")
@@ -51,16 +73,16 @@ with gr.Blocks(theme=gr.themes.Default()) as app:
51
 
52
  df_table = gr.Dataframe(
53
  headers=["text","label"],
 
54
  interactive=True,
55
- visible=False,
56
- label="📝 Editable Table"
57
  )
58
  status = gr.Textbox(label="Status", interactive=False)
59
 
60
  with gr.Row():
61
- save_btn = gr.Button("💾 Save")
62
- download_btn= gr.Button("⬇️ Download CSV")
63
- download_out= gr.File(label="Download")
64
 
65
  with gr.Accordion("📦 Push to Hugging Face Hub", open=False):
66
  repo_input = gr.Textbox(label="Repo (username/dataset-name)")
@@ -68,12 +90,11 @@ with gr.Blocks(theme=gr.themes.Default()) as app:
68
  push_btn = gr.Button("🚀 Push")
69
  push_status = gr.Textbox(label="Push Status", interactive=False)
70
 
71
- # ------------------------
72
- upload_btn.click(upload_csv, inputs=file_input, outputs=[df_table, status])
73
- save_btn.click( save_changes, inputs=df_table, outputs=status)
74
  download_btn.click(download_csv, outputs=download_out)
75
- push_btn.click( push_to_hub, inputs=[repo_input,token_input], outputs=push_status)
76
- # ------------------------
77
 
78
- # Launch the app
79
- app.launch()
 
2
  import pandas as pd
3
  import io
4
  import os
5
+ from pathlib import Path
6
  from huggingface_hub import HfApi, Repository
7
 
8
  # Global DataFrame
 
12
  global df
13
  df = pd.read_csv(file.name)
14
  if "text" not in df.columns or "label" not in df.columns:
15
+ return gr.update(visible=False), "❌ CSV must contain text and label columns."
16
  df["label"] = df["label"].fillna("")
17
+ # Show the table and set status
18
  return (
19
  gr.update(value=df[["text","label"]], visible=True),
20
+ "✅ File uploaded — you can now edit labels."
21
  )
22
 
23
  def save_changes(edited_table):
 
27
 
28
  def download_csv():
29
  global df
30
+ out_path = "annotated_data.csv"
31
+ df.to_csv(out_path, index=False)
32
+ return out_path
33
 
34
+ def push_to_hub(repo_name: str, hf_token: str) -> str:
35
  global df
36
+ try:
37
+ api = HfApi()
38
+ api.create_repo(
39
+ repo_id=repo_name,
40
+ token=hf_token,
41
+ repo_type="dataset",
42
+ exist_ok=True
43
+ )
44
+
45
+ local_dir = Path(f"./{repo_name.replace('/', '_')}")
46
+ if local_dir.exists():
47
+ for child in local_dir.iterdir():
48
+ child.unlink()
49
+ local_dir.rmdir()
50
+
51
+ repo = Repository(
52
+ local_dir=str(local_dir),
53
+ clone_from=repo_name,
54
+ use_auth_token=hf_token
55
+ )
56
+
57
+ csv_path = local_dir / "data.csv"
58
+ df.to_csv(csv_path, index=False)
59
+
60
+ repo.push_to_hub(commit_message="📑 Update annotated data")
61
+ return f"🚀 Pushed to https://huggingface.co/datasets/{repo_name}"
62
+
63
+ except Exception as e:
64
+ return f"❌ Push failed: {e}"
65
 
66
  with gr.Blocks(theme=gr.themes.Default()) as app:
67
  gr.Markdown("## 🏷️ Label It! Text Annotation Tool")
 
73
 
74
  df_table = gr.Dataframe(
75
  headers=["text","label"],
76
+ label="📝 Editable Table",
77
  interactive=True,
78
+ visible=False
 
79
  )
80
  status = gr.Textbox(label="Status", interactive=False)
81
 
82
  with gr.Row():
83
+ save_btn = gr.Button("💾 Save")
84
+ download_btn = gr.Button("⬇️ Download CSV")
85
+ download_out = gr.File(label="📥 Downloaded File")
86
 
87
  with gr.Accordion("📦 Push to Hugging Face Hub", open=False):
88
  repo_input = gr.Textbox(label="Repo (username/dataset-name)")
 
90
  push_btn = gr.Button("🚀 Push")
91
  push_status = gr.Textbox(label="Push Status", interactive=False)
92
 
93
+ # Event bindings
94
+ upload_btn.click(upload_csv, inputs=file_input, outputs=[df_table, status])
95
+ save_btn.click( save_changes, inputs=df_table, outputs=status)
96
  download_btn.click(download_csv, outputs=download_out)
97
+ push_btn.click( push_to_hub, inputs=[repo_input, token_input], outputs=push_status)
 
98
 
99
+ # Launch the app
100
+ app.launch()