From f34a3c0fc64c8e4b8f10235edd84ff87fac4ef66 Mon Sep 17 00:00:00 2001 From: Bobby <30593201+luciferreeves@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:16:30 +0000 Subject: AI Spam Check --- .github/workflows/main.yml | 21 ++++++++++++--------- blog/context_processors.py | 28 +++++++++++++++++++++++++++- requirements.txt | 1 + templates/blog/post.html | 14 +++++++------- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b9e73c52..90f0922c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,16 +1,19 @@ name: remote ssh command on: [push] jobs: - build: name: Build runs-on: ubuntu-latest steps: - - name: executing remote ssh commands using ssh key - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.HOST }} - username: ${{ secrets.USERNAME }} - key: ${{ secrets.KEY_ED25519 }} - port: ${{ secrets.PORT }} - script: sh /home/ubuntu/deploy.sh \ No newline at end of file + - name: executing remote ssh commands using ssh key + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.KEY_ED25519 }} + port: ${{ secrets.PORT }} + script: | + if [ -z "$GEMINI_API_KEY" ]; then + export GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }} + fi + sh /home/ubuntu/deploy.sh diff --git a/blog/context_processors.py b/blog/context_processors.py index acb09082..c4c5cd66 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -10,11 +10,14 @@ from django.core.cache import cache from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import get_lexer_by_name, guess_lexer +import google.generativeai as genai from .models import Category, Comment, Post dotenv.load_dotenv() +gemini_api_key = os.getenv("GEMINI_API_KEY") + akismet_api = akismet.Akismet( key=os.getenv("AKISMET_API_KEY"), blog_url="https://preview.thatcomputerscientist.com" @@ -24,13 +27,36 @@ akismet_api = akismet.Akismet( def check_spam(user_ip, user_agent, comment, author): + spam = False akismet_data = { "comment_type": "comment", "comment_author": author, "comment_content": comment, "is_test": settings.DEBUG, } - return akismet_api.comment_check(user_ip, user_agent, **akismet_data) + spam = akismet_api.comment_check(user_ip, user_agent, **akismet_data) + + if spam: + return spam + + # Now we check with Google Generative AI + if gemini_api_key is None: + return spam + else: + genai.configure(api_key=gemini_api_key) + + model = genai.GenerativeModel('gemini-1.5-flash') + + input_prompt = f"Comment Processing Checker. This is for a personal blog site. Output only Y or N for the included text. Y if the comment seems like spam. N if the comment seems safe. Do not access links. Just mark Y or N for the text. \n\nText: {comment}" + + response = model.generate_content(input_prompt) + + r_text = response.text + + if r_text == "Y": + spam = True + + return spam def add_excerpt(post): diff --git a/requirements.txt b/requirements.txt index 107f3920..d8b03c97 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,3 +30,4 @@ apscheduler django-sslserver djangorestframework django-cors-headers +google-generativeai diff --git a/templates/blog/post.html b/templates/blog/post.html index f0027ecc..b0b6194b 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -180,6 +180,13 @@ background: #ffffff0d; {% if user.is_authenticated %}
Your comment was not allowed as it was marked as possible spam. If you think this is a mistake, please contact me at webmaster@thatcomputerscientist.com.
+ {% endif %} + {% endfor %} + {% endif %}