Suzana commited on
Commit
729db5b
·
verified ·
1 Parent(s): 2dccd10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -57
app.py CHANGED
@@ -4,10 +4,8 @@ import matplotlib.pyplot as plt
4
  from pathlib import Path
5
  from huggingface_hub import HfApi, Repository
6
 
7
- plt.rcParams.update({
8
- "font.family": "sans-serif",
9
- "font.size": 10,
10
- })
11
 
12
  # Global DataFrame
13
  df = pd.DataFrame()
@@ -17,21 +15,19 @@ def upload_csv(file):
17
  df = pd.read_csv(file.name)
18
  if "text" not in df.columns or "label" not in df.columns:
19
  return (
20
- None, # table
21
  "❌ CSV must contain 'text' and 'label' columns.",
22
- gr.update(visible=False), # save
23
- gr.update(visible=False), # download CSV
24
- gr.update(visible=False), # visualize
25
- gr.update(visible=False), # push accordion
26
  )
27
  df["label"] = df["label"].fillna("")
28
  return (
29
- df[["text","label"]],
30
- "✅ File uploaded you can now annotate and use the buttons below.",
31
- gr.update(visible=True),
32
- gr.update(visible=True),
33
- gr.update(visible=True),
34
- gr.update(visible=True),
35
  )
36
 
37
  def save_changes(table):
@@ -45,14 +41,13 @@ def download_csv():
45
  df.to_csv(path, index=False)
46
  return path
47
 
48
- def create_distribution_figure():
49
  global df
50
  counts = df["label"].value_counts().sort_values(ascending=False)
51
  labels, values = counts.index.tolist(), counts.values.tolist()
52
-
53
  fig, (ax_table, ax_bar) = plt.subplots(
54
  ncols=2,
55
- gridspec_kw={"width_ratios": [1,2]},
56
  figsize=(8, max(2, len(labels)*0.4)),
57
  tight_layout=True
58
  )
@@ -66,11 +61,11 @@ def create_distribution_figure():
66
  ax_bar.invert_yaxis(); ax_bar.set_xlabel("Count")
67
  return fig
68
 
69
- def visualize_and_download_chart():
70
- fig = create_distribution_figure()
71
- out_path = "label_distribution.png"
72
- fig.savefig(out_path, dpi=150, bbox_inches="tight")
73
- return fig, out_path
74
 
75
  def push_to_hub(repo_name, hf_token):
76
  global df
@@ -89,53 +84,67 @@ def push_to_hub(repo_name, hf_token):
89
  use_auth_token=hf_token
90
  )
91
  df.to_csv(local_dir/"data.csv", index=False)
92
- repo.push_to_hub(commit_message="📑 Update annotated data")
93
- return f"🚀 Pushed to https://huggingface.co/datasets/{repo_name}"
94
  except Exception as e:
95
  return f"❌ Push failed: {e}"
96
 
97
- with gr.Blocks(theme=gr.themes.Default()) as app:
98
- gr.Markdown("## 🏷️ Label It! Text Annotation Tool\n"
99
- "Upload a `.csv` (with **text** + **label** columns), "
100
- "then annotate, export, visualize, or publish.")
101
-
102
  # Step 1: Upload
103
  with gr.Row():
104
- file_input = gr.File(label="📁 Upload CSV", file_types=[".csv"])
105
  upload_btn = gr.Button("Upload")
106
-
107
- # Editable table
108
  table = gr.Dataframe(headers=["text","label"], interactive=True, visible=False)
109
  status = gr.Textbox(label="Status", interactive=False)
110
-
111
- # Step 2 buttons (hidden initially)
112
  with gr.Row(visible=False) as action_row:
113
- save_btn = gr.Button("💾 Save")
114
- download_btn = gr.Button("⬇️ Download CSV")
115
- visualize_btn= gr.Button("📊 Visualize Distribution")
116
- download_csv_out = gr.File(label="📥 Download CSV")
 
 
 
117
  chart_plot = gr.Plot(label="Label Distribution")
118
- download_chart_out = gr.File(label="📥 Download Chart")
119
-
120
- # Push accordion
121
- push_acc = gr.Accordion("📦 Push to Hugging Face Hub", open=False, visible=False)
122
- with push_acc:
123
- repo_in = gr.Textbox(label="Repo (username/dataset-name)")
124
- token_in = gr.Textbox(label="🔑 HF Token", type="password")
125
- push_btn = gr.Button("🚀 Push")
126
  push_status = gr.Textbox(label="Push Status", interactive=False)
127
-
128
- # Event bindings
129
  upload_btn.click(
130
  upload_csv,
131
- inputs=file_input,
132
- outputs=[table, status,
133
- save_btn, download_btn, visualize_btn, push_acc]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  )
135
- save_btn.click(save_changes, inputs=table, outputs=status)
136
- download_btn.click(download_csv, outputs=download_csv_out)
137
- visualize_btn.click(visualize_and_download_chart,
138
- outputs=[chart_plot, download_chart_out])
139
- push_btn.click(push_to_hub, inputs=[repo_in, token_in], outputs=push_status)
140
 
