Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review Task Views #16

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions parlance/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
from django.contrib import admin
from django.urls import path, include

from parley.views import ReviewTaskDetail
from parley.views import LLMList, LLMDetail
from parley.views import UploaderFormView, CreateReviewTask, ResponseDetail
from parlance.views import Dashboard, AccountSettings, AccountProfile
from parley.views import UploaderFormView, CreateReviewTask, ResponseDetail
from parley.views import EvaluationList, EvaluationDetail, DownloadPrompts


Expand All @@ -47,17 +48,21 @@
# Application Pages
path("", Dashboard.as_view(), name="dashboard"),
path("upload/", UploaderFormView.as_view(), name="upload"),

path("account/profile", AccountProfile.as_view(), name="account-profile"),
path("account/settings", AccountSettings.as_view(), name="account-settings"),

path("evaluations/", EvaluationList.as_view(), name="evaluations-list"),
path("evaluations/<uuid:pk>", EvaluationDetail.as_view(), name="evaluation-detail"),
path("evaluations/<uuid:pk>/download", DownloadPrompts.as_view(), name="evaluation-download"),
path("evaluations/create-review-task", CreateReviewTask.as_view(), name="create-review-task"),

path("reviews/<int:pk>", ReviewTaskDetail.as_view(), name="review-task"),

path("models/", LLMList.as_view(), name="llms-list"),
path("models/<uuid:pk>", LLMDetail.as_view(), name="llm-detail"),
path("responses/<uuid:pk>", ResponseDetail.as_view(), name="response-detail"),

path("evaluations/create-review-task", CreateReviewTask.as_view(), name="create-review-task"),

# Admin URLs
path("admin/", admin.site.urls),

Expand Down
6 changes: 4 additions & 2 deletions parlance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from django.shortcuts import render
from django.views.generic import TemplateView
from parley.models import LLM, Evaluation, Prompt, Response
from parley.models import LLM, Evaluation, Prompt, Response, ReviewTask


##########################################################################
Expand All @@ -41,7 +41,9 @@ def get_context_data(self, **kwargs):
context["n_responses"] = Response.objects.count()

# Get evaluations and models
context["evaluations"] = Evaluation.objects.filter(active=True)
context["evaluations"] = ReviewTask.objects.filter(
completed_on=None, user=self.request.user
)
context["llms"] = LLM.objects.all()[:20]

return context
Expand Down
8 changes: 6 additions & 2 deletions parley/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ class CreateReviewForm(forms.Form):
def save(self):
try:
ReviewTask.objects.create(
user=User.objects.get(pk=self.cleaned_data["user"]),
evaluation=ModelEvaluation.objects.get(pk=self.cleaned_data["evaluation"]),
user=User.objects.get(
pk=self.cleaned_data["user"]
),
model_evaluation=ModelEvaluation.objects.get(
pk=self.cleaned_data["evaluation"]
),
)
except (User.DoesNotExist, ModelEvaluation.DoesNotExist):
return
10 changes: 6 additions & 4 deletions parley/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.1.1 on 2024-10-07 01:00
# Generated by Django 5.1.1 on 2024-10-07 03:15

