Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,75 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
|
|
3 |
|
4 |
-
#
|
5 |
token_df = pd.DataFrame()
|
6 |
|
7 |
def make_sample_data(n=100):
|
8 |
people = ["Alice","Bob","Charlie","Diane","Eve"]
|
9 |
orgs = ["Acme","Globex","Initech","Umbrella","Stark"]
|
10 |
locs = ["Paris","NYC","London","Tokyo","Sydney"]
|
11 |
-
|
12 |
-
rows = []
|
13 |
-
for i in range(n):
|
14 |
-
p = people[i % len(people)]
|
15 |
-
v = verbs[i % len(verbs)]
|
16 |
-
o = orgs[i % len(orgs)]
|
17 |
-
l = locs[i % len(locs)]
|
18 |
-
rows.append({"text": f"{p} {v} {o} in {l}."})
|
19 |
return pd.DataFrame(rows)
|
20 |
|
|
|
21 |
def load_data(file):
|
22 |
global token_df
|
23 |
-
|
24 |
-
if file:
|
25 |
-
df = pd.read_csv(file.name)
|
26 |
-
else:
|
27 |
-
df = make_sample_data(100)
|
28 |
if "text" not in df.columns:
|
29 |
-
return (
|
30 |
-
|
31 |
-
|
32 |
-
gr.update(visible=False)
|
33 |
-
)
|
34 |
-
# Tokenize
|
35 |
-
records = []
|
36 |
-
for sid, txt in enumerate(df["text"]):
|
37 |
for tok in txt.split():
|
38 |
-
records.append({"sentence_id":
|
39 |
-
token_df
|
40 |
-
return (
|
41 |
-
gr.update(value=token_df, visible=True),
|
42 |
-
f"β
Loaded {len(df)} sentences β {len(token_df)} tokens.",
|
43 |
-
gr.update(visible=True),
|
44 |
-
)
|
45 |
|
46 |
-
def save_edits(
|
47 |
global token_df
|
48 |
-
token_df
|
49 |
-
return "πΎ
|
50 |
|
51 |
-
def
|
52 |
-
|
53 |
-
return "raw_tokens.csv"
|
54 |
|
55 |
-
def
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
if
|
61 |
-
|
62 |
-
|
63 |
-
else:
|
64 |
-
tag = ("I-" if prev.get(sid)==lbl else "B-") + lbl
|
65 |
-
iob.append(tag)
|
66 |
-
prev[sid] = lbl
|
67 |
-
out = token_df.copy()
|
68 |
-
out["iob"] = iob
|
69 |
-
out.to_csv("ner_iob.csv", index=False)
|
70 |
-
return "ner_iob.csv"
|
71 |
|
72 |
-
|
|
|
73 |
gr.Markdown("# π·οΈ Label It! Mini-NER")
|
74 |
-
gr.Markdown("
|
75 |
|
76 |
with gr.Row():
|
77 |
-
file_in
|
78 |
-
load_btn = gr.Button("Load
|
79 |
|
80 |
status = gr.Textbox(label="Status", interactive=False)
|
81 |
-
table = gr.Dataframe(
|
82 |
-
headers=["sentence_id","token","label"],
|
83 |
-
interactive=True,
|
84 |
-
visible=False,
|
85 |
-
label="π Annotate Tokens"
|
86 |
-
)
|
87 |
|
88 |
-
#
|
89 |
with gr.Row(visible=False) as actions:
|
90 |
-
save_btn
|
91 |
-
|
92 |
-
|
93 |
|
94 |
-
|
|
|
|
|
|
|
|
|
95 |
save_btn.click(save_edits, inputs=table, outputs=status)
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
β’ Download your **Tokens CSV** or **IOB CSV** with the buttons above.
|
102 |
-
""")
|
103 |
|
104 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
from pathlib import Path
|
4 |
|
5 |
+
# Global store
|
6 |
token_df = pd.DataFrame()
|
7 |
|
8 |
def make_sample_data(n=100):
|
9 |
people = ["Alice","Bob","Charlie","Diane","Eve"]
|
10 |
orgs = ["Acme","Globex","Initech","Umbrella","Stark"]
|
11 |
locs = ["Paris","NYC","London","Tokyo","Sydney"]
|
12 |
+
rows = [{"text": f"{people[i%5]} visited {orgs[i%5]} in {locs[i%5]}."} for i in range(n)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
return pd.DataFrame(rows)
|
14 |
|
15 |
+
# ββββββββββββββββββββββββββ I/O helpers ββββββββββββββββββββββββββ
|
16 |
def load_data(file):
|
17 |
global token_df
|
18 |
+
df = pd.read_csv(file.name) if file else make_sample_data()
|
|
|
|
|
|
|
|
|
19 |
if "text" not in df.columns:
|
20 |
+
return None,"β Need a `text` column",gr.update(visible=False)
|
21 |
+
records=[]
|
22 |
+
for sid,txt in enumerate(df["text"]):
|
|
|
|
|
|
|
|
|
|
|
23 |
for tok in txt.split():
|
24 |
+
records.append({"sentence_id":sid,"token":tok,"label":"O"})
|
25 |
+
token_df=pd.DataFrame(records)
|
26 |
+
return token_df,"β
Loaded & tokenized",gr.update(visible=True)
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def save_edits(tbl): # keep edits in memory
|
29 |
global token_df
|
30 |
+
token_df=pd.DataFrame(tbl,columns=["sentence_id","token","label"])
|
31 |
+
return "πΎ Saved"
|
32 |
|
33 |
+
def get_tokens_csv():
|
34 |
+
path="raw_tokens.csv"; token_df.to_csv(path,index=False); return Path(path)
|
|
|
35 |
|
36 |
+
def get_iob_csv():
|
37 |
+
iob,prev=[],{}
|
38 |
+
for _,r in token_df.iterrows():
|
39 |
+
sid,l=r["sentence_id"],r["label"]
|
40 |
+
if l=="O": iob.append("O"); prev[sid]=None
|
41 |
+
else: iob.append(("I-" if prev.get(sid)==l else "B-")+l); prev[sid]=l
|
42 |
+
out=token_df.copy(); out["iob"]=iob
|
43 |
+
path="ner_iob.csv"; out.to_csv(path,index=False); return Path(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# ββββββββββββββββββββββββββ UI ββββββββββββββββββββββββββ
|
46 |
+
with gr.Blocks() as demo:
|
47 |
gr.Markdown("# π·οΈ Label It! Mini-NER")
|
48 |
+
gr.Markdown("Step 1 β Upload a CSV with a `text` column (or leave blank for sample).")
|
49 |
|
50 |
with gr.Row():
|
51 |
+
file_in = gr.File(label="π Upload CSV", file_types=[".csv"])
|
52 |
+
load_btn = gr.Button("Load")
|
53 |
|
54 |
status = gr.Textbox(label="Status", interactive=False)
|
55 |
+
table = gr.Dataframe(headers=["sentence_id","token","label"], interactive=True, visible=False)
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# action row
|
58 |
with gr.Row(visible=False) as actions:
|
59 |
+
save_btn = gr.Button("πΎ Save Edits")
|
60 |
+
dl_tok_btn = gr.Button("β¬οΈ Download Tokens CSV")
|
61 |
+
dl_iob_btn = gr.Button("β¬οΈ Download IOB CSV")
|
62 |
|
63 |
+
hidden_tok = gr.File(visible=False)
|
64 |
+
hidden_iob = gr.File(visible=False)
|
65 |
+
|
66 |
+
# Bindings
|
67 |
+
load_btn.click(load_data, inputs=file_in, outputs=[table,status,actions])
|
68 |
save_btn.click(save_edits, inputs=table, outputs=status)
|
69 |
|
70 |
+
dl_tok_btn.click(lambda: get_tokens_csv(), outputs=hidden_tok)
|
71 |
+
dl_iob_btn.click(lambda: get_iob_csv(), outputs=hidden_iob)
|
72 |
+
|
73 |
+
gr.Markdown("Step 2 β Edit **label** cells (`PER`,`ORG`,`LOC`, or `O`), then Save/Download.")
|
|
|
|
|
74 |
|
75 |
+
demo.launch()
|