Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def moderation(input_text,api_key='sk-proj-KMeL7IIELJPekicYQOYmT3BlbkFJT8CBEbRL5h3OBExmCMHx'):
|
| 7 |
+
headers = {
|
| 8 |
+
"Content-Type": "application/json",
|
| 9 |
+
"Authorization": f"Bearer {api_key}"
|
| 10 |
+
}
|
| 11 |
+
data = {
|
| 12 |
+
"input": input_text
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
response = requests.post('https://api.openai.com/v1/moderations',headers=headers,json=data)
|
| 16 |
+
|
| 17 |
+
result = response.json()['results'][0]
|
| 18 |
+
|
| 19 |
+
flag = 1 if result['flagged'] else 0
|
| 20 |
+
categories = result['categories']
|
| 21 |
+
scores = result['category_scores']
|
| 22 |
+
|
| 23 |
+
return flag,scores
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
title = "Smart content moderation"
|
| 28 |
+
description = "Check whether your content is potentially harmful."
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(
|
| 32 |
+
fn=moderation,
|
| 33 |
+
inputs=[gr.Text(label='Content',info="Input your text to check")],
|
| 34 |
+
outputs=[
|
| 35 |
+
gr.Slider(label="Is it harmful?",info="1 is harmful, 0 is safe",minimum=0,maximum=1),
|
| 36 |
+
# gr.CheckboxGroup(),
|
| 37 |
+
# gr.Text(label="Categories Flag")
|
| 38 |
+
gr.Label(label='Categories')
|
| 39 |
+
],
|
| 40 |
+
title = title,
|
| 41 |
+
description = description
|
| 42 |
+
)
|
| 43 |
+
demo.queue(max_size = 20)
|
| 44 |
+
|
| 45 |
+
demo.launch(share = True)
|