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)