File size: 1,103 Bytes
7db1e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import gradio as gr



def moderation(input_text,api_key='sk-proj-KMeL7IIELJPekicYQOYmT3BlbkFJT8CBEbRL5h3OBExmCMHx'):
  headers = {
      "Content-Type": "application/json",
      "Authorization": f"Bearer {api_key}"
  }
  data = {
      "input": input_text
  }

  response = requests.post('https://api.openai.com/v1/moderations',headers=headers,json=data)

  result = response.json()['results'][0]

  flag = 1 if result['flagged'] else 0
  categories = result['categories']
  scores = result['category_scores']
  
  return flag,scores



title = "Smart content moderation"
description = "Check whether your content is potentially harmful."


demo = gr.Interface(
    fn=moderation,
    inputs=[gr.Text(label='Content',info="Input your text to check")],
    outputs=[
        gr.Slider(label="Is it harmful?",info="1 is harmful, 0 is safe",minimum=0,maximum=1),
        # gr.CheckboxGroup(),
        # gr.Text(label="Categories Flag")
        gr.Label(label='Categories')
        ],
    title = title,
    description = description
)
demo.queue(max_size = 20)

demo.launch(share = True)