Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from pathlib import Path
|
|
5 |
from huggingface_hub import HfApi, Repository
|
6 |
|
7 |
# Matplotlib style
|
8 |
-
plt.rcParams.update({"font.family":"sans-serif",
|
9 |
|
10 |
# Global DataFrame
|
11 |
df = pd.DataFrame()
|
@@ -13,21 +13,19 @@ df = pd.DataFrame()
|
|
13 |
def upload_csv(file):
|
14 |
global df
|
15 |
df = pd.read_csv(file.name)
|
16 |
-
if "text"
|
17 |
return (
|
18 |
-
None,
|
19 |
"❌ CSV must contain 'text' and 'label' columns.",
|
20 |
-
gr.update(visible=False),
|
21 |
-
gr.update(visible=False),
|
22 |
-
gr.update(visible=False), # hide push accordion
|
23 |
)
|
24 |
df["label"] = df["label"].fillna("")
|
25 |
return (
|
26 |
-
df[["text","label"]],
|
27 |
-
"✅ Uploaded!
|
28 |
-
gr.update(visible=True),
|
29 |
-
gr.update(visible=True),
|
30 |
-
gr.update(visible=True), # show push accordion
|
31 |
)
|
32 |
|
33 |
def save_changes(table):
|
@@ -42,30 +40,32 @@ def download_csv():
|
|
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
|
48 |
-
fig, (
|
49 |
ncols=2,
|
50 |
gridspec_kw={"width_ratios":[1,2]},
|
51 |
-
figsize=(8, max(2,
|
52 |
tight_layout=True
|
53 |
)
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
58 |
tbl.auto_set_font_size(False); tbl.set_fontsize(10); tbl.scale(1,1.2)
|
59 |
-
#
|
60 |
-
|
61 |
-
|
62 |
return fig
|
63 |
|
64 |
def visualize_and_download():
|
65 |
fig = make_figure()
|
66 |
-
|
67 |
-
fig.savefig(
|
68 |
-
return fig,
|
69 |
|
70 |
def push_to_hub(repo_name, hf_token):
|
71 |
global df
|
@@ -84,7 +84,7 @@ def push_to_hub(repo_name, hf_token):
|
|
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}"
|
@@ -93,58 +93,52 @@ 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 |
-
#
|
97 |
with gr.Row():
|
98 |
-
|
99 |
upload_btn = gr.Button("Upload")
|
100 |
|
101 |
-
#
|
102 |
-
table
|
103 |
-
status
|
104 |
-
|
105 |
-
#
|
106 |
-
with gr.Row(visible=False) as
|
107 |
-
save_btn
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
push_status = gr.Textbox(label="Push Status", interactive=False)
|
124 |
-
|
125 |
-
# Bind events
|
126 |
upload_btn.click(
|
127 |
upload_csv,
|
128 |
-
inputs=
|
129 |
-
outputs=[table, status,
|
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 |
-
|
|
|
143 |
)
|
144 |
push_btn.click(
|
145 |
push_to_hub,
|
146 |
inputs=[repo_in, token_in],
|
147 |
-
outputs=
|
148 |
)
|
149 |
|
150 |
app.launch()
|
|
|
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()
|
|
|
13 |
def upload_csv(file):
|
14 |
global df
|
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):
|
|
|
40 |
return path
|
41 |
|
42 |
def make_figure():
|
|
|
43 |
counts = df["label"].value_counts().sort_values(ascending=False)
|
44 |
+
labels, values = list(counts.index), list(counts.values)
|
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)],
|
55 |
+
colLabels=["Label","Count"],
|
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
|
|
|
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 annotated data")
|
88 |
return f"🚀 Pushed to datasets/{repo_name}"
|
89 |
except Exception as e:
|
90 |
return f"❌ Push failed: {e}"
|
|
|
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,
|
140 |
inputs=[repo_in, token_in],
|
141 |
+
outputs=push_out
|
142 |
)
|
143 |
|
144 |
app.launch()
|