This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added the toxicity for the ai bot (#52)
- Loading branch information
Showing
14 changed files
with
145 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
"rest_framework", | ||
"corsheaders", | ||
"chat", | ||
"toxicity", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ToxicityConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "toxicity" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Dict, Union, List | ||
import requests | ||
import os | ||
|
||
RESPONSE_TYPE = Dict[str, Union[str, float]] | ||
|
||
|
||
def _transform_response(response: List[List[RESPONSE_TYPE]]) -> RESPONSE_TYPE: | ||
""" | ||
Transforms the response from the API call. | ||
Args: | ||
response (List[List[RESPONSE_TYPE]]): The response from the API call. | ||
Returns: | ||
RESPONSE_TYPE: The transformed response. | ||
""" | ||
if response[0][0]["score"] > response[0][1]["score"]: | ||
return response[0][0] | ||
return response[0][1] | ||
|
||
|
||
def call(text: str) -> RESPONSE_TYPE: | ||
""" | ||
Calls the Hugging Face API to classify toxic comments. | ||
Args: | ||
text (str): The text to classify. | ||
Returns: | ||
RESPONSE_TYPE: The response from the API. | ||
""" | ||
if "HF_TOKEN" not in os.environ or not os.environ["HF_TOKEN"]: | ||
return { | ||
"success": False, | ||
"message": "Huggingface Token not found or empty", | ||
} | ||
|
||
HF_TOKEN = os.environ["HF_TOKEN"] | ||
headers = {"Authorization": f"Bearer {HF_TOKEN}"} | ||
api_url = ( | ||
"https://api-inference.huggingface.co/models/martin-ha/toxic-comment-model" | ||
) | ||
|
||
response = requests.post(api_url, headers=headers, json={"inputs": text}) | ||
|
||
if response.status_code != 200: | ||
return { | ||
"success": False, | ||
"message": f"API returned status code {response.status_code}", | ||
} | ||
|
||
response_data = response.json() | ||
transformed_response = _transform_response(response_data) | ||
transformed_response["success"] = True | ||
|
||
return transformed_response |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path("", views.index, name="toxic"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Dict, Union | ||
from rest_framework.decorators import api_view | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from .hf_call import call as hf_call | ||
|
||
@api_view(["POST"]) | ||
def index(request): | ||
content: dict = request.data | ||
text = content.get("text") | ||
if text is None: | ||
return Response( | ||
{"success": False, "message": "text is required !"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
if not isinstance(text, str): | ||
return Response( | ||
{"success": False, "message": "text should be str !"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
if not text: | ||
return Response( | ||
{"success": False, "message": "query should not be empty"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
response:Dict[str, Union[bool, str]] = hf_call(text=text) | ||
|
||
if not response["success"]: | ||
return Response( | ||
response, | ||
status=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
) | ||
|
||
return Response( | ||
response, | ||
status=status.HTTP_200_OK, | ||
) | ||
|