Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,23 +5,23 @@ import os
|
|
5 |
openai_api_key = os.environ.get('openai_api_key')
|
6 |
|
7 |
def moderation(input_text,api_key=openai_api_key):
|
8 |
-
|
9 |
"Content-Type": "application/json",
|
10 |
"Authorization": f"Bearer {api_key}"
|
11 |
-
|
12 |
-
|
13 |
"input": input_text
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
|
27 |
|
|
|
5 |
openai_api_key = os.environ.get('openai_api_key')
|
6 |
|
7 |
def moderation(input_text,api_key=openai_api_key):
|
8 |
+
headers = {
|
9 |
"Content-Type": "application/json",
|
10 |
"Authorization": f"Bearer {api_key}"
|
11 |
+
}
|
12 |
+
data = {
|
13 |
"input": input_text
|
14 |
+
}
|
15 |
+
|
16 |
+
response = requests.post('https://api.openai.com/v1/moderations',headers=headers,json=data)
|
17 |
+
print(response.json())
|
18 |
+
result = response.json()['results'][0]
|
19 |
+
|
20 |
+
flag = 1 if result['flagged'] else 0
|
21 |
+
categories = result['categories']
|
22 |
+
scores = result['category_scores']
|
23 |
+
|
24 |
+
return flag,scores
|
25 |
|
26 |
|
27 |
|