Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
"token-classification",
|
| 6 |
-
model="techysanoj/fine-tuned-IndicNER",
|
| 7 |
-
aggregation_strategy="simple"
|
| 8 |
-
)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 4 |
|
| 5 |
+
MODEL_ID = "techysanoj/fine-tuned-IndicNER"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForTokenClassification.from_pretrained(MODEL_ID)
|
| 9 |
|
| 10 |
+
id2label = {int(k): v for k, v in model.config.id2label.items()}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def ner_predict(text):
|
| 14 |
+
# tokenize input
|
| 15 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 16 |
+
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 17 |
+
|
| 18 |
+
# run model
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
logits = model(**inputs).logits
|
| 21 |
+
|
| 22 |
+
pred_ids = torch.argmax(logits, dim=-1)[0].tolist()
|
| 23 |
+
|
| 24 |
+
# build output table
|
| 25 |
+
rows = []
|
| 26 |
+
for tok, pid in zip(tokens, pred_ids):
|
| 27 |
+
rows.append([tok, id2label[pid]])
|
| 28 |
+
|
| 29 |
+
# pretty text version
|
| 30 |
+
pretty_output = ""
|
| 31 |
+
for tok, lab in rows:
|
| 32 |
+
pretty_output += f"{tok:15} → {lab}\n"
|
| 33 |
+
|
| 34 |
+
return pretty_output, rows
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# gradio UI
|
| 38 |
+
with gr.Blocks(title="Indic NER Token-wise Output") as demo:
|
| 39 |
+
gr.Markdown("🔥 Indian Language NER — Token Level Output (Hindi + English)")
|
| 40 |
+
|
| 41 |
+
inp = gr.Textbox(lines=3, label="Enter text")
|
| 42 |
+
|
| 43 |
+
btn = gr.Button("Run NER")
|
| 44 |
+
|
| 45 |
+
out_text = gr.Textbox(label="Tokenized Output")
|
| 46 |
+
out_table = gr.Dataframe(
|
| 47 |
+
headers=["Token", "Label"],
|
| 48 |
+
datatype=["str", "str"],
|
| 49 |
+
label="Table View",
|
| 50 |
+
wrap=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
btn.click(fn=ner_predict, inputs=inp, outputs=[out_text, out_table])
|
| 54 |
+
|
| 55 |
+
demo.launch()
|