diff --git a/.envs/.local/.django b/.envs/.local/.django index 3c2ac15..ddbdcc1 100755 --- a/.envs/.local/.django +++ b/.envs/.local/.django @@ -16,4 +16,7 @@ CELERY_FLOWER_PASSWORD=QgScyefPrYhHgO6onW61u0nazc5xdBuP4sM7jMRrBBFuA2RjsFhZLp7xb # Timeout fetch_data # ------------------------------------------------------------------------------ -FETCH_DATA_TIMEOUT=2 \ No newline at end of file +FETCH_DATA_TIMEOUT=2 + + +HF_TOKEN= \ No newline at end of file diff --git a/.gitignore b/.gitignore index ca9e27c..a3cd991 100644 --- a/.gitignore +++ b/.gitignore @@ -171,4 +171,6 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -# End of https://www.toptal.com/developers/gitignore/api/django \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/django + +llama3/llama-3.2/* \ No newline at end of file diff --git a/config/settings/base.py b/config/settings/base.py index 51948db..33db1ba 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -24,8 +24,6 @@ ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent # core/ APPS_DIR = ROOT_DIR / "core" -LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2" -MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf" env = environ.Env() READ_DOT_ENV_FILE = env.bool("DJANGO_READ_DOT_ENV_FILE", default=False) @@ -294,4 +292,8 @@ "ACCESS_TOKEN_LIFETIME": timedelta(minutes=60), "REFRESH_TOKEN_LIFETIME": timedelta(days=1), # "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",), -} \ No newline at end of file +} + +# LLAMA +LLAMA_MODEL_DIR = ROOT_DIR / "llama3/llama-3.2" +MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf" \ No newline at end of file diff --git a/llama3/download_model.py b/llama3/download_model.py deleted file mode 100644 index 0b93f8f..0000000 --- a/llama3/download_model.py +++ /dev/null @@ -1,15 +0,0 @@ -from huggingface_hub import login -from huggingface_hub import hf_hub_download - - -HF_TOKEN = 'INTRODUCE_TOKEN' - -login(token=HF_TOKEN) - -LLAMA_MODEL_DIR = "llama3/llama-3.2" -MODEL_LLAMA = "llama-3.2-3b-instruct-q4_k_m.gguf" -repo_id = 'hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF' -filename = MODEL_LLAMA -local_dir = LLAMA_MODEL_DIR - -downloaded_file = hf_hub_download(repo_id=repo_id, filename=filename, local_dir=local_dir) \ No newline at end of file diff --git a/llama3/generic_llama.py b/llama3/generic_llama.py index 2e56f16..5cc9d41 100644 --- a/llama3/generic_llama.py +++ b/llama3/generic_llama.py @@ -6,8 +6,8 @@ class GenericLlama: def __init__(self, messages, response_format, max_tokens=4000, temperature=0.5, top_p=0.5): - self. llm = Llama(model_path = os.path.join(LLAMA_MODEL_DIR, MODEL_LLAMA), n_ctx=4000) - self. messages = messages + self.llm = Llama(model_path = os.path.join(LLAMA_MODEL_DIR, MODEL_LLAMA), n_ctx=4000) + self.messages = messages self.response_format = response_format self.max_tokens = max_tokens self.temperature = temperature diff --git a/reference/management/commands/download_model.py b/reference/management/commands/download_model.py new file mode 100644 index 0000000..39f99b7 --- /dev/null +++ b/reference/management/commands/download_model.py @@ -0,0 +1,48 @@ +import os +from pathlib import Path + +from django.core.management.base import BaseCommand, CommandError +from huggingface_hub import hf_hub_download, login + + +class Command(BaseCommand): + help = "Download the model from HuggingFace" + + def add_arguments(self, parser): + parser.add_argument( + "--dir", + type=str, + default="llama3/llama-3.2", + help="Directory to download the model", + ) + parser.add_argument( + "--repo", + type=str, + default="hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF", + ) + parser.add_argument( + "--filename", + type=str, + default="llama-3.2-3b-instruct-q4_k_m.gguf", + help="Model name", + ) + parser.add_argument("--force", action="store_true", help="Force download") + + def handle(self, *args, **options): + token = os.getenv("HF_TOKEN") + if not token: + raise CommandError("You need to set the HF_TOKEN environment variable") + login(token=token, add_to_git_credential=False) + + target_dir = Path(options["dir"]) + target_dir.mkdir(parents=True, exist_ok=True) + + downloaded_file = hf_hub_download( + repo_id=options["repo"], + filename=options["filename"], + local_dir=str(target_dir), + local_dir_use_symlinks=False, + force_download=options["force"], + resume_download=True, + ) + self.stdout.write(self.style.SUCCESS(f"Downloaded {downloaded_file}")) diff --git a/reference/models.py b/reference/models.py index b38dd2a..e5e39aa 100755 --- a/reference/models.py +++ b/reference/models.py @@ -10,7 +10,7 @@ from django.core.validators import MinValueValidator, MaxValueValidator from core.forms import CoreAdminModelForm -# Create your models here + class Reference(CommonControlField, ClusterableModel): mixed_citation = models.TextField(_("Mixed Citation"), null=False, blank=True) @@ -18,7 +18,7 @@ class Reference(CommonControlField, ClusterableModel): panels = [ FieldPanel('mixed_citation'), - InlinePanel('element_citation') + InlinePanel('element_citation', label=_("Cited Elements")) ] base_form_class = CoreAdminModelForm diff --git a/reference/wagtail_hooks.py b/reference/wagtail_hooks.py index 8730e19..c7c5255 100644 --- a/reference/wagtail_hooks.py +++ b/reference/wagtail_hooks.py @@ -1,18 +1,11 @@ from django.http import HttpResponseRedirect from django.utils.translation import gettext_lazy as _ -from wagtail_modeladmin.options import ( - ModelAdmin, - ModelAdminGroup, - modeladmin_register, -) -from wagtail_modeladmin.views import CreateView -from wagtail.admin.menu import MenuItem +from wagtail.snippets.models import register_snippet +from wagtail.snippets.views.snippets import CreateView, SnippetViewSet +from reference.models import Reference from reference.tasks import get_reference -from reference.models import ( - Reference -) class ReferenceCreateView(CreateView): def form_valid(self, form): @@ -38,18 +31,16 @@ def form_valid(self, form): -class ReferenceAdmin(ModelAdmin): +class ReferenceAdmin(SnippetViewSet): model = Reference - create_view_class = ReferenceCreateView - #edit_view_class = ArticleDocxEditView + add_view_class = ReferenceCreateView menu_label = _("Reference") menu_icon = "folder" menu_order = 1 - add_to_settings_menu = False # or True to add your model to the Settings sub-menu exclude_from_explorer = ( - False # or True to exclude pages of this type from Wagtail's explorer view + False ) list_per_page = 20 + add_to_admin_menu = True - -modeladmin_register(ReferenceAdmin) \ No newline at end of file +register_snippet(ReferenceAdmin) \ No newline at end of file