Aman Kumar Singh commited on
Commit ·
4f5d9cc
1
Parent(s): a17285a
SpacyBison
Browse files- spacybison/config.cfg +16 -0
- spacybison/llm_ner.py +10 -0
- spacybison/occurences.py +8 -0
- spacybison/run_llm.py +27 -0
spacybison/config.cfg
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[nlp]
|
| 2 |
+
lang = "en"
|
| 3 |
+
pipeline = ["llm"]
|
| 4 |
+
|
| 5 |
+
[components]
|
| 6 |
+
|
| 7 |
+
[components.llm]
|
| 8 |
+
factory = "llm"
|
| 9 |
+
|
| 10 |
+
[components.llm.task]
|
| 11 |
+
@llm_tasks = "spacy.NER.v2"
|
| 12 |
+
labels = PERSON , FULL_ADDRESS , PHONE_NO , ORGANISATION , FIR_NO , CRIME_NO , DISTRICT , STATE , COUNTRY , AADHAAR , VEHICLE_NO
|
| 13 |
+
|
| 14 |
+
[components.llm.model]
|
| 15 |
+
@llm_models = "spacy.PaLM.v1"
|
| 16 |
+
name = "text-bison-001"
|
spacybison/llm_ner.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
from spacy_llm.util import assemble
|
| 4 |
+
|
| 5 |
+
def llm_ner(input_text):
|
| 6 |
+
nlp = assemble("spacybison/config.cfg")
|
| 7 |
+
doc = nlp(input_text)
|
| 8 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 9 |
+
|
| 10 |
+
return entities
|
spacybison/occurences.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def find_all_occurrences(text, substring):
|
| 2 |
+
start = 0
|
| 3 |
+
while start < len(text):
|
| 4 |
+
start = text.find(substring, start)
|
| 5 |
+
if start == -1:
|
| 6 |
+
break
|
| 7 |
+
yield start, start + len(substring)
|
| 8 |
+
start += len(substring)
|
spacybison/run_llm.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from spacybison.occurences import find_all_occurrences
|
| 2 |
+
from spacybison.llm_ner import llm_ner
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
def run_llm(input_text):
|
| 6 |
+
extracted_list = llm_ner(input_text)
|
| 7 |
+
|
| 8 |
+
output = []
|
| 9 |
+
seen_occurrences = set()
|
| 10 |
+
|
| 11 |
+
for entity, entity_type in extracted_list:
|
| 12 |
+
occurrences = list(find_all_occurrences(input_text, entity))
|
| 13 |
+
for start, end in occurrences:
|
| 14 |
+
if (start, end) not in seen_occurrences:
|
| 15 |
+
seen_occurrences.add((start, end))
|
| 16 |
+
output.append({
|
| 17 |
+
"entity_type": entity_type,
|
| 18 |
+
"start": start,
|
| 19 |
+
"end": end,
|
| 20 |
+
"score": 1,
|
| 21 |
+
"analysis_explanation": None,
|
| 22 |
+
"recognition_metadata": {
|
| 23 |
+
"recognizer_identifier": "spacy-llm",
|
| 24 |
+
"recognizer_name": "chat-bison-001"
|
| 25 |
+
}
|
| 26 |
+
})
|
| 27 |
+
return output
|