File size: 2,495 Bytes
2fbc69b
 
601d05b
a706eeb
2fbc69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
_A=True
import os
from llama_index import PromptHelper,GPTVectorStoreIndex
from langchain import OpenAI
import gradio as gr,openai
API_URL='https://api.openai.com/v1/chat/completions'
openai.api_key=os.environ['OPENAI_API_KEY']
top_p_chatgpt=1.0
temperature_chatgpt=1.0
def predict_chatgpt(inputs,chat_counter_chatgpt,chatbot_chatgpt=[],history=[]):
        N='user';J='content';I='role';D=chat_counter_chatgpt;C=inputs;A=history
        if D!=0:
                E=[]
                for K in chatbot_chatgpt:F={};F[I]=N;F[J]=K[0];G={};G[I]='assistant';G[J]=K[1];E.append(F);E.append(G)
                H={};H[I]=N;H[J]=C;E.append(H)
        D+=1;A.append('You typed: '+C);O=openai.Moderation.create(input=C);P=O['results'][0].flagged
        if P==_A:L='This content is offensive and needs to be moderated'
        else:L="This content doesn't need moderation"
        Q=L.split();M=0;B='';T=0
        for R in Q:
                B=B+' '+R
                if M==0:A.append(' '+B)
                else:A[-1]=B
                S=[(A[B],A[B+1])for B in range(0,len(A)-1,2)];M+=1;yield(S,A,D)
def reset_textbox():return gr.update(value='')
def reset_chat(chatbot,state):return None,[]
with gr.Blocks(css='#col_container {width: 1000px; margin-left: auto; margin-right: auto;}\n                #chatgpt {height: 400px; overflow: auto;}} ',theme=gr.themes.Default(primary_hue='slate'))as ModerationAI:
        with gr.Row():
                with gr.Column(scale=14):
                        with gr.Box():
                                with gr.Row():
                                        with gr.Column(scale=13):inputs=gr.Textbox(label='Type any abusive sentence ⤵️    Try : I will pi** on you')
                                        with gr.Column(scale=1):b1=gr.Button('Submit',elem_id='submit').style(full_width=_A);b2=gr.Button('Clear',elem_id='clear').style(full_width=_A)
                                        state_chatgpt=gr.State([])
                        with gr.Box():
                                with gr.Row():chatbot_chatgpt=gr.Chatbot(elem_id='chatgpt',label='Moderation AI')
        chat_counter_chatgpt=gr.Number(value=0,visible=False,precision=0);inputs.submit(reset_textbox,[],[inputs]);b1.click(predict_chatgpt,[inputs,chat_counter_chatgpt,chatbot_chatgpt,state_chatgpt],[chatbot_chatgpt,state_chatgpt]);b2.click(reset_chat,[chatbot_chatgpt,state_chatgpt],[chatbot_chatgpt,state_chatgpt]);ModerationAI.queue(concurrency_count=16).launch(height=2500,debug=_A)