productizationlabs commited on
Commit
c9b77d2
·
1 Parent(s): 5c7ecc8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -92
app.py CHANGED
@@ -1,92 +1,44 @@
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
-
11
- top_p_chatgpt = 1.0
12
- temperature_chatgpt = 1.0
13
-
14
- def predict_chatgpt(inputs,chat_counter_chatgpt, chatbot_chatgpt=[], history=[]):
15
- if chat_counter_chatgpt != 0:
16
- messages = []
17
- for data in chatbot_chatgpt:
18
- temp1 = {}
19
- temp1["role"] = "user"
20
- temp1["content"] = data[0]
21
- temp2 = {}
22
- temp2["role"] = "assistant"
23
- temp2["content"] = data[1]
24
- messages.append(temp1)
25
- messages.append(temp2)
26
- temp3 = {}
27
- temp3["role"] = "user"
28
- temp3["content"] = inputs
29
- messages.append(temp3)
30
-
31
- #os.environ['OPENAI_API_KEY'] = openai.api_key
32
- chat_counter_chatgpt += 1
33
- history.append("You asked: " + inputs)
34
-
35
- # load index from disk
36
- index = GPTSimpleVectorIndex.load_from_disk('IPCIndex.json')
37
-
38
- # query the index
39
- result = index.query(inputs)
40
- response = result.response.split()
41
- token_counter = 0
42
- partial_words = ""
43
- counter = 0
44
- for chunk in response:
45
- partial_words=partial_words+" "+chunk
46
- if token_counter == 0:
47
- history.append(" " + partial_words)
48
- else:
49
- history[-1] = partial_words
50
- chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)] # convert to tuples of list
51
- token_counter += 1
52
- yield chat, history, chat_counter_chatgpt # This resembles {chatbot: chat, state: history}
53
-
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 PLCoversationalAI:
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="Ask anything about Productization Labs ⤵️ Try : Who is Gopala" )
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="Productization Labs Conversational 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
- PLCoversationalAI.queue(concurrency_count=16).launch(height= 2500, debug=True)
88
-
89
-
90
-
91
-
92
-
 
1
+ _E='You are a Legal advisor. Do not answer anything other than legal matter queries.'
2
+ _D='system'
3
+ _C=True
4
+ _B='role'
5
+ _A='content'
6
+ import os,gradio as gr,json,requests,openai
7
+ try:openai.api_key=os.environ['OPENAI_API_KEY']
8
+ except KeyError:
9
+ error_message='System is at capacity right now.Please try again later';print(error_message)
10
+ def chatbot(input):return error_message
11
+ else:messages=[{_B:_D,_A:_E}]
12
+ messages=[{_B:_D,_A:_E}]
13
+ API_URL='https://api.openai.com/v1/chat/completions'
14
+ top_p_chatgpt=1.0
15
+ temperature_chatgpt=1.0
16
+ def chatbot(inputs,chat_counter_chatgpt,chatbot_chatgpt=[],history=[]):
17
+ X='delta';W='choices';V='gpt-3.5-turbo';U='frequency_penalty';T='presence_penalty';S='stream';R='top_p';Q='temperature';P='messages';O='model';J='user';F=chat_counter_chatgpt;E=inputs;A=history;K={O:V,P:[{_B:J,_A:f"{E}"}],Q:1.0,R:1.0,'n':1,S:_C,T:0,U:0};Y={'Content-Type':'application/json','Authorization':f"Bearer {openai.api_key}"}
18
+ if F!=0:
19
+ C=[]
20
+ for L in chatbot_chatgpt:G={};G[_B]=J;G[_A]=L[0];H={};H[_B]='assistant';H[_A]=L[1];C.append(G);C.append(H)
21
+ I={};I[_B]=J;I[_A]=E;C.append(I);K={O:V,P:C,Q:temperature_chatgpt,R:top_p_chatgpt,'n':1,S:_C,T:0,U:0}
22
+ F+=1;A.append('You asked: '+E);Z=requests.post(API_URL,headers=Y,json=K,stream=_C);M=0;D='';N=0
23
+ for B in Z.iter_lines():
24
+ if N==0:N+=1;continue
25
+ if B.decode():
26
+ B=B.decode()
27
+ if len(B)>13 and _A in json.loads(B[6:])[W][0][X]:
28
+ D=D+json.loads(B[6:])[W][0][X][_A]
29
+ if M==0:A.append(' '+D)
30
+ else:A[-1]=D
31
+ a=[(A[B],A[B+1])for B in range(0,len(A)-1,2)];M+=1;yield(a,A,F)
32
+ def reset_textbox():return gr.update(value='')
33
+ def reset_chat(chatbot,state):return None,[]
34
+ 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 ConversationalAILegal:
35
+ with gr.Row():
36
+ with gr.Column(scale=14):
37
+ with gr.Box():
38
+ with gr.Row():
39
+ with gr.Column(scale=13):inputs=gr.Textbox(label='Ask me anything ⤵️ Try: Binary equivalent of 10. Since this query is not related to legal, ChatGPT provides irrelavant response. Try: How to draft a legal notice. It works. ')
40
+ with gr.Column(scale=1):b1=gr.Button('Submit',elem_id='submit').style(full_width=_C);b2=gr.Button('Clear',elem_id='clear').style(full_width=_C)
41
+ state_chatgpt=gr.State([])
42
+ with gr.Box():
43
+ with gr.Row():chatbot_chatgpt=gr.Chatbot(elem_id='chatgpt',label='Conversational AI Legal')
44
+ chat_counter_chatgpt=gr.Number(value=0,visible=False,precision=0);inputs.submit(reset_textbox,[],[inputs]);b1.click(chatbot,[inputs,chat_counter_chatgpt,chatbot_chatgpt,state_chatgpt],[chatbot_chatgpt,state_chatgpt]);b2.click(reset_chat,[chatbot_chatgpt,state_chatgpt],[chatbot_chatgpt,state_chatgpt]);ConversationalAILegal.queue(concurrency_count=16).launch(height=2500,debug=_C)