Skip to content

Commit f472ea8

Browse files
authored
Merge pull request #42 from JustInternetAI/Chloe
Chloe
2 parents 75eb78c + 1362725 commit f472ea8

6 files changed

Lines changed: 127 additions & 69 deletions

File tree

.docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN apt-get update && \
1313
# Install requirements
1414
COPY requirements.txt /workspace/requirements.txt
1515
RUN pip install --no-cache-dir -r /workspace/requirements.txt
16-
RUN pip install torch==2.1.2 torchvision --index-url https://download.pytorch.org/whl/cpu
16+
RUN pip install torch==2.4.0 torchvision --index-url https://download.pytorch.org/whl/cpu
1717

1818

1919
RUN playwright install

src/justinsight/celery.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
# "args": (),
2424
# },
2525

26-
"check-BBCfeed-every-5-minutes": {
27-
"task": "justinsight.tasks.bbcLogger_task",
28-
"schedule": 5.0,
29-
"args": (),
30-
},
26+
# "check-BBCfeed-every-5-minutes": {
27+
# "task": "justinsight.tasks.bbcLogger_task",
28+
# "schedule": 5.0,
29+
# "args": (),
30+
# },
3131

3232
# "check-CBSfeed-every-5-minutes": {
3333
# "task": "justinsight.tasks.cbsLogger_task",

src/justinsight/tasks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ingest.usnews_ingestor import USNEWSIngestor
1111
from .nlpthings import dummy_addToEntryInDB
1212
from ingest.save_to_database import collection
13-
from nlp.core import process_article
13+
from nlp.ner_core import NERCore
1414
from bson import ObjectId
1515

1616
@shared_task
@@ -90,8 +90,10 @@ def runNER_task(entry_id):
9090
@shared_task
9191
def ner_task(article_id):
9292
# Process article with NER results
93+
print("In the NER task")
94+
core = NERCore()
9395
print("Actually trying to do NER!!!")
94-
process_article(article_id)
96+
core.process_article(article_id)
9597

9698

9799

src/nlp/base_core.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from pymongo import MongoClient
2+
from bson import ObjectId
3+
from transformers import pipeline
4+
5+
class BaseCore:
6+
# Include username, password, and authentication database
7+
client = MongoClient("mongodb://myuser:mypassword@mongo:27017/justinsightdb?authSource=admin")
8+
db = client["justinsightdb"]
9+
collection = db["articles"]
10+
11+
model = None #Set in subclass
12+
13+
def __init__(self, task: str, model_name: str, aggregation_strategy: str = None):
14+
self.task = task
15+
self.model_name = model_name
16+
self.aggregation_strategy = aggregation_strategy
17+
self.pipeline = self.load_pipeline()
18+
19+
def load_pipeline(self):
20+
if self.task == "ner" and self.aggregation_strategy:
21+
return pipeline(self.task, model=self.model_name, aggregation_strategy=self.aggregation_strategy)
22+
23+
print(f"pipeline loading...")
24+
return pipeline(self.task, model=self.model_name)
25+
26+
def process_article(self, article_id: str):
27+
#Retrieve article by ID
28+
article = self.collection.find_one({"_id": ObjectId(article_id)})
29+
30+
if not article:
31+
print(f"No article found with ID: {article_id}")
32+
return []
33+
34+
if article.get("processed") is True:
35+
print(f"Article {article_id} already processed.")
36+
return
37+
38+
# We have a check running so only articles with full text are saved
39+
full_text = article.get("full_text", "")
40+
# if not full_text:
41+
# print(f"Article {article_id} has no full text.")
42+
# return
43+
44+
entities = self.pipeline(full_text)
45+
46+
self.collection.update_one(
47+
{"_id": ObjectId(article_id)},
48+
{"$set": {
49+
"processed": True,
50+
"entities": entities
51+
}}
52+
)
53+
return entities

src/nlp/core.py

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/nlp/ner_core.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from nlp.base_core import BaseCore
2+
from transformers import pipeline
3+
from bson import ObjectId
4+
5+
class NERCore(BaseCore):
6+
def __init__(self):
7+
print("constructing NER Core instance")
8+
super().__init__(
9+
task="ner",
10+
model_name="dslim/bert-base-NER",
11+
aggregation_strategy="simple"
12+
)
13+
14+
def process_article(self, article_id: str):
15+
#Retrieve article by ID
16+
article = self.collection.find_one({"_id": ObjectId(article_id)})
17+
18+
if not article:
19+
print(f"No article found with ID: {article_id}")
20+
return []
21+
22+
if article.get("processed") is True:
23+
print(f"Article {article_id} already processed.")
24+
return []
25+
26+
# We have a check running so only articles with full text are saved
27+
full_text = article.get("full_text", "")
28+
# if not full_text:
29+
# print(f"Article {article_id} has no full text.")
30+
# return
31+
32+
# Run NER
33+
entities = self.pipeline(full_text) # run_ner_hf(full_text)
34+
35+
# Update article in DB
36+
self.addToEntryInDB(article_id, {
37+
"ner": entities,
38+
"processed": True
39+
})
40+
41+
return entities
42+
43+
def format_ner_tags(self, ner_list):
44+
formatted = []
45+
for ent in ner_list:
46+
label = ent.get("label") or ent.get("entity") or ent.get("entity_group", "UNKNOWN")
47+
text = ent.get("text") or ent.get("word") or ""
48+
formatted.append(f"{label}: {text}")
49+
return ", ".join(formatted)
50+
51+
def addToEntryInDB(self, entry_id, updates):
52+
print("Adding NER results to database\r\r\r")
53+
54+
if "ner" in updates:
55+
for ent in updates["ner"]:
56+
ent["score"] = float(ent["score"]) # convert np.float32 to Python float
57+
58+
updates["ner_pretty"] = self.format_ner_tags(updates["ner"]) # so we can actually read the NER
59+
60+
id = ObjectId(entry_id)
61+
self.collection.update_one(
62+
{"_id": id},
63+
{"$set": updates}
64+
)

0 commit comments

Comments
 (0)