Skip to content

Commit

Permalink
pass full object back with id from callback
Browse files Browse the repository at this point in the history
  • Loading branch information
daveminer committed Aug 16, 2024
1 parent ac7fe45 commit 4253f46
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
11 changes: 11 additions & 0 deletions sentiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@
from django.contrib.postgres.fields import ArrayField

class Sentiment(models.Model):
id = models.AutoField(primary_key=True)
label = models.TextField(null=False)
score = models.FloatField(null=False)
tags = ArrayField(models.TextField(), null=False)
text = models.TextField(null=False)
created_at = models.DateTimeField(auto_now=True)

def to_dict(self):
return {
'id': self.id,
'label': self.label,
'score': self.score,
'tags': self.tags,
'text': self.text,
'created_at': self.created_at.isoformat() if self.created_at else None
}
28 changes: 17 additions & 11 deletions sentiment/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .celery import Celery
from .models import Sentiment
from transformers import BertTokenizer, BertForSequenceClassification, pipeline
from typing import List, Tuple
import json
import numpy as np
import requests
Expand All @@ -17,32 +18,37 @@
nlp = pipeline("text-classification", model=finbert, tokenizer=tokenizer)

@celery.task
def run_sentiment(sentences, tags):
results = nlp(sentences)
def run_sentiment(content: List[Tuple[int, str]], tags: List[str]):
results = nlp([item[1] for item in content])

sentiment_objects = []
sentiments = []

for idx, result in enumerate(results):
label = result['label']
score = result['score']
sentiment = Sentiment(
label=label,
text=sentences[idx],
text=content[idx][1],
score=score,
tags=tags
)
sentiment_objects.append(sentiment)
sentiments.append(sentiment)

# Bulk create all sentiment objects in one query
Sentiment.objects.bulk_create(sentiment_objects)
Sentiment.objects.bulk_create(sentiments)

# Return the IDs of the created sentiments
return [sentiment.id for sentiment in sentiment_objects]

return [
{
"article_id": content[idx][0],
"sentiment": sentiment.to_dict()
}
for idx, sentiment in enumerate(sentiments)
]


@celery.task
def send_webhook(sentiment_ids, url):
def send_webhook(sentiments, url):
payload = {
"ids": sentiment_ids
"results": sentiments
}
requests.post(url, json=payload)
7 changes: 4 additions & 3 deletions sentiment/views/createview.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ def post(self, request, *args, **kwargs):
body = parse_request_body(request)

try:
text = body.get('text', [])
content = body.get('content', [])
tags = body.get('tags', [])

callback_url = request.GET.get('callback_url')
content = [(item['id'], item['text']) for item in content]

callback_url = request.GET.get('callback_url')
if callback_url:
chain(
signature("sentiment.tasks.run_sentiment", args=(text,tags,)),
signature("sentiment.tasks.run_sentiment", args=(content,tags,)),
signature("sentiment.tasks.send_webhook", args=(callback_url,), retries=3)
).delay()
else:
Expand Down

0 comments on commit 4253f46

Please sign in to comment.