Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,52 +11,48 @@ model = AutoModelForTokenClassification.from_pretrained(MODEL_ID)
|
|
| 11 |
id2label = {int(k): v for k, v in model.config.id2label.items()}
|
| 12 |
|
| 13 |
|
| 14 |
-
def
|
| 15 |
if not text.strip():
|
| 16 |
-
return "Please enter
|
| 17 |
|
| 18 |
-
# tokenize text
|
| 19 |
inputs = tokenizer(text, return_tensors="pt")
|
| 20 |
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 21 |
|
| 22 |
-
# model forward
|
| 23 |
with torch.no_grad():
|
| 24 |
logits = model(**inputs).logits
|
| 25 |
|
| 26 |
pred_ids = torch.argmax(logits, dim=-1)[0].tolist()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
for tok, pid in zip(tokens, pred_ids):
|
| 32 |
label = id2label[pid]
|
| 33 |
-
|
| 34 |
-
pretty_text += f"{tok:15} β {label}\n"
|
| 35 |
-
|
| 36 |
-
return pretty_text, rows
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
with gr.Blocks(title="Indic NER Token Viewer") as demo:
|
| 41 |
-
gr.Markdown("## π₯ Hindi + English Token-level NER (Fine-tuned Model)")
|
| 42 |
|
| 43 |
-
inp = gr.Textbox(lines=3, label="Enter text to analyze")
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
if __name__ == "__main__":
|
| 61 |
-
demo = build_ui()
|
| 62 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 11 |
id2label = {int(k): v for k, v in model.config.id2label.items()}
|
| 12 |
|
| 13 |
|
| 14 |
+
def generate_ner_output(text):
|
| 15 |
if not text.strip():
|
| 16 |
+
return "Please enter text."
|
| 17 |
|
|
|
|
| 18 |
inputs = tokenizer(text, return_tensors="pt")
|
| 19 |
tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
|
| 20 |
|
|
|
|
| 21 |
with torch.no_grad():
|
| 22 |
logits = model(**inputs).logits
|
| 23 |
|
| 24 |
pred_ids = torch.argmax(logits, dim=-1)[0].tolist()
|
| 25 |
|
| 26 |
+
# Build formatted text
|
| 27 |
+
output_lines = []
|
|
|
|
| 28 |
for tok, pid in zip(tokens, pred_ids):
|
| 29 |
label = id2label[pid]
|
| 30 |
+
output_lines.append(f"{tok:<15} β {label}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
return "\n".join(output_lines)
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
|
| 35 |
+
# ----------- GRADIO UI ---------------
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("## π₯ IndicNER β Token β Label Output")
|
| 38 |
|
| 39 |
+
text_input = gr.Textbox(
|
| 40 |
+
label="Enter text",
|
| 41 |
+
placeholder="Type your Hindi/English sentence here...",
|
| 42 |
+
lines=4
|
| 43 |
+
)
|
|
|
|
| 44 |
|
| 45 |
+
run_button = gr.Button("Generate NER")
|
| 46 |
|
| 47 |
+
ner_output = gr.Textbox(
|
| 48 |
+
label="NER Output (Token β Label Format)",
|
| 49 |
+
lines=30
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
run_button.click(
|
| 53 |
+
fn=generate_ner_output,
|
| 54 |
+
inputs=text_input,
|
| 55 |
+
outputs=ner_output
|
| 56 |
+
)
|
| 57 |
|
| 58 |
+
demo.launch()
|
|
|
|
|
|
|
|
|