productizationlabs commited on
Commit
2fbc69b
·
1 Parent(s): ae719b3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -89
app.py CHANGED
@@ -1,90 +1,37 @@
1
- import os
2
- from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
 
3
  from langchain import OpenAI
4
- import gradio as gr
5
- import openai
6
-
7
- API_URL = "https://api.openai.com/v1/chat/completions"
8
-
9
- openai.api_key = os.environ["OPENAI_API_KEY"]
10
- top_p_chatgpt = 1.0
11
- temperature_chatgpt = 1.0
12
-
13
- def predict_chatgpt(inputs,chat_counter_chatgpt, chatbot_chatgpt=[], history=[]):
14
- if chat_counter_chatgpt != 0:
15
- messages = []
16
- for data in chatbot_chatgpt:
17
- temp1 = {}
18
- temp1["role"] = "user"
19
- temp1["content"] = data[0]
20
- temp2 = {}
21
- temp2["role"] = "assistant"
22
- temp2["content"] = data[1]
23
- messages.append(temp1)
24
- messages.append(temp2)
25
- temp3 = {}
26
- temp3["role"] = "user"
27
- temp3["content"] = inputs
28
- messages.append(temp3)
29
-
30
- #os.environ['OPENAI_API_KEY'] = openai.api_key
31
- chat_counter_chatgpt += 1
32
- history.append("You typed: " + inputs)
33
-
34
- res = openai.Moderation.create(input=inputs)
35
- reply = res["results"][0].flagged
36
- if reply == True:
37
- result= "This content is offensive and needs to be moderated"
38
- else:
39
- result= "This content doesn't need moderation"
40
-
41
- response = result.split()
42
- token_counter = 0
43
- partial_words = ""
44
- counter = 0
45
- for chunk in response:
46
- partial_words=partial_words+" "+chunk
47
- if token_counter == 0:
48
- history.append(" " + partial_words)
49
- else:
50
- history[-1] = partial_words
51
- chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)] # convert to tuples of list
52
- token_counter += 1
53
- yield chat, history, chat_counter_chatgpt # This resembles {chatbot: chat, state: history}
54
-
55
- def reset_textbox():
56
- return gr.update(value="")
57
-
58
- def reset_chat(chatbot, state):
59
- return None, []
60
- with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
61
- #chatgpt {height: 400px; overflow: auto;}} """, theme=gr.themes.Default(primary_hue="slate") ) as ModerationAI:
62
- with gr.Row():
63
- with gr.Column(scale=14):
64
- with gr.Box():
65
- with gr.Row():
66
- with gr.Column(scale=13):
67
- inputs = gr.Textbox(label="Type any abusive sentence ⤵️ Try : I will pi** on you" )
68
- with gr.Column(scale=1):
69
- b1 = gr.Button('Submit', elem_id = 'submit').style(full_width=True)
70
- b2 = gr.Button('Clear', elem_id = 'clear').style(full_width=True)
71
- state_chatgpt = gr.State([])
72
-
73
- with gr.Box():
74
- with gr.Row():
75
- chatbot_chatgpt = gr.Chatbot(elem_id="chatgpt", label="Moderation AI")
76
- chat_counter_chatgpt = gr.Number(value=0, visible=False, precision=0)
77
-
78
-
79
- inputs.submit(reset_textbox, [], [inputs])
80
-
81
- b1.click( predict_chatgpt,
82
- [ inputs, chat_counter_chatgpt, chatbot_chatgpt, state_chatgpt],
83
- [chatbot_chatgpt, state_chatgpt],)
84
-
85
- b2.click(reset_chat, [chatbot_chatgpt, state_chatgpt], [chatbot_chatgpt, state_chatgpt])
86
-
87
- ModerationAI.queue(concurrency_count=16).launch(height= 2500, debug=True)
88
-
89
-
90
-
 
1
+ _A=True
2
+ import os
3
+ from gpt_index import SimpleDirectoryReader,GPTSimpleVectorIndex,LLMPredictor,PromptHelper
4
  from langchain import OpenAI
5
+ import gradio as gr,openai
6
+ API_URL='https://api.openai.com/v1/chat/completions'
7
+ openai.api_key=os.environ['OPENAI_API_KEY']
8
+ top_p_chatgpt=1.0
9
+ temperature_chatgpt=1.0
10
+ def predict_chatgpt(inputs,chat_counter_chatgpt,chatbot_chatgpt=[],history=[]):
11
+ N='user';J='content';I='role';D=chat_counter_chatgpt;C=inputs;A=history
12
+ if D!=0:
13
+ E=[]
14
+ 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)
15
+ H={};H[I]=N;H[J]=C;E.append(H)
16
+ D+=1;A.append('You typed: '+C);O=openai.Moderation.create(input=C);P=O['results'][0].flagged
17
+ if P==_A:L='This content is offensive and needs to be moderated'
18
+ else:L="This content doesn't need moderation"
19
+ Q=L.split();M=0;B='';T=0
20
+ for R in Q:
21
+ B=B+' '+R
22
+ if M==0:A.append(' '+B)
23
+ else:A[-1]=B
24
+ S=[(A[B],A[B+1])for B in range(0,len(A)-1,2)];M+=1;yield(S,A,D)
25
+ def reset_textbox():return gr.update(value='')
26
+ def reset_chat(chatbot,state):return None,[]
27
+ 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:
28
+ with gr.Row():
29
+ with gr.Column(scale=14):
30
+ with gr.Box():
31
+ with gr.Row():
32
+ with gr.Column(scale=13):inputs=gr.Textbox(label='Type any abusive sentence ⤵️ Try : I will pi** on you')
33
+ 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)
34
+ state_chatgpt=gr.State([])
35
+ with gr.Box():
36
+ with gr.Row():chatbot_chatgpt=gr.Chatbot(elem_id='chatgpt',label='Moderation AI')
37
+ 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)