Spaces:
Paused
Paused
rick
commited on
add moderation features...
Browse files- core/moderation.py +37 -0
core/moderation.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#coding: utf-8
|
| 2 |
+
|
| 3 |
+
from typing import Any
|
| 4 |
+
from typing import Dict
|
| 5 |
+
#from typing import List
|
| 6 |
+
#from typing import Tuple
|
| 7 |
+
|
| 8 |
+
from typing import Optional
|
| 9 |
+
#from typing import Union
|
| 10 |
+
from os import getenv
|
| 11 |
+
import json
|
| 12 |
+
|
| 13 |
+
from openai import OpenAI
|
| 14 |
+
from dotenv import load_dotenv
|
| 15 |
+
|
| 16 |
+
def moderate_text(input_text: str,
|
| 17 |
+
model: Optional[str] = "omni-moderation-latest") -> Dict[str, Any]:
|
| 18 |
+
|
| 19 |
+
# Load environment variables from the .env file
|
| 20 |
+
load_dotenv()
|
| 21 |
+
|
| 22 |
+
# correct values for `model` is "omni-moderation-latest" and "text-moderation-latest"
|
| 23 |
+
try:
|
| 24 |
+
client = OpenAI(api_key=getenv("OPENAI_API_KEY"))
|
| 25 |
+
response = client.moderations.create(
|
| 26 |
+
model=f"{model}",
|
| 27 |
+
input=f"{input_text}"
|
| 28 |
+
)
|
| 29 |
+
return response
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"An error occurred: {e}")
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|