Explorar el Código

Change nlu app for new model

Mohidul Islam hace 4 años
padre
commit
4f469e0464

+ 9 - 125
analytics/utils.py

@@ -40,122 +40,6 @@ def last_data(date, location_id):
         ratings[r.get('star_rating')-1] = r.get('total')
     return ratings
 
-#
-# def get_review_count_by_month(location_id):
-#     now = timezone.now()
-#     date = now.replace(day=1) - timezone.timedelta(days=1)
-#     day = date.day - now.day
-#     first_day_month = now - timezone.timedelta(days=now.day)
-#     this_month = last_data(first_day_month, location_id)
-#     sql = f'''
-#     SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_review
-#     FROM review_review
-#     WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
-#     GROUP BY MONTH(create_time)
-#     ORDER BY DATE(create_time)
-#     '''
-#     qs = Review.objects.raw(sql)
-#     label = [q.month for q in qs]
-#     total_review = [q.total_review for q in qs]
-#     star_ratings = []
-#     for i in range(1, 6):
-#         sql = f'''
-#             SELECT review_id, MONTHNAME(create_time) as month, COUNT(review_id) as total_ratings
-#             FROM review_review
-#             WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
-#             and location_id={location_id} and star_rating={i}
-#             GROUP BY MONTH(create_time)
-#             ORDER BY DATE(create_time)
-#             '''
-#         qs = Review.objects.raw(sql)
-#         ratings = [0]*len(label)
-#
-#         for q in qs:
-#             ratings[label.index(q.month)] = q.total_ratings
-#         star_ratings.append(ratings)
-#     response = {
-#         'label': label,
-#         'total_reviews': total_review,
-#         'one_star': star_ratings[0],
-#         'two_star': star_ratings[1],
-#         'three_star': star_ratings[2],
-#         'four_star': star_ratings[3],
-#         'five_star': star_ratings[4],
-#         'this': this_month
-#     }
-#     return response
-#
-#
-# def get_review_count_by_week(location_id):
-#     now = timezone.now()
-#     day = 6 - now.weekday()
-#     first_day_week = now - timezone.timedelta(days=(now.weekday()+1))
-#     this_week = last_data(first_day_week, location_id)
-#     sql = f'''
-#     SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_review
-#     FROM review_review
-#     WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time and location_id={location_id}
-#     GROUP BY WEEK(create_time)
-#     ORDER BY DATE(create_time)
-#     '''
-#     qs = Review.objects.raw(sql)
-#     label = [q.week for q in qs]
-#     total_review = [q.total_review for q in qs]
-#     star_ratings = []
-#     for i in range(1, 6):
-#         sql = f'''
-#             SELECT review_id, WEEK(create_time) as week, COUNT(review_id) as total_ratings
-#             FROM review_review
-#             WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),INTERVAL -1 YEAR), INTERVAL {day} DAY) <= create_time
-#             and location_id={location_id} and star_rating={i}
-#             GROUP BY WEEK(create_time)
-#             ORDER BY DATE(create_time)
-#             '''
-#         qs = Review.objects.raw(sql)
-#         ratings = [0]*len(label)
-#         for q in qs:
-#             ratings[label.index(q.week)] = q.total_ratings
-#         star_ratings.append(ratings)
-#     response = {
-#         'label': label,
-#         'total_reviews': total_review,
-#         'one_star': star_ratings[0],
-#         'two_star': star_ratings[1],
-#         'three_star': star_ratings[2],
-#         'four_star': star_ratings[3],
-#         'five_star': star_ratings[4],
-#         'this': this_week
-#     }
-#     return response
-
-
-def monthly_review_count(location_id):
-    sql = f'''
-            SELECT MONTHNAME(create_time) as month, star_rating, COALESCE(COUNT(*), 0) as total_review
-            FROM review_review
-            WHERE DATE_ADD(DATE_ADD(CURRENT_TIMESTAMP(),
-            INTERVAL -1 YEAR), INTERVAL 15 DAY) <= create_time and location_id={location_id}
-            GROUP BY MONTH(create_time), star_rating
-            ORDER BY create_time;
-           '''
-    with connection.cursor() as cursor:
-        cursor.execute(sql)
-        rows = cursor.fetchall()
-        # It will transform a cursor like (('September', 5, 26), ('October', 3, 21), ...)
-        # to a dict like this {'September_5': 26, 'October_3', 21), ...}
-        # Here key 'September_5' means 5 star ratings in September month and value is the number of star count.
-        row_dict = {row[0] + '_' + str(row[1]): row[2] for row in rows}
-        # Get all month names for graph label
-        labels = []
-        for row in rows:
-            if row[0] not in labels:
-                labels.append(row[0])
-        row_lists = []
-        for i in range(1, 6):
-            row = [row_dict.get(m+'_'+str(i), 0) for m in labels]
-            row_lists.append(row)
-    return labels, row_lists
-
 
 def get_review_count_by_week(location_id):
     now = timezone.now()