141
  app.launch()
 
4
  from pathlib import Path
5
  from huggingface_hub import HfApi, Repository
6
 
7
+ # Matplotlib style
8
+ plt.rcParams.update({"font.family":"sans-serif", "font.size":10})
 
 
9
 
10
  # Global DataFrame
11
  df = pd.DataFrame()
 
15
  df = pd.read_csv(file.name)
16
  if "text" not in df.columns or "label" not in df.columns:
17
  return (
18
+ None, # hide table
19
  "❌ CSV must contain 'text' and 'label' columns.",
20
+ gr.update(visible=False), # hide actions
21
+ gr.update(visible=False), # hide outputs
22
+ gr.update(visible=False), # hide push accordion
 
23
  )
24
  df["label"] = df["label"].fillna("")
25
  return (
26
+ df[["text","label"]], # show table
27
+ "✅ Uploaded! Edit below or use the buttons.",
28
+ gr.update(visible=True), # show action row
29
+ gr.update(visible=True), # show output row
30
+ gr.update(visible=True), # show push accordion
 
31
  )
32
 
33
  def save_changes(table):
 
41
  df.to_csv(path, index=False)
42
  return path
43
 
44
+ def make_figure():
45
  global df
46
  counts = df["label"].value_counts().sort_values(ascending=False)
47
  labels, values = counts.index.tolist(), counts.values.tolist()
 
48
  fig, (ax_table, ax_bar) = plt.subplots(
49
  ncols=2,
50
+ gridspec_kw={"width_ratios":[1,2]},
51
  figsize=(8, max(2, len(labels)*0.4)),
52
  tight_layout=True
53
  )
 
61
  ax_bar.invert_yaxis(); ax_bar.set_xlabel("Count")
62
  return fig
63
 
64
+ def visualize_and_download():
65
+ fig = make_figure()
66
+ png_path = "label_distribution.png"
67
+ fig.savefig(png_path, dpi=150, bbox_inches="tight")
68
+ return fig, png_path
69
 
70
  def push_to_hub(repo_name, hf_token):
71
  global df
 
84
  use_auth_token=hf_token
85
  )
86
  df.to_csv(local_dir/"data.csv", index=False)
87
+ repo.push_to_hub(commit_message="📑 Updated data")
88
+ return f"🚀 Pushed to datasets/{repo_name}"
89
  except Exception as e:
90
  return f"❌ Push failed: {e}"
91
 
92
+ with gr.Blocks() as app:
93
+ gr.Markdown("## 🏷️ Label It! Text Annotation Tool")
94
+ gr.Markdown("Upload a `.csv` (columns: **text**, **label**), then annotate, export, visualize, or push.")
95
+
 
96
  # Step 1: Upload
97
  with gr.Row():
98
+ csv_input = gr.File(label="📁 Upload CSV", file_types=[".csv"])
99
  upload_btn = gr.Button("Upload")
100
+
101
+ # Table + Status
102
  table = gr.Dataframe(headers=["text","label"], interactive=True, visible=False)
103
  status = gr.Textbox(label="Status", interactive=False)
104
+
105
+ # Step 2: Actions (hidden initially)
106
  with gr.Row(visible=False) as action_row:
107
+ save_btn = gr.Button("💾 Save")
108
+ download_btn = gr.Button("⬇️ Download CSV")
109
+ visualize_btn = gr.Button("📊 Visualize")
110
+
111
+ # Outputs row (hidden initially)
112
+ with gr.Row(visible=False) as output_row:
113
+ download_csv_out = gr.File(label="📥 CSV File")
114
  chart_plot = gr.Plot(label="Label Distribution")
115
+ download_chart_out = gr.File(label="📥 Chart PNG")
116
+
117
+ # Push section
118
+ push_accordion = gr.Accordion("📦 Push to Hugging Face Hub", open=False, visible=False)
119
+ with push_accordion:
120
+ repo_in = gr.Textbox(label="Repo (username/dataset)")
121
+ token_in = gr.Textbox(label="🔑 HF Token", type="password")
122
+ push_btn = gr.Button("🚀 Push")
123
  push_status = gr.Textbox(label="Push Status", interactive=False)
124
+
125
+ # Bind events
126
  upload_btn.click(
127
  upload_csv,
128
+ inputs=csv_input,
129
+ outputs=[table, status, action_row, output_row, push_accordion]
130
+ )
131
+ save_btn.click(
132
+ save_changes,
133
+ inputs=table,
134
+ outputs=status
135
+ )
136
+ download_btn.click(
137
+ download_csv,
138
+ outputs=download_csv_out
139
+ )
140
+ visualize_btn.click(
141
+ visualize_and_download,
142
+ outputs=[chart_plot, download_chart_out]
143
+ )
144
+ push_btn.click(
145
+ push_to_hub,
146
+ inputs=[repo_in, token_in],
147
+ outputs=push_status
148
  )
 
 
 
 
 
149
 
150
  app.launch()