Spaces:
Running
Running
hugforziio
commited on
Commit
β’
5839265
1
Parent(s):
4c7da61
Update app.py
Browse files
app.py
CHANGED
@@ -44,10 +44,12 @@ DEFAULT_PROMPT = [
|
|
44 |
|
45 |
|
46 |
def on_click_send_btn(
|
47 |
-
|
48 |
chat_model, temperature, top_p, choices_num, stream, max_tokens, presence_penalty, frequency_penalty, logit_bias,
|
49 |
):
|
50 |
|
|
|
|
|
51 |
print('\n\n\n\n\n')
|
52 |
print(prompt_table)
|
53 |
prompt_table = prompt_table or []
|
@@ -65,7 +67,7 @@ def on_click_send_btn(
|
|
65 |
chat_log_md += '\n---\n'
|
66 |
|
67 |
# if chat_input=='':
|
68 |
-
# return old_state, chat_log, chat_log_md, chat_log_md, None, None, chat_input
|
69 |
|
70 |
print('\n')
|
71 |
print(chat_input)
|
@@ -74,7 +76,7 @@ def on_click_send_btn(
|
|
74 |
try:
|
75 |
logit_bias_json = json.dumps(logit_bias) if logit_bias else None
|
76 |
except:
|
77 |
-
return old_state, chat_log, chat_log_md, chat_log_md, None, None, chat_input
|
78 |
|
79 |
new_state = copy.deepcopy(old_state) or {}
|
80 |
|
@@ -138,7 +140,7 @@ def on_click_send_btn(
|
|
138 |
chat_log.append([the_response_role, the_response])
|
139 |
chat_log_md += f"##### `{the_response_role}`\n\n{the_response}\n\n"
|
140 |
|
141 |
-
return new_state, chat_log, chat_log_md, chat_log_md, chat_last_resp, props_json, ''
|
142 |
except Exception as error:
|
143 |
print(error)
|
144 |
|
@@ -158,12 +160,13 @@ def on_click_send_btn(
|
|
158 |
|
159 |
chat_log_md += "\n"
|
160 |
chat_log_md += str(error)
|
161 |
-
return new_state, chat_log, chat_log_md, chat_log_md, None, props_json, chat_input
|
162 |
|
163 |
|
164 |
def clear_history():
|
165 |
return [], ""
|
166 |
|
|
|
167 |
def copy_history(txt):
|
168 |
# print('\n\n copying')
|
169 |
# print(txt)
|
@@ -171,6 +174,33 @@ def copy_history(txt):
|
|
171 |
pass
|
172 |
|
173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
css = """
|
175 |
.table-wrap .cell-wrap input {min-width:80%}
|
176 |
#api-key-textbox textarea {filter:blur(8px); transition: filter 0.25s}
|
@@ -178,7 +208,7 @@ css = """
|
|
178 |
#chat-log-md hr {margin-top: 1rem; margin-bottom: 1rem;}
|
179 |
"""
|
180 |
with gradio.Blocks(title="ChatGPT", css=css) as demo:
|
181 |
-
|
182 |
|
183 |
# https://gradio.app/docs
|
184 |
# https://platform.openai.com/docs/api-reference/chat/create
|
@@ -186,50 +216,198 @@ with gradio.Blocks(title="ChatGPT", css=css) as demo:
|
|
186 |
with gradio.Tab("ChatGPT"):
|
187 |
|
188 |
with gradio.Row():
|
189 |
-
with gradio.
|
190 |
-
gradio.
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
with gradio.Row():
|
194 |
-
with gradio.
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
)
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
|
235 |
with gradio.Row():
|
@@ -293,10 +471,10 @@ with gradio.Blocks(title="ChatGPT", css=css) as demo:
|
|
293 |
chat_send_btn.click(
|
294 |
on_click_send_btn,
|
295 |
inputs=[
|
296 |
-
|
297 |
chat_model, chat_temperature, chat_top_p, chat_choices_num, chat_stream, chat_max_tokens, chat_presence_penalty, chat_frequency_penalty, chat_logit_bias,
|
298 |
],
|
299 |
-
outputs=[
|
300 |
api_name="click-send-btn",
|
301 |
)
|
302 |
|
|
|
44 |
|
45 |
|
46 |
def on_click_send_btn(
|
47 |
+
global_state_json, api_key_text, chat_input_role, chat_input, prompt_table, chat_use_prompt, chat_use_history, chat_log,
|
48 |
chat_model, temperature, top_p, choices_num, stream, max_tokens, presence_penalty, frequency_penalty, logit_bias,
|
49 |
):
|
50 |
|
51 |
+
old_state = json.loads(global_state_json or "{}")
|
52 |
+
|
53 |
print('\n\n\n\n\n')
|
54 |
print(prompt_table)
|
55 |
prompt_table = prompt_table or []
|
|
|
67 |
chat_log_md += '\n---\n'
|
68 |
|
69 |
# if chat_input=='':
|
70 |
+
# return json.dumps(old_state), chat_log, chat_log_md, chat_log_md, None, None, chat_input
|
71 |
|
72 |
print('\n')
|
73 |
print(chat_input)
|
|
|
76 |
try:
|
77 |
logit_bias_json = json.dumps(logit_bias) if logit_bias else None
|
78 |
except:
|
79 |
+
return json.dumps(old_state), chat_log, chat_log_md, chat_log_md, None, None, chat_input
|
80 |
|
81 |
new_state = copy.deepcopy(old_state) or {}
|
82 |
|
|
|
140 |
chat_log.append([the_response_role, the_response])
|
141 |
chat_log_md += f"##### `{the_response_role}`\n\n{the_response}\n\n"
|
142 |
|
143 |
+
return json.dumps(new_state), chat_log, chat_log_md, chat_log_md, chat_last_resp, props_json, ''
|
144 |
except Exception as error:
|
145 |
print(error)
|
146 |
|
|
|
160 |
|
161 |
chat_log_md += "\n"
|
162 |
chat_log_md += str(error)
|
163 |
+
return json.dumps(new_state), chat_log, chat_log_md, chat_log_md, None, props_json, chat_input
|
164 |
|
165 |
|
166 |
def clear_history():
|
167 |
return [], ""
|
168 |
|
169 |
+
|
170 |
def copy_history(txt):
|
171 |
# print('\n\n copying')
|
172 |
# print(txt)
|
|
|
174 |
pass
|
175 |
|
176 |
|
177 |
+
def update_saved_prompt_titles(global_state_json, selected_saved_prompt_title):
|
178 |
+
print('')
|
179 |
+
global_state = json.loads(global_state_json or "{}")
|
180 |
+
print(global_state)
|
181 |
+
print(selected_saved_prompt_title)
|
182 |
+
saved_prompts = global_state.get('saved_prompts') or []
|
183 |
+
print(saved_prompts)
|
184 |
+
the_choices = [(it.get('title') or '[untitled]') for it in saved_prompts]
|
185 |
+
print(the_choices)
|
186 |
+
print('')
|
187 |
+
return gradio.Dropdown.update(choices=the_choices)
|
188 |
+
|
189 |
+
|
190 |
+
def save_prompt(global_state_json, saved_prompts, prompt_title, prompt_table):
|
191 |
+
the_choices = []
|
192 |
+
global_state = json.loads(global_state_json or "{}")
|
193 |
+
saved_prompts = global_state.get('saved_prompts') or []
|
194 |
+
if len(saved_prompts):
|
195 |
+
the_choices = [it.get('title') or '[untitled]' for it in saved_prompts]
|
196 |
+
pass
|
197 |
+
return global_state_json, gradio.Dropdown.update(choices=the_choices, value=prompt_title), prompt_title, prompt_table
|
198 |
+
|
199 |
+
|
200 |
+
def load_saved_prompt(title):
|
201 |
+
pass
|
202 |
+
|
203 |
+
|
204 |
css = """
|
205 |
.table-wrap .cell-wrap input {min-width:80%}
|
206 |
#api-key-textbox textarea {filter:blur(8px); transition: filter 0.25s}
|
|
|
208 |
#chat-log-md hr {margin-top: 1rem; margin-bottom: 1rem;}
|
209 |
"""
|
210 |
with gradio.Blocks(title="ChatGPT", css=css) as demo:
|
211 |
+
global_state_json = gradio.Textbox(visible=False)
|
212 |
|
213 |
# https://gradio.app/docs
|
214 |
# https://platform.openai.com/docs/api-reference/chat/create
|
|
|
216 |
with gradio.Tab("ChatGPT"):
|
217 |
|
218 |
with gradio.Row():
|
219 |
+
with gradio.Box():
|
220 |
+
with gradio.Column(scale=12):
|
221 |
+
with gradio.Row():
|
222 |
+
api_key_text = gradio.Textbox(label="Your API key", elem_id="api-key-textbox")
|
223 |
+
with gradio.Row():
|
224 |
+
with gradio.Column(scale=2):
|
225 |
+
api_key_refresh_btn = gradio.Button("π Load from browser storage")
|
226 |
+
api_key_refresh_btn.click(
|
227 |
+
# get_settings,
|
228 |
+
None,
|
229 |
+
inputs=[],
|
230 |
+
outputs=[api_key_text],
|
231 |
+
api_name="load-settings",
|
232 |
+
_js="""()=>{
|
233 |
+
const the_api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]') ?? '';
|
234 |
+
return the_api_key;
|
235 |
+
}""",
|
236 |
+
)
|
237 |
+
with gradio.Column(scale=2):
|
238 |
+
api_key_save_btn = gradio.Button("πΎ Save to browser storage")
|
239 |
+
api_key_save_btn.click(
|
240 |
+
# save_settings,
|
241 |
+
None,
|
242 |
+
inputs=[api_key_text],
|
243 |
+
outputs=[api_key_text],
|
244 |
+
api_name="save-settings",
|
245 |
+
_js="""(api_key_text)=>{
|
246 |
+
localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
|
247 |
+
return api_key_text;
|
248 |
+
}""",
|
249 |
+
)
|
250 |
+
with gradio.Row():
|
251 |
+
gradio.Markdown("Go to https://platform.openai.com/account/api-keys to get your API key.")
|
252 |
|
253 |
with gradio.Row():
|
254 |
+
with gradio.Box():
|
255 |
+
gradio.Markdown("**Prompt**")
|
256 |
+
with gradio.Column(scale=12):
|
257 |
+
with gradio.Row():
|
258 |
+
prompt_title = gradio.Textbox(label='Prompt title (only for saving)')
|
259 |
+
selected_saved_prompt_title = gradio.Dropdown(label='Select prompt from saved list')
|
260 |
+
with gradio.Row():
|
261 |
+
saved_prompts_refresh_btn = gradio.Button("β»οΈ")
|
262 |
+
saved_prompts_save_btn = gradio.Button("πΎ")
|
263 |
+
saved_prompts_delete_btn = gradio.Button("π")
|
264 |
+
saved_prompts_list_refresh_btn = gradio.Button("π")
|
265 |
+
copy_prompt = gradio.Button("π")
|
266 |
+
paste_prompt = gradio.Button("π")
|
267 |
+
with gradio.Row():
|
268 |
+
gradio.Markdown("""Buttons above: β»οΈ : Load prompts from browser storage (but not updated into the list). πΎ : Save current prompt to browser storage, overwrite the prompt with the same title (but not updated into the list). π : Delete prompt with the same title from browser storage (but not updated into the list). π : Update the selector list. π : Copy current prompt to clipboard. π : Paste prompt from clipboard (need [permission](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText#browser_compatibility)).""")
|
269 |
+
with gradio.Row():
|
270 |
+
prompt_table = gradio.Dataframe(
|
271 |
+
type='array',
|
272 |
+
label='Prompt content', col_count=(2, 'fixed'), max_cols=2,
|
273 |
+
value=DEFAULT_PROMPT, headers=['role', 'content'], interactive=True,
|
274 |
+
)
|
275 |
+
with gradio.Row():
|
276 |
+
gradio.Markdown("The Table above is editable. The content will be added to the beginning of the conversation (if you check 'send with prompt' as `β`). See https://platform.openai.com/docs/guides/chat/introduction .")
|
277 |
+
|
278 |
+
copy_prompt.click(None, inputs=[prompt_title, prompt_table], outputs=[prompt_title, prompt_table], _js="""(prompt_title, prompt_table)=>{
|
279 |
+
try {
|
280 |
+
const txt = JSON.stringify({
|
281 |
+
title: prompt_title,
|
282 |
+
content: prompt_table,
|
283 |
+
}, null, 2);
|
284 |
+
console.log(txt);
|
285 |
+
const promise = navigator?.clipboard?.writeText?.(txt);
|
286 |
+
} catch(error) {console?.log?.(error);};
|
287 |
+
return [prompt_title, prompt_table];
|
288 |
+
}""")
|
289 |
+
paste_prompt.click(None, inputs=[prompt_title, prompt_table], outputs=[prompt_title, prompt_table], _js="""async (prompt_title, prompt_table)=>{
|
290 |
+
console.log("flag1");
|
291 |
+
try {
|
292 |
+
const promise = navigator?.clipboard?.readText?.();
|
293 |
+
console.log(promise);
|
294 |
+
console.log("flag1 p");
|
295 |
+
const result = await promise?.then?.((txt)=>{
|
296 |
+
console.log("flag1 t");
|
297 |
+
const json = JSON.parse(txt);
|
298 |
+
const title = json?.title ?? "";
|
299 |
+
console.log("flag1 0");
|
300 |
+
console.log(title);
|
301 |
+
const content = json?.content ?? {data: [], headers: ['role', 'content']};
|
302 |
+
console.log(content);
|
303 |
+
const result = [title, content];
|
304 |
+
console.log("flag1 1");
|
305 |
+
console.log(result);
|
306 |
+
console.log("flag1 2");
|
307 |
+
return result;
|
308 |
+
});
|
309 |
+
console.log("flag1 3");
|
310 |
+
if (result!=null) {
|
311 |
+
return result;
|
312 |
+
};
|
313 |
+
} catch(error) {console?.log?.(error);};
|
314 |
+
console.log("flag2");
|
315 |
+
try {
|
316 |
+
const promise = navigator?.clipboard?.read?.();
|
317 |
+
console.log(promise);
|
318 |
+
promise?.then?.((data)=>{
|
319 |
+
console.log(data);
|
320 |
+
});
|
321 |
+
} catch(error) {console?.log?.(error);};
|
322 |
+
console.log("flag3");
|
323 |
+
return [prompt_title, prompt_table];
|
324 |
+
}""")
|
325 |
+
saved_prompts_refresh_btn.click(None, inputs=[global_state_json, selected_saved_prompt_title], outputs=[global_state_json, selected_saved_prompt_title], _js="""(global_state_json, saved_prompts)=>{
|
326 |
+
try {
|
327 |
+
if(global_state_json=="") {global_state_json=null;};
|
328 |
+
console.log('global_state_json:\\n', global_state_json);
|
329 |
+
const global_state = JSON.parse(global_state_json??"{ }")??{ };
|
330 |
+
|
331 |
+
const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
|
332 |
+
console.log('saved:\\n', saved);
|
333 |
+
global_state['saved_prompts'] = saved;
|
334 |
+
global_state['selected_saved_prompt_title'] = saved.map(it=>it?.title??"[untitled]")[0];
|
335 |
+
|
336 |
+
const results = [JSON.stringify(global_state), global_state['selected_saved_prompt_title']];
|
337 |
+
console.log(results);
|
338 |
+
return results;
|
339 |
+
} catch(error) {
|
340 |
+
console.log(error);
|
341 |
+
return ["{ }", ""];
|
342 |
+
};
|
343 |
+
}""")
|
344 |
+
|
345 |
+
saved_prompts_list_refresh_btn.click(
|
346 |
+
update_saved_prompt_titles, inputs=[global_state_json, selected_saved_prompt_title], outputs=[selected_saved_prompt_title],
|
347 |
)
|
348 |
|
349 |
+
selected_saved_prompt_title.change(None, inputs=[global_state_json, selected_saved_prompt_title], outputs=[global_state_json, prompt_title, prompt_table], _js="""(global_state_json, selected_saved_prompt_title)=>{
|
350 |
+
if(global_state_json=="") {global_state_json=null;};
|
351 |
+
const global_state = JSON.parse(global_state_json??"{ }")??{ };
|
352 |
+
const found = (global_state?.['saved_prompts']??[]).find(it=>it?.title==selected_saved_prompt_title);
|
353 |
+
return [JSON.stringify(global_state), found?.title??'', found?.content??{data:[], headers:["role", "content"]}];
|
354 |
+
}""")
|
355 |
+
|
356 |
+
saved_prompts_delete_btn.click(None, inputs=[global_state_json, selected_saved_prompt_title, prompt_title, prompt_table], outputs=[global_state_json, selected_saved_prompt_title, prompt_title, prompt_table], _js="""(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
|
357 |
+
if(prompt_title==""||!prompt_title){
|
358 |
+
return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
|
359 |
+
};
|
360 |
+
console.log('global_state_json:\\n', global_state_json);
|
361 |
+
|
362 |
+
if(global_state_json=="") {global_state_json=null;};
|
363 |
+
const global_state = JSON.parse(global_state_json??"{ }")??{ };
|
364 |
+
console.log(global_state);
|
365 |
+
|
366 |
+
const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
|
367 |
+
console.log('saved:\\n', saved);
|
368 |
+
|
369 |
+
|
370 |
+
global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];
|
371 |
+
|
372 |
+
global_state['selected_saved_prompt_title'] = "";
|
373 |
+
|
374 |
+
console.log(global_state);
|
375 |
+
|
376 |
+
localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));
|
377 |
+
|
378 |
+
return [JSON.stringify(global_state), "", "", {data: [], headers: ['role', 'content']}];
|
379 |
+
}""")
|
380 |
+
|
381 |
+
saved_prompts_save_btn.click(None, inputs=[global_state_json, selected_saved_prompt_title, prompt_title, prompt_table], outputs=[global_state_json, selected_saved_prompt_title, prompt_title, prompt_table], _js="""(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
|
382 |
+
if(prompt_title==""||!prompt_title){
|
383 |
+
return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
|
384 |
+
};
|
385 |
+
console.log('global_state_json:\\n', global_state_json);
|
386 |
+
|
387 |
+
if(global_state_json=="") {global_state_json=null;};
|
388 |
+
const global_state = JSON.parse(global_state_json??"{ }")??{ };
|
389 |
+
console.log(global_state);
|
390 |
+
|
391 |
+
const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
|
392 |
+
console.log('saved:\\n', saved);
|
393 |
+
|
394 |
+
|
395 |
+
const new_prompt_obj = {
|
396 |
+
title: prompt_title, content: prompt_table,
|
397 |
+
};
|
398 |
+
|
399 |
+
global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];
|
400 |
+
|
401 |
+
global_state['saved_prompts'].unshift(new_prompt_obj);
|
402 |
+
|
403 |
+
global_state['selected_saved_prompt_title'] = prompt_title;
|
404 |
+
|
405 |
+
console.log(global_state);
|
406 |
+
|
407 |
+
localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));
|
408 |
+
|
409 |
+
return [JSON.stringify(global_state), prompt_title, prompt_title, prompt_table];
|
410 |
+
}""")
|
411 |
|
412 |
|
413 |
with gradio.Row():
|
|
|
471 |
chat_send_btn.click(
|
472 |
on_click_send_btn,
|
473 |
inputs=[
|
474 |
+
global_state_json, api_key_text, chat_input_role, chat_input, prompt_table, chat_use_prompt, chat_use_history, chat_log,
|
475 |
chat_model, chat_temperature, chat_top_p, chat_choices_num, chat_stream, chat_max_tokens, chat_presence_penalty, chat_frequency_penalty, chat_logit_bias,
|
476 |
],
|
477 |
+
outputs=[global_state_json, chat_log, chat_log_box, real_md_box, chat_last_resp, chat_last_req, chat_input],
|
478 |
api_name="click-send-btn",
|
479 |
)
|
480 |
|