diff options
| author | Bobby <[email protected]> | 2024-06-06 15:16:30 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-06-06 15:16:30 +0000 |
| commit | f34a3c0fc64c8e4b8f10235edd84ff87fac4ef66 (patch) | |
| tree | 063b9c7389ad435fd55fb53bfaad2d34e1e96866 /blog/context_processors.py | |
| parent | e9096190c2833319c04a5de55b313330c4b51808 (diff) | |
| download | thatcomputerscientist-f34a3c0fc64c8e4b8f10235edd84ff87fac4ef66.tar.xz thatcomputerscientist-f34a3c0fc64c8e4b8f10235edd84ff87fac4ef66.zip | |
AI Spam Check
Diffstat (limited to 'blog/context_processors.py')
| -rw-r--r-- | blog/context_processors.py | 28 |
1 files changed, 27 insertions, 1 deletions
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): |
