# SPDX-FileCopyrightText: Copyright (C) 2026 Omid Jafari <omidjafari.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from auditlog.models import LogEntry
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.forms.models import model_to_dict
from django.views.generic import TemplateView
from nlpmed_portal.nlp.constants import BLEEDING_KEYWORD_EXC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_KEYWORD_INC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_SECTION_EXC_LIST
from nlpmed_portal.nlp.constants import BLEEDING_SECTION_INC_LIST
from nlpmed_portal.nlp.constants import VTE_KEYWORD_EXC_LIST
from nlpmed_portal.nlp.constants import VTE_KEYWORD_INC_LIST
from nlpmed_portal.nlp.constants import VTE_SECTION_EXC_LIST
from nlpmed_portal.nlp.constants import VTE_SECTION_INC_LIST
from nlpmed_portal.nlp.forms import DuplicateCheckerForm
from nlpmed_portal.nlp.forms import EncodingFixerForm
from nlpmed_portal.nlp.forms import JoinerForm
from nlpmed_portal.nlp.forms import MLInferenceForm
from nlpmed_portal.nlp.forms import NLPForm
from nlpmed_portal.nlp.forms import NoteFilterForm
from nlpmed_portal.nlp.forms import PatternReplacerForm
from nlpmed_portal.nlp.forms import SectionFilterForm
from nlpmed_portal.nlp.forms import SectionSplitterForm
from nlpmed_portal.nlp.forms import SentenceExpanderForm
from nlpmed_portal.nlp.forms import SentenceFilterForm
from nlpmed_portal.nlp.forms import SentenceSegmenterForm
from nlpmed_portal.nlp.forms import WordMaskerForm
[docs]
class NLPPageView(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
permission_required = "nlp.view_nlp"
template_name = "nlp/nlp.html"
COMPONENT_MODELS = [
("encoding_fixer", EncodingFixerForm),
("pattern_replacer", PatternReplacerForm),
("word_masker", WordMaskerForm),
("note_filter", NoteFilterForm),
("section_splitter", SectionSplitterForm),
("section_filter", SectionFilterForm),
("sentence_segmenter", SentenceSegmenterForm),
("duplicate_checker", DuplicateCheckerForm),
("sentence_filter", SentenceFilterForm),
("sentence_expander", SentenceExpanderForm),
("joiner", JoinerForm),
("ml_inference", MLInferenceForm),
]
[docs]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
forms = {}
for key, form_class in self.COMPONENT_MODELS:
empty_form = form_class(prefix=key)
initial_data = model_to_dict(empty_form.instance)
for field, value in initial_data.items():
if isinstance(value, list):
initial_data[field] = "\n".join(value)
elif value is None:
initial_data[field] = ""
forms[key] = form_class(prefix=key, initial=initial_data)
nlp_form = NLPForm(prefix="nlp")
context.update(
{
"component_forms": forms,
"nlp_form": nlp_form,
"VTE_SECTION_INC_LIST": VTE_SECTION_INC_LIST,
"VTE_SECTION_EXC_LIST": VTE_SECTION_EXC_LIST,
"VTE_KEYWORD_INC_LIST": VTE_KEYWORD_INC_LIST,
"VTE_KEYWORD_EXC_LIST": VTE_KEYWORD_EXC_LIST,
"BLEEDING_SECTION_INC_LIST": BLEEDING_SECTION_INC_LIST,
"BLEEDING_SECTION_EXC_LIST": BLEEDING_SECTION_EXC_LIST,
"BLEEDING_KEYWORD_INC_LIST": BLEEDING_KEYWORD_INC_LIST,
"BLEEDING_KEYWORD_EXC_LIST": BLEEDING_KEYWORD_EXC_LIST,
},
)
return context
[docs]
def dispatch(self, request, *args, **kwargs):
ct = ContentType.objects.get_for_model(Site)
LogEntry.objects.create(
content_type=ct,
object_repr=f"nlp | {self.__class__.__name__}",
action=LogEntry.Action.ACCESS,
)
return super().dispatch(request, *args, **kwargs)
[docs]
class VteBertDemoPageView(TemplateView):
template_name = "nlp/demo.html"
[docs]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "VTE-BERT"
return context
[docs]
def dispatch(self, request, *args, **kwargs):
ct = ContentType.objects.get_for_model(Site)
LogEntry.objects.create(
content_type=ct,
object_repr=f"nlp | {self.__class__.__name__}",
action=LogEntry.Action.ACCESS,
)
return super().dispatch(request, *args, **kwargs)
[docs]
class BleedBertDemoPageView(TemplateView):
template_name = "nlp/demo.html"
[docs]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_title"] = "Bleed-BERT"
return context
[docs]
def dispatch(self, request, *args, **kwargs):
ct = ContentType.objects.get_for_model(Site)
LogEntry.objects.create(
content_type=ct,
object_repr=f"nlp | {self.__class__.__name__}",
action=LogEntry.Action.ACCESS,
)
return super().dispatch(request, *args, **kwargs)