import django.db.models.deletion
import parley.models.llm
Expand Down Expand Up @@ -704,7 +704,7 @@ class Migration(migrations.Migration):
"verbose_name": "response",
"verbose_name_plural": "responses",
"db_table": "responses",
"ordering": ("-created",),
"ordering": ("created",),
"get_latest_by": "created",
},
),
Expand Down Expand Up @@ -814,6 +814,7 @@ class Migration(migrations.Migration):
(
"started_on",
models.DateTimeField(
blank=True,
default=None,
help_text="The timestamp that the review was start on, null if not started",
null=True,
Expand All @@ -822,13 +823,14 @@ class Migration(migrations.Migration):
(
"completed_on",
models.DateTimeField(
blank=True,
default=None,
help_text="The timestamp that the review was completed, null if not completed",
null=True,
),
),
(
"evaluation",
"model_evaluation",
models.ForeignKey(
help_text="The evaluation the user is performing",
on_delete=django.db.models.deletion.CASCADE,
Expand Down Expand Up @@ -856,7 +858,7 @@ class Migration(migrations.Migration):
"db_table": "review_tasks",
"ordering": ("-created",),
"get_latest_by": "created",
"unique_together": {("user", "evaluation")},
"unique_together": {("user", "model_evaluation")},
},
),
migrations.AddField(
Expand Down
5 changes: 4 additions & 1 deletion parley/models/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ class Meta:
def image(self):
return self.model.image

def prompts(self):
return self.evaluation.prompts.filter(exclude=False)

def responses(self):
return Response.objects.filter(
model=self.model, prompt__evaluation=self.evaluation
Expand Down Expand Up @@ -423,7 +426,7 @@ class Response(BaseModel):

class Meta:
db_table = "responses"
ordering = ("-created",)
ordering = ("created",)
get_latest_by = "created"
verbose_name = "response"
verbose_name_plural = "responses"
Expand Down
36 changes: 31 additions & 5 deletions parley/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .base import TimestampedModel

from django.db import models
from django.urls import reverse


##########################################################################
Expand All @@ -36,7 +37,7 @@ class ReviewTask(TimestampedModel):
help_text="The user that is conducting the evaluation",
)

evaluation = models.ForeignKey(
model_evaluation = models.ForeignKey(
"parley.ModelEvaluation",
null=False,
on_delete=models.CASCADE,
Expand All @@ -49,20 +50,31 @@ class ReviewTask(TimestampedModel):
)

started_on = models.DateTimeField(
null=True, default=None,
null=True, default=None, blank=True,
help_text="The timestamp that the review was start on, null if not started"
)

completed_on = models.DateTimeField(
null=True, default=None,
null=True, default=None, blank=True,
help_text="The timestamp that the review was completed, null if not completed",
)

class Meta:
db_table = "review_tasks"
ordering = ("-created",)
get_latest_by = "created"
unique_together = ("user", "evaluation")
unique_together = ("user", "model_evaluation")

@property
def evaluation(self):
return self.model_evaluation.evaluation

@property
def model(self):
return self.model_evaluation.model

def prompts(self):
return self.model_evaluation.prompts()

@property
def is_started(self):
Expand All @@ -72,6 +84,20 @@ def is_started(self):
def is_completed(self):
return self.completed_on is not None

@property
def percent_complete(self):
n_prompts = self.prompts().count()
if n_prompts == 0:
return 0
n_reviews = self.response_reviews.count()
return int((float(n_reviews) / float(n_prompts)) * 100)

def __str__(self):
return f"{self.evaluation.name} ({self.model.name})"

def get_absolute_url(self):
return reverse("review-task", args=(self.id,))


class ResponseReview(TimestampedModel):

Expand All @@ -87,7 +113,7 @@ class ResponseReview(TimestampedModel):
'parley.Response',
null=False,
on_delete=models.CASCADE,
related_name=("reviews"),
related_name="reviews",
)

output_correct = models.BooleanField(
Expand Down
49 changes: 48 additions & 1 deletion parley/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
## Imports
##########################################################################

from django.utils import timezone
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete

from parley.models import Response, ModelEvaluation
from parley.models import Response, ModelEvaluation, ResponseReview


##########################################################################
Expand Down Expand Up @@ -61,3 +62,49 @@ def model_evaluation_unlink(sender, instance, *args, **kwargs):
ModelEvaluation.objects.get(**kwargs).delete()
except ModelEvaluation.DoesNotExist:
pass


##########################################################################
## Ensure ReviewTasks are
##########################################################################

@receiver(post_save, sender=ResponseReview, dispatch_uid="check_review_task_completion")
def check_review_task_completion(sender, instance, created, *args, **kwargs):
task = instance.review
changed = False

if not task.started_on:
task.started_on = timezone.localtime()
changed = True

if not task.completed_on:
n_prompts = task.prompts().count()
if n_prompts == 0 or task.response_reviews.count() == n_prompts:
task.completed_on = timezone.localtime()
changed = True

if changed:
task.save()


@receiver(post_delete, sender=ResponseReview, dispatch_uid="check_review_task_unfinished")
def check_review_task_unfinished(sender, instance, *args, **kwargs):
task = instance.review
if task.completed_on:
n_prompts = task.prompts().count()
if n_prompts == 0:
return

n_reviews = task.response_reviews.count()
changed = False

if n_reviews < n_prompts:
task.completed_on = None
changed = True

if n_reviews == 0:
task.started_on = None
changed = True

if changed:
task.save()
24 changes: 24 additions & 0 deletions parley/templates/evaluation/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ <h4 class="mb-1 name">

{% get_review_task user model as review_task %}
{% if review_task %}
{% if review_task.is_completed %}
<span class="badge py-2 text-bg-success">Review Complete!</span>
{% else %}
<a href="{{ review_task.get_absolute_url }}" class="btn btn-sm btn-white d-none d-xl-inline-block">
Continue Review
</a>
{% endif %}
{% else %}
<form action="{% url 'create-review-task' %}" method="post" class="m-0 p-0 d-inline-block">
<input type="hidden" name="user" value="{{ user.id }}" />
Expand All @@ -96,6 +100,26 @@ <h4 class="mb-1 name">
</form>
{% endif %}
</div>
<div class="col-auto">

<!-- Dropdown -->
<div class="dropdown">
<a href="#" class="dropdown-ellipses dropdown-toggle" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fe fe-more-vertical"></i>
</a>
<div class="dropdown-menu dropdown-menu-end">
{% if user.is_staff %}
<a href="{% url 'admin:parley_modelevaluation_change' model.pk %}" class="dropdown-item">
Edit in CMS
</a>
{% endif %}
<a href="{% url 'llm-detail' model.model.pk %}" class="dropdown-item">
View Model
</a>
</div>
</div>

</div>
</div>
</li>
{% empty %}
Expand Down
4 changes: 4 additions & 0 deletions parley/templates/reviews/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends 'page.html' %}

{% block page-pretitle %}Reviews{% endblock %}
{% block page-title %}{{ review }}{% endblock %}
2 changes: 1 addition & 1 deletion parley/templatetags/parlance.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def alert_level(level):
@register.simple_tag()
def get_review_task(user, evaluation):
try:
return ReviewTask.objects.get(user=user, evaluation=evaluation)
return ReviewTask.objects.get(user=user, model_evaluation=evaluation)
except ReviewTask.DoesNotExist:
return None
14 changes: 13 additions & 1 deletion parley/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

from parley.exceptions import ParlanceUploadError
from parley.forms import Uploader, CreateReviewForm
from parley.models import LLM, Response, Evaluation, Prompt
from parley.models import LLM, Response, Evaluation, Prompt, ReviewTask


##########################################################################
Expand Down Expand Up @@ -194,3 +194,15 @@ def form_invalid(self, form):

def get(self, *args, **kwargs):
return HttpResponseNotAllowed(['POST'])


class ReviewTaskDetail(DetailView):

model = ReviewTask
template_name = "reviews/detail.html"
context_object_name = "review"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_id"] = "review"
return context
Loading