Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -54,15 +54,15 @@ def format_prompt(message, history):
|
|
54 |
result = []
|
55 |
|
56 |
|
57 |
-
def chat_inf(system_prompt,prompt,history,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem):
|
58 |
#token max=8192
|
59 |
hist_len=0
|
60 |
client=clients[int(client_choice)-1]
|
61 |
if not history:
|
62 |
history = []
|
63 |
hist_len=0
|
64 |
-
if
|
65 |
-
for ea in
|
66 |
hist_len+=len(str(ea))
|
67 |
print(hist_len)
|
68 |
in_len=len(system_prompt+prompt)+hist_len
|
@@ -71,7 +71,8 @@ def chat_inf(system_prompt,prompt,history,client_choice,seed,temp,tokens,top_p,r
|
|
71 |
print("\n######### HIST "+str(in_len))
|
72 |
print("\n######### TOKENS "+str(tokens))
|
73 |
if (in_len+tokens) > 8000:
|
74 |
-
|
|
|
75 |
#hist=compress_history(history,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem)
|
76 |
#yield [(prompt,"History has been compressed, processing request...")]
|
77 |
#history.append((prompt,hist))
|
@@ -85,16 +86,16 @@ def chat_inf(system_prompt,prompt,history,client_choice,seed,temp,tokens,top_p,r
|
|
85 |
seed=seed,
|
86 |
)
|
87 |
#formatted_prompt=prompt
|
88 |
-
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}",
|
89 |
print("\n######### PROMPT "+str(len(formatted_prompt)))
|
90 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
91 |
output = ""
|
92 |
for response in stream:
|
93 |
output += response.token.text
|
94 |
-
yield [(prompt,output)]
|
95 |
history.append((prompt,output))
|
96 |
-
memory
|
97 |
-
yield history
|
98 |
|
99 |
def get_screenshot(chat: list,height=5000,width=600,chatblock=[],theme="light",wait=3000,header=True):
|
100 |
print(chatblock)
|
@@ -162,9 +163,9 @@ with gr.Blocks() as app:
|
|
162 |
|
163 |
|
164 |
im_go=im_btn.click(get_screenshot,[chat_b,im_height,im_width,chatblock,theme,wait_time],img)
|
165 |
-
chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem],chat_b)
|
166 |
|
167 |
-
go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem],[chat_b])
|
168 |
|
169 |
stop_btn.click(None,None,None,cancels=[go,im_go,chat_sub])
|
170 |
clear_btn.click(clear_fn,None,[inp,sys_inp,chat_b])
|
|
|
54 |
result = []
|
55 |
|
56 |
|
57 |
+
def chat_inf(system_prompt,prompt,history,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem):
|
58 |
#token max=8192
|
59 |
hist_len=0
|
60 |
client=clients[int(client_choice)-1]
|
61 |
if not history:
|
62 |
history = []
|
63 |
hist_len=0
|
64 |
+
if memory:
|
65 |
+
for ea in memory[0-chat_mem:]:
|
66 |
hist_len+=len(str(ea))
|
67 |
print(hist_len)
|
68 |
in_len=len(system_prompt+prompt)+hist_len
|
|
|
71 |
print("\n######### HIST "+str(in_len))
|
72 |
print("\n######### TOKENS "+str(tokens))
|
73 |
if (in_len+tokens) > 8000:
|
74 |
+
history.append((prompt,"Wait, that's too many tokens, please reduce the Chat Memory value"))
|
75 |
+
yield history,memory
|
76 |
#hist=compress_history(history,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem)
|
77 |
#yield [(prompt,"History has been compressed, processing request...")]
|
78 |
#history.append((prompt,hist))
|
|
|
86 |
seed=seed,
|
87 |
)
|
88 |
#formatted_prompt=prompt
|
89 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", memory[0-chat_mem:])
|
90 |
print("\n######### PROMPT "+str(len(formatted_prompt)))
|
91 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
92 |
output = ""
|
93 |
for response in stream:
|
94 |
output += response.token.text
|
95 |
+
yield [(prompt,output)],memory
|
96 |
history.append((prompt,output))
|
97 |
+
memory.append((prompt,output))
|
98 |
+
yield history,memory
|
99 |
|
100 |
def get_screenshot(chat: list,height=5000,width=600,chatblock=[],theme="light",wait=3000,header=True):
|
101 |
print(chatblock)
|
|
|
163 |
|
164 |
|
165 |
im_go=im_btn.click(get_screenshot,[chat_b,im_height,im_width,chatblock,theme,wait_time],img)
|
166 |
+
chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem],[chat_b,memory])
|
167 |
|
168 |
+
go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,memory,client_choice,seed,temp,tokens,top_p,rep_p,chat_mem],[chat_b,memory])
|
169 |
|
170 |
stop_btn.click(None,None,None,cancels=[go,im_go,chat_sub])
|
171 |
clear_btn.click(clear_fn,None,[inp,sys_inp,chat_b])
|