Skip to content

Instantly share code, notes, and snippets.

View aballah-chamakh's full-sized avatar

abdallah-chamakh aballah-chamakh

View GitHub Profile
# ....
# 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'
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)
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
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 :
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'
# ...
# installed app
INSTALLED_APPS = [
# ...
'rest_framework',
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'),
from django.urls import path
from .views import (SentimentAnalysisInferenceListApiView,
SentimentAnalysisInferenceCreateApiView,
SentimentAnalysisInferenceUpdateApiView
SentimentAnalysisInferenceDetailApiView,
SentimentAnalysisInferenceDeleteApiView,
)
urlpatterns = [
path('sa_inferences', SentimentAnalysisInferenceListApiView.as_view()),
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
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