|
_E='You are a Legal advisor. Do not answer anything other than legal matter queries.' |
|
_D='system' |
|
_C=True |
|
_B='role' |
|
_A='content' |
|
import os,gradio as gr,json,requests,openai |
|
try:openai.api_key=os.environ['OPENAI_API_KEY'] |
|
except KeyError: |
|
error_message='System is at capacity right now.Please try again later';print(error_message) |
|
def chatbot(input):return error_message |
|
else:messages=[{_B:_D,_A:_E}] |
|
messages=[{_B:_D,_A:_E}] |
|
API_URL='https://api.openai.com/v1/chat/completions' |
|
top_p_chatgpt=1.0 |
|
temperature_chatgpt=1.0 |
|
def chatbot(inputs,chat_counter_chatgpt,chatbot_chatgpt=[],history=[]): |
|
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}"} |
|
if F!=0: |
|
C=[] |
|
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) |
|
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} |
|
F+=1;A.append('You asked: '+E);Z=requests.post(API_URL,headers=Y,json=K,stream=_C);M=0;D='';N=0 |
|
for B in Z.iter_lines(): |
|
if N==0:N+=1;continue |
|
if B.decode(): |
|
B=B.decode() |
|
if len(B)>13 and _A in json.loads(B[6:])[W][0][X]: |
|
D=D+json.loads(B[6:])[W][0][X][_A] |
|
if M==0:A.append(' '+D) |
|
else:A[-1]=D |
|
a=[(A[B],A[B+1])for B in range(0,len(A)-1,2)];M+=1;yield(a,A,F) |
|
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 ConversationalAILegal: |
|
with gr.Row(): |
|
with gr.Column(scale=14): |
|
with gr.Box(): |
|
with gr.Row(): |
|
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. ') |
|
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) |
|
state_chatgpt=gr.State([]) |
|
with gr.Box(): |
|
with gr.Row():chatbot_chatgpt=gr.Chatbot(elem_id='chatgpt',label='Conversational AI Legal') |
|
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) |