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) print(response.json()) 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',placeholder="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)