File size: 1,346 Bytes
7db1e02
 
c3e03be
7db1e02
c3e03be
7db1e02
c3e03be
7db1e02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167ecf0
 
 
7db1e02
 
 
 
 
 
 
 
 
 
 
167ecf0
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import gradio as gr
import os

openai_api_key = os.environ.get('openai_api_key')

def moderation(input_text,api_key=openai_api_key):
  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."

title_css = "text-align: center;"
desc_css = "text-align: center; color: gray;"


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,
    css=f"""
        #header-title {{
            {title_css}
        }}
        #header-description {{
            {desc_css}
        }}
    """
)
demo.queue(max_size = 20)

demo.launch(share = True)