Upload 2 files
Browse files- app.py +59 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import BertTokenizerFast, BertForTokenClassification
|
4 |
+
|
5 |
+
# Load Model and Tokenizer
|
6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
+
model_name = "AventIQ-AI/bert-named-entity-recognition"
|
8 |
+
model = BertForTokenClassification.from_pretrained(model_name).to(device)
|
9 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Label List
|
12 |
+
label_list = ["O", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC", "B-MISC", "I-MISC"]
|
13 |
+
|
14 |
+
def predict_entities(text):
|
15 |
+
tokens = tokenizer(text, return_tensors="pt", truncation=True)
|
16 |
+
tokens = {key: val.to(device) for key, val in tokens.items()} # Move to CUDA
|
17 |
+
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model(**tokens)
|
20 |
+
|
21 |
+
logits = outputs.logits # Extract logits
|
22 |
+
predictions = torch.argmax(logits, dim=2) # Get highest probability labels
|
23 |
+
|
24 |
+
tokens_list = tokenizer.convert_ids_to_tokens(tokens["input_ids"][0])
|
25 |
+
predicted_labels = [label_list[pred] for pred in predictions[0].cpu().numpy()]
|
26 |
+
|
27 |
+
final_tokens = []
|
28 |
+
final_labels = []
|
29 |
+
for token, label in zip(tokens_list, predicted_labels):
|
30 |
+
if token.startswith("##"):
|
31 |
+
final_tokens[-1] += token[2:] # Merge subword
|
32 |
+
else:
|
33 |
+
final_tokens.append(token)
|
34 |
+
final_labels.append(label)
|
35 |
+
|
36 |
+
table_rows = []
|
37 |
+
highlighted_text = text
|
38 |
+
for token, label in zip(final_tokens, final_labels):
|
39 |
+
if token not in ["[CLS]", "[SEP]", "O"]:
|
40 |
+
table_rows.append(f"<tr><td>{token}</td><td>{label}</td></tr>")
|
41 |
+
highlighted_text = highlighted_text.replace(token, f"<mark>{token}</mark>", 1)
|
42 |
+
|
43 |
+
table_data = "<table border='1' style='width:100%;'><tr><th>Entity</th><th>Label</th></tr>" + "".join(table_rows) + "</table>"
|
44 |
+
|
45 |
+
return f"<div style='font-size:16px; padding:10px;'><b>Highlighted Text:</b><br>{highlighted_text}<br><br><b>Entities Table:</b><br>{table_data}</div>"
|
46 |
+
|
47 |
+
# Create Gradio Interface
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=predict_entities,
|
50 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text for entity recognition..."),
|
51 |
+
outputs=gr.HTML(),
|
52 |
+
title="BERT Named Entity Recognition",
|
53 |
+
description="Identify named entities (e.g., names, locations, organizations) in text using the BERT model fine-tuned by AventIQ. The results are displayed with highlighted entities and a structured table.",
|
54 |
+
live=True
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch the app
|
58 |
+
if __name__ == "__main__":
|
59 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
sentencepiece
|
4 |
+
gradio
|