diff options
| -rw-r--r-- | .github/workflows/main.yml | 21 | ||||
| -rw-r--r-- | blog/context_processors.py | 28 | ||||
| -rw-r--r-- | requirements.txt | 1 | ||||
| -rw-r--r-- | 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 %} <div id="new-comment" class="mtsbitem"> <h2>Leave a Comment</h2> + {% if messages %} + {% for message in messages %} + {% if 'spam' in message.tags %} + <p style="color: red;">Your comment was not allowed as it was marked as possible spam. If you think this is a mistake, please contact me at <a href="mailto:[email protected]">[email protected]</a>.</p> + {% endif %} + {% endfor %} + {% endif %} <form action="{% url 'blog:comment' post.slug %}" method="POST"> {% csrf_token %} <textarea required name="comment" id="comment" cols="88" rows="10" style="width: 710px; display: block; margin-bottom: 15px;" placeholder="Your comment here..."></textarea> @@ -362,13 +369,6 @@ background: #ffffff0d; <script type="text/javascript"> var onImage = "{% static 'images/site/on.png' %}"; var offImage = "{% static 'images/site/off.png' %}"; - {% if messages %} - {% for message in messages %} - {% if 'spam' in message.tags %} - alert('Your comment was marked as spam. If you think this is a mistake, please contact me.'); - {% endif %} - {% endfor %} - {% endif %} {% if request.COOKIES.anonymous_name and request.COOKIES.anonymous_email and request.COOKIES.anonymous_token %} $('#ancmClick').toggle(); $('#anonymous-comment-form').toggle(); |