@@ -173,10 +57,10 @@ def get_review_count_by_week(location_id):
     with connection.cursor() as cursor:
         cursor.execute(sql)
         rows = cursor.fetchall()
-        # It will transform a cursor like (('September', 5, 26), ('October', 3, 21), ...)
-        # to a dict like this {'September_5': 26, 'October_3', 21), ...}
-        # Here key 'September_5' means 5 star ratings in September month and value is the number of star count.
-        row_dict = {str(row[0]) + '_' + str(row[1]): row[2] for row in rows}
+        # It will transform a cursor like ((3, 5, 26), (43, 3, 21), ...)
+        # to a dict like this {(3, 5): 26, (43, 3): 21), ...}
+        # Here key tuple (3, 5) means 5 star ratings in 3rd week of the year and value is the number of star count.
+        row_dict = {(row[0], row[1]): row[2] for row in rows}
         # Get all week names for graph label
         labels = []
         for row in rows:
@@ -184,7 +68,7 @@ def get_review_count_by_week(location_id):
                 labels.append(row[0])
         star_ratings = []
         for i in range(1, 6):
-            row = [row_dict.get(str(w) + '_' + str(i), 0) for w in labels]
+            row = [row_dict.get((w, i), 0) for w in labels]
             star_ratings.append(row)
     total_review = list(np.sum(star_ratings, axis=0))
 
@@ -219,9 +103,9 @@ def get_review_count_by_month(location_id):
         cursor.execute(sql)
         rows = cursor.fetchall()
         # It will transform a cursor like (('September', 5, 26), ('October', 3, 21), ...)
-        # to a dict like this {'September_5': 26, 'October_3', 21), ...}
-        # Here key 'September_5' means 5 star ratings in September month and value is the number of star count.
-        row_dict = {str(row[0]) + '_' + str(row[1]): row[2] for row in rows}
+        # to a dict like this {('September', 5): 26, ('October', 3): 21), ...}
+        # Here key tuple ('September', 5) means 5 star ratings in September month and value is the number of star count.
+        row_dict = {(row[0], row[1]): row[2] for row in rows}
         # Get all month names for graph label
         labels = []
         for row in rows:
@@ -229,7 +113,7 @@ def get_review_count_by_month(location_id):
                 labels.append(row[0])
         star_ratings = []
         for i in range(1, 6):
-            row = [row_dict.get(str(m) + '_' + str(i), 0) for m in labels]
+            row = [row_dict.get((m, i), 0) for m in labels]
             star_ratings.append(row)
     total_review = list(np.sum(star_ratings, axis=0))
 

+ 16 - 1
nlu_job/nlu_utils.py

@@ -1,4 +1,5 @@
 import re
+import operator
 from django.conf import settings
 from requests import post
 import json
@@ -41,7 +42,8 @@ def clean_text(text):
 
 
 def model_inference(text):
