Upload 3 files
Browse files- README.md +88 -3
- app.py +40 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# π§ Named Entity Recognition (NER) with BERT
|
| 3 |
+
|
| 4 |
+
This is a web-based Named Entity Recognition (NER) tool powered by a fine-tuned BERT model, built using PyTorch, Hugging Face Transformers, and Streamlit.
|
| 5 |
+
|
| 6 |
+
## π Demo
|
| 7 |
+
|
| 8 |
+
Try the live app (if deployed):
|
| 9 |
+
π [Your Streamlit Cloud or Hugging Face Space link]
|
| 10 |
+
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
## π Features
|
| 14 |
+
|
| 15 |
+
- π Extracts and highlights entities in raw text (PER, LOC, ORG, etc.)
|
| 16 |
+
- π¬ Clean and interactive Streamlit UI
|
| 17 |
+
- π§ Fine-tuned on the CoNLL-2003 dataset
|
| 18 |
+
- βοΈ Deployable via Streamlit Cloud or Hugging Face Spaces
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## π₯οΈ Run Locally
|
| 23 |
+
|
| 24 |
+
1. Clone this repo:
|
| 25 |
+
```bash
|
| 26 |
+
git clone https://github.com/prasanna-badiger-7/ner-bert-app.git
|
| 27 |
+
cd ner-bert-app
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
2. Install requirements:
|
| 31 |
+
```bash
|
| 32 |
+
pip install -r requirements.txt
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
3. Run the app:
|
| 36 |
+
```bash
|
| 37 |
+
streamlit run app.py
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## π Project Structure
|
| 43 |
+
|
| 44 |
+
```
|
| 45 |
+
ner-bert-app/
|
| 46 |
+
β
|
| 47 |
+
βββ app.py # Streamlit frontend
|
| 48 |
+
βββ ner_model/ # Saved model & tokenizer
|
| 49 |
+
βββ requirements.txt # Dependencies
|
| 50 |
+
βββ README.md
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## π§ Model
|
| 56 |
+
|
| 57 |
+
Fine-tuned `bert-base-cased` on the CoNLL-2003 dataset using Hugging Face Transformers and Datasets.
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## π¦ Dependencies
|
| 62 |
+
|
| 63 |
+
- `streamlit`
|
| 64 |
+
- `transformers`
|
| 65 |
+
- `torch`
|
| 66 |
+
|
| 67 |
+
Install all with:
|
| 68 |
+
```bash
|
| 69 |
+
pip install -r requirements.txt
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## πΈ Example
|
| 75 |
+
|
| 76 |
+

|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## π§βπ» Author
|
| 81 |
+
|
| 82 |
+
Made with β€οΈ by [Prasanna](https://github.com/your-username)
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
## π License
|
| 87 |
+
|
| 88 |
+
MIT License
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification
|
| 2 |
+
import torch
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
model = AutoModelForTokenClassification.from_pretrained('./ner_model')
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained('./ner_model')
|
| 7 |
+
label_list = model.config.id2label
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def predict_entities(text):
|
| 11 |
+
tokens = tokenizer(text, return_tensors='pt', truncation=True, is_split_into_words=False)
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
outputs = model(**tokens)
|
| 14 |
+
|
| 15 |
+
predictions = torch.argmax(outputs.logits, dim=2)
|
| 16 |
+
tokens = tokens['input_ids'][0]
|
| 17 |
+
prediction_ids = predictions[0]
|
| 18 |
+
|
| 19 |
+
result = []
|
| 20 |
+
for token_id, pred_id in zip(tokens, prediction_ids):
|
| 21 |
+
token = tokenizer.decode([token_id])
|
| 22 |
+
label = label_list[pred_id.item()]
|
| 23 |
+
if token not in ['[CLS]', '[SEP]', '[PAD]']:
|
| 24 |
+
result.append((token, label))
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
st.set_page_config(page_title="NER App", layout="wide")
|
| 28 |
+
|
| 29 |
+
st.title("π§ Named Entity Recognition (NER) with BERT")
|
| 30 |
+
text_input = st.text_area("Enter text to analyze:")
|
| 31 |
+
|
| 32 |
+
if st.button("Analyze"):
|
| 33 |
+
if text_input:
|
| 34 |
+
results = predict_entities(text_input)
|
| 35 |
+
st.markdown("### π Extracted Entities:")
|
| 36 |
+
for word, label in results:
|
| 37 |
+
if label != "O":
|
| 38 |
+
st.markdown(f"**{word}** β `{label}`")
|
| 39 |
+
else:
|
| 40 |
+
st.warning("Please enter some text.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
streamlit
|