This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .... | |
# configure celery and redis | |
BROKER_URL = 'redis://localhost:6379' | |
CELERY_RESULT_BACKEND = 'redis://' | |
CELERY_ACCEPT_CONTENT = ['application/json'] | |
CELERY_RESULT_SERIALIZER = 'json' | |
CELERY_TASK_SERIALIZER = 'json' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from celery import Celery | |
from django.conf import settings | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MyApp.settings') | |
app = Celery('MyApp') | |
app.config_from_object('django.conf:settings') | |
app.autodiscover_tasks(settings.INSTALLED_APPS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from .models import SentimentAnalysisInference | |
import nltk.sentiment.vader import SentimentIntensityAnalyzer | |
from .tasks import run_inference | |
class SentimentAnalysisInferenceSerializer(serializers.ModelSerializer): | |
username = serializers.CharField(source='user.username',read_only=True) | |
class Meta : | |
model = SentimentAnalysisInference |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from MyApp.celery import app | |
from .models import SentimentAnalysisInference | |
import nltk.sentiment.vader import SentimentIntensityAnalyzer | |
@app.task(bind=True) | |
def run_inference(self, sa_id): | |
sai_inference_obj = SentimentAnalysisInference.objects.get(id=sa_id) | |
sid = SentimentIntensityAnalyzer() | |
compound = sid.polarity_scores(sai_inference.text)['compountd'] | |
if compound > 0.5 : |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
# endpoints | |
base_end_point = 'http://127.0.0.1/api/' | |
token_end_point = base_end_point+'token' | |
sa_endpoint = base_end_point+'/make_sa_inference' | |
# credentials | |
username = 'ElonMusk' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
# installed app | |
INSTALLED_APPS = [ | |
# ... | |
'rest_framework', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib import admin | |
from django.urls import path,include | |
from rest_framework_jwt.views import obtain_jwt_token,refresh_jwt_token | |
urlpatterns = [ | |
path('admin/', admin.site.urls), | |
path('api/',include('SAInference.urls')), | |
path('api/token/', obtain_jwt_token, name='token_obtain_pair'), | |
path('api/token/refresh', refresh_jwt_token, name='token_refresh'), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import path | |
from .views import (SentimentAnalysisInferenceListApiView, | |
SentimentAnalysisInferenceCreateApiView, | |
SentimentAnalysisInferenceUpdateApiView | |
SentimentAnalysisInferenceDetailApiView, | |
SentimentAnalysisInferenceDeleteApiView, | |
) | |
urlpatterns = [ | |
path('sa_inferences', SentimentAnalysisInferenceListApiView.as_view()), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import generics | |
from .serializers import SentimentAnalysisInferenceSerializer | |
from .models import SentimentAnalysisInference | |
from .permissions import IsAuthenticatedAndOwnerOrDeny | |
class SentimentAnalysisInferenceListApiView(generics.ListAPIView): | |
queryset = SentimentAnalysisInference.objects.all() | |
serializer_class = SentimentAnalysisInferenceSerializer | |
permission_class = IsAuthenticatedAndOwnerOrDeny | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import permissions | |
class IsAuthenticatedAndOwnerOrDeny(permissions.BasePermission): | |
def has_permission(self, request, view): | |
# check if the user is authenticated | |
return self.request.user.is_authenticated() | |
def has_object_permission(self, request, view, obj): | |
# check if the user is the owner of the inference object |
NewerOlder