-    url = nlu_server_url + '/model/parse'
+    # url = nlu_server_url + '/model/parse'
+    url = nlu_server_url + '/predict'
     text = clean_text(text)
     payload = {'text': text}
     headers = {'content-type': 'application/json'}
@@ -70,6 +72,7 @@ def is_a_name(name):
         return False
 
 
+# This function will not use anymore
 def analyze_inference(response):
     '''
     response has four property
@@ -89,3 +92,15 @@ def analyze_inference(response):
             intents[key] = int(values*100)
 
     return intents
+
+
+def analyze_model_inference(response):
+    res_intents = response.get('response').get('intents')
+    sorted_dict = dict(sorted(res_intents.items(), key=operator.itemgetter(1), reverse=True))
+    intents = {}
+    for key, value in sorted_dict.items():
+        value = float(value)
+        if value > 0.1:
+            intents[key] = int(value*100)
+
+    return intents

+ 2 - 2
nlu_job/views.py

@@ -9,7 +9,7 @@ from django.contrib.auth.decorators import login_required
 
 from .nlu_utils import (
     model_inference,
-    analyze_inference,
+    analyze_model_inference,
     filter_with_last_ten_reviews
 )
 
@@ -22,7 +22,7 @@ def predict_report(request, review_id):
     if review.star_rating == 5:
         text = review.comment.lower()
         res = model_inference(text=text)
-        intents = analyze_inference(res)
+        intents = analyze_model_inference(res)
         for intent in intents.keys():
             r = CustomReply.objects.filter(reply_category=intent)
             filtered_replies = filter_with_last_ten_reviews(location_id, r)

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 3 - 3
review_automation.sql


+ 3 - 2
review_automation/settings/base.py

@@ -31,7 +31,8 @@ PROJECTS_APPS = [
     'analytics.apps.AnalyticsConfig',
     'name_extractor.apps.NameExtractorConfig',
     'yelp.apps.YelpConfig',
-    'facebook_app.apps.FacebookConfig'
+    'facebook_app.apps.FacebookConfig',
+    'user.apps.UserConfig'
 ]
 
 INSTALLED_APPS = DJANGO_DEFAULT_APPS + THIRD_PARTY_APPS + PROJECTS_APPS
@@ -83,7 +84,7 @@ USE_TZ = True
 STATIC_URL = '/static/'
 STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
 
-LOGIN_URL = '/admin/login'
+LOGIN_URL = '/user/login'
 LOGIN_REDIRECT_URL = '/'
 CRISPY_TEMPLATE_PACK = 'bootstrap4'
 

+ 1 - 1
review_automation/settings/dev.py

@@ -23,4 +23,4 @@ DATABASES = {
 # Host URLS
 
 HOST_URI = "http://127.0.0.1:8000"
-NLU_SERVER_URI = 'http://localhost:5005'
+NLU_SERVER_URI = 'http://localhost:1996'

+ 1 - 0
review_automation/urls.py

@@ -14,6 +14,7 @@ urlpatterns = [
     path('analytics/', include('analytics.urls')),
     path('leaderboard/', include('name_extractor.urls')),
     path('facebook/', include('facebook_app.urls')),
+    path('user/', include('user.urls')),
 ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
 
 

+ 0 - 0
user/__init__.py


+ 3 - 0
user/admin.py

@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.

+ 5 - 0
user/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class UserConfig(AppConfig):
+    name = 'user'

+ 11 - 0
user/forms.py

@@ -0,0 +1,11 @@
+from django import forms
+from django.contrib.auth.models import User
+from django.contrib.auth.forms import UserCreationForm
+
+
+class UserRegisterForm(UserCreationForm):
+    email = forms.EmailField()
+
+    class Meta:
+        model = User
+        fields = ['username', 'email', 'password1', 'password2']

+ 26 - 0
user/migrations/0001_initial.py

@@ -0,0 +1,26 @@
+# Generated by Django 3.0.4 on 2020-08-19 07:51
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+        ('gauth', '0013_auto_20200727_0612'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='UserAccount',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('location', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='gauth.Location')),
+                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+            ],
+        ),
+    ]

+ 0 - 0
user/migrations/__init__.py


+ 11 - 0
user/models.py

@@ -0,0 +1,11 @@
+from django.db import models
+from django.contrib.auth.models import User
+from gauth.models import Location
+
+
+class UserAccount(models.Model):
+    user = models.OneToOneField(User, on_delete=models.CASCADE)
+    location = models.OneToOneField(Location, on_delete=models.CASCADE)
+
+    def __str__(self):
+        return f'<UserAccount> -- {self.user}'

+ 21 - 0
user/templates/login.html

@@ -0,0 +1,21 @@
+{% extends "base.html" %}
+{% load crispy_forms_tags %}
+{% block content %}
+    <div class="content-section">
+        <form method="POST">
+            {% csrf_token %}
+            <fieldset class="form-group">
+                <legend class="border-bottom mb-4">Log In</legend>
+                {{ form|crispy }}
+            </fieldset>
+            <div class="form-group">
+                <button class="btn btn-outline-info" type="submit">Login</button>
+            </div>
+        </form>
+        <div class="border-top pt-3">
+            <small class="text-muted">
+                Need An Account? <a class="ml-2" href="{% url 'registration' %}">Sign Up Now</a>
+            </small>
+        </div>
+    </div>
+{% endblock content %}

+ 9 - 0
user/templates/logout.html

@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+{% block content %}
+    <h2>You have been logged out</h2>
+    <div class="border-top pt-3">
+        <small class="text-muted">
+            <a href="{% url 'login' %}">Log In Again</a>
+        </small>
+    </div>
+{% endblock content %}

+ 21 - 0
user/templates/signup.html

@@ -0,0 +1,21 @@
+{% extends "base.html" %}
+{% load crispy_forms_tags %}
+{% block content %}
+    <div class="content-section">
+        <form method="POST">
+            {% csrf_token %}
+            <fieldset class="form-group">
+                <legend class="border-bottom mb-4">Join Today</legend>
+                {{ form|crispy }}
+            </fieldset>
+            <div class="form-group">
+                <button class="btn btn-outline-info" type="submit">Sign Up</button>
+            </div>
+        </form>
+        <div class="border-top pt-3">
+            <small class="text-muted">
+                Already Have An Account? <a class="ml-2" href="{% url 'login' %}">Sign In</a>
+            </small>
+        </div>
+    </div>
+{% endblock content %}

+ 3 - 0
user/tests.py

@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.

+ 9 - 0
user/urls.py

@@ -0,0 +1,9 @@
+from django.urls import path
+from django.contrib.auth import views as auth_views
+from .views import RegistrationView
+
+urlpatterns = [
+    path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
+    path('logout/', auth_views.LogoutView.as_view(template_name='logout.html'), name='logout'),
+    path('signup', RegistrationView.as_view(), name='registration'),
+]

+ 23 - 0
user/views.py

@@ -0,0 +1,23 @@
+from django.shortcuts import render, redirect
+from django.views.generic import View
+from django.contrib import messages
+from .forms import UserRegisterForm
+
+
+class RegistrationView(View):
+
+    def get(self, request, *args, **kwargs):
+        form = UserRegisterForm()
+        return render(request, 'signup.html', {'form': form})
+
+    def post(self, request, *args, **kwargs):
+        form = UserRegisterForm(request.POST)
+        if form.is_valid():
+            email = form.cleaned_data.get('email')
+            username = form.cleaned_data.get('username')
+            print(email, username)
+
+            messages.success(request, f'Your account has been created! You are now able to log in')
+            return redirect('login')
+
+        return render(request, 'signup.html', {'form': form})

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio