Update app.py
Browse files
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
|
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),
|
21 |
-
gr.update(visible=False),
|
|
|
|
|
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),
|
28 |
-
gr.update(visible=True),
|
|
|
|
|
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 |
-
|
67 |
-
fig.savefig(
|
68 |
-
return fig,
|
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 |
-
#
|
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 |
-
visualize_btn = gr.Button("📊 Visualize")
|
111 |
|
112 |
-
|
|
|
|
|
113 |
|
114 |
-
# Push
|
115 |
-
push_acc
|
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 |
-
#
|
123 |
upload_btn.click(
|
124 |
upload_csv,
|
125 |
-
inputs=
|
126 |
-
outputs=[table, status,
|
127 |
)
|
128 |
save_btn.click(
|
129 |
save_changes,
|
130 |
inputs=table,
|
131 |
outputs=status
|
132 |
)
|
|
|
|
|
|
|
|
|
133 |
visualize_btn.click(
|
134 |
visualize_and_download,
|
135 |
-
|
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,
|