Suzana commited on
Commit
045377e
·
verified ·
1 Parent(s): d304161

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -31
app.py CHANGED
@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
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
@@ -15,17 +15,21 @@ def upload_csv(file):
15
  df = pd.read_csv(file.name)
16
  if not {"text","label"}.issubset(df.columns):
17
  return (
18
- None,
19
  "❌ CSV must contain 'text' and 'label' columns.",
20
- gr.update(visible=False), # hide action buttons
21
- gr.update(visible=False), # hide push
 
 
22
  )
23
  df["label"] = df["label"].fillna("")
24
  return (
25
  df[["text","label"]],
26
  "✅ Uploaded! You can now annotate and use the buttons below.",
27
- gr.update(visible=True), # show action buttons
28
- gr.update(visible=True), # show push accordion
 
 
29
  )
30
 
31
  def save_changes(table):
@@ -45,10 +49,9 @@ def make_figure():
45
  fig, (ax1, ax2) = plt.subplots(
46
  ncols=2,
47
  gridspec_kw={"width_ratios":[1,2]},
48
- figsize=(8, max(2,len(labels)*0.4)),
49
  tight_layout=True
50
  )
51
- # table
52
  ax1.axis("off")
53
  tbl = ax1.table(
54
  cellText=[[l,v] for l,v in zip(labels,values)],
@@ -56,16 +59,15 @@ def make_figure():
56
  loc="center"
57
  )
58
  tbl.auto_set_font_size(False); tbl.set_fontsize(10); tbl.scale(1,1.2)
59
- # bar chart
60
  ax2.barh(labels, values, color="#222222")
61
  ax2.invert_yaxis(); ax2.set_xlabel("Count")
62
  return fig
63
 
64
  def visualize_and_download():
65
  fig = make_figure()
66
- out_png = "label_distribution.png"
67
- fig.savefig(out_png, dpi=150, bbox_inches="tight")
68
- return fig, out_png
69
 
70
  def push_to_hub(repo_name, hf_token):
71
  global df
@@ -93,47 +95,51 @@ 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_in = gr.File(label="📁 Upload CSV", file_types=[".csv"])
99
  upload_btn = gr.Button("Upload")
100
 
101
- # Editable 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: Action buttons (hidden initially)
106
- with gr.Row(visible=False) as actions:
107
- save_btn = gr.Button("💾 Save")
108
- download_csv_btn = gr.DownloadButton(fn=download_csv, label="⬇️ Download CSV", file_name="annotated_data.csv")
109
- download_png_btn = gr.DownloadButton(fn=visualize_and_download, label="⬇️ Download Chart", file_name="label_distribution.png")
110
- visualize_btn = gr.Button("📊 Visualize")
111
 
112
- chart_plot = gr.Plot(visible=False)
 
 
113
 
114
- # Push accordion
115
- push_acc = gr.Accordion("📦 Push to Hugging Face Hub", open=False, visible=False)
116
  with push_acc:
117
  repo_in = gr.Textbox(label="Repo (username/dataset)")
118
  token_in = gr.Textbox(label="🔑 HF Token", type="password")
119
  push_btn = gr.Button("🚀 Push")
120
  push_out = gr.Textbox(label="Push Status", interactive=False)
121
 
122
- # Event wiring
123
  upload_btn.click(
124
  upload_csv,
125
- inputs=csv_in,
126
- outputs=[table, status, actions, push_acc]
127
  )
128
  save_btn.click(
129
  save_changes,
130
  inputs=table,
131
  outputs=status
132
  )
 
 
 
 
133
  visualize_btn.click(
134
  visualize_and_download,
135
- inputs=None,
136
- outputs=[chart_plot, download_png_btn]
137
  )
138
  push_btn.click(
139
  push_to_hub,
 
4
  from pathlib import Path
5
  from huggingface_hub import HfApi, Repository
6
 
7
+ # Matplotlib styling
8
  plt.rcParams.update({"font.family":"sans-serif","font.size":10})
9
 
10
  # Global DataFrame
 
15
  df = pd.read_csv(file.name)
16
  if not {"text","label"}.issubset(df.columns):
17
  return (
18
+ None,
19
  "❌ CSV must contain 'text' and 'label' columns.",
20
+ gr.update(visible=False), # Save
21
+ gr.update(visible=False), # Download CSV
22
+ gr.update(visible=False), # Visualize
23
+ gr.update(visible=False), # Push accordion
24
  )
25
  df["label"] = df["label"].fillna("")
26
  return (
27
  df[["text","label"]],
28
  "✅ Uploaded! You can now annotate and use the buttons below.",
29
+ gr.update(visible=True), # Save
30
+ gr.update(visible=True), # Download CSV
31
+ gr.update(visible=True), # Visualize
32
+ gr.update(visible=True), # Push accordion
33
  )
34
 
35
  def save_changes(table):
 
49
  fig, (ax1, ax2) = plt.subplots(
50
  ncols=2,
51
  gridspec_kw={"width_ratios":[1,2]},
52
+ figsize=(8, max(2, len(labels)*0.4)),
53
  tight_layout=True
54
  )
 
55
  ax1.axis("off")
56
  tbl = ax1.table(
57
  cellText=[[l,v] for l,v in zip(labels,values)],
 
59
  loc="center"
60
  )
61
  tbl.auto_set_font_size(False); tbl.set_fontsize(10); tbl.scale(1,1.2)
 
62
  ax2.barh(labels, values, color="#222222")
63
  ax2.invert_yaxis(); ax2.set_xlabel("Count")
64
  return fig
65
 
66
  def visualize_and_download():
67
  fig = make_figure()
68
+ png_path = "label_distribution.png"
69
+ fig.savefig(png_path, dpi=150, bbox_inches="tight")
70
+ return fig, png_path
71
 
72
  def push_to_hub(repo_name, hf_token):
73
  global df
 
95
  gr.Markdown("## 🏷️ Label It! Text Annotation Tool")
96
  gr.Markdown("Upload a `.csv` (columns: **text**, **label**), then annotate, export, visualize, or push.")
97
 
98
+ # Step 1: Upload
99
  with gr.Row():
100
+ csv_input = gr.File(label="📁 Upload CSV", file_types=[".csv"])
101
  upload_btn = gr.Button("Upload")
102
 
103
+ # Table + status
104
+ table = gr.Dataframe(headers=["text","label"], interactive=True, visible=False)
105
+ status = gr.Textbox(label="Status", interactive=False)
106
 
107
+ # Step 2: Actions (hidden initially)
108
+ with gr.Row(visible=False) as action_row:
109
+ save_btn = gr.Button("💾 Save")
110
+ download_btn = gr.Button("⬇️ Download CSV")
111
+ visualize_btn= gr.Button("📊 Visualize")
 
112
 
113
+ download_csv_out = gr.File(label="📥 Downloaded CSV", interactive=False)
114
+ chart_plot = gr.Plot(label="Label Distribution", visible=False)
115
+ download_chart_out = gr.File(label="📥 Downloaded Chart", interactive=False)
116
 
117
+ # Push controls
118
+ push_acc = gr.Accordion("📦 Push to Hugging Face Hub", open=False, visible=False)
119
  with push_acc:
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_out = 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, save_btn, download_btn, visualize_btn, push_acc]
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,