hugforziio commited on
Commit
9d28b0d
·
1 Parent(s): 90fe6c7

Create app_js.py

Browse files
Files changed (1) hide show
  1. app_js.py +196 -0
app_js.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ saved_prompts_refresh_btn__click_js = """(global_state_json, saved_prompts)=>{
3
+ try {
4
+ if(global_state_json=="") {global_state_json=null;};
5
+ console.log('global_state_json:\\n', global_state_json);
6
+ const global_state = JSON.parse(global_state_json??"{ }")??{ };
7
+
8
+ const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
9
+ console.log('saved:\\n', saved);
10
+ global_state['saved_prompts'] = saved;
11
+ global_state['selected_saved_prompt_title'] = saved.map(it=>it?.title??"[untitled]")[0];
12
+
13
+ const results = [JSON.stringify(global_state), global_state['selected_saved_prompt_title']];
14
+ console.log(results);
15
+ return results;
16
+ } catch(error) {
17
+ console.log(error);
18
+ return ["{ }", ""];
19
+ };
20
+ }"""
21
+
22
+
23
+ selected_saved_prompt_title__change_js = """(global_state_json, selected_saved_prompt_title)=>{
24
+ if(global_state_json=="") {global_state_json=null;};
25
+ const global_state = JSON.parse(global_state_json??"{ }")??{ };
26
+ const found = (global_state?.['saved_prompts']??[]).find(it=>it?.title==selected_saved_prompt_title);
27
+ return [JSON.stringify(global_state), found?.title??'', found?.content??{data:[], headers:["role", "content"]}];
28
+ }"""
29
+
30
+
31
+ saved_prompts_delete_btn__click_js = """(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
32
+ if(prompt_title==""||!prompt_title){
33
+ return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
34
+ };
35
+ console.log('global_state_json:\\n', global_state_json);
36
+
37
+ if(global_state_json=="") {global_state_json=null;};
38
+ const global_state = JSON.parse(global_state_json??"{ }")??{ };
39
+ console.log(global_state);
40
+
41
+ const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
42
+ console.log('saved:\\n', saved);
43
+
44
+
45
+ global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];
46
+
47
+ global_state['selected_saved_prompt_title'] = "";
48
+
49
+ console.log(global_state);
50
+
51
+ localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));
52
+
53
+ return [JSON.stringify(global_state), "", "", {data: [], headers: ['role', 'content']}];
54
+ }"""
55
+
56
+
57
+ saved_prompts_save_btn__click_js = """(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
58
+ if(prompt_title==""||!prompt_title){
59
+ return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
60
+ };
61
+ console.log('global_state_json:\\n', global_state_json);
62
+
63
+ if(global_state_json=="") {global_state_json=null;};
64
+ const global_state = JSON.parse(global_state_json??"{ }")??{ };
65
+ console.log(global_state);
66
+
67
+ const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
68
+ console.log('saved:\\n', saved);
69
+
70
+
71
+ const new_prompt_obj = {
72
+ title: prompt_title, content: prompt_table,
73
+ };
74
+
75
+ global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];
76
+
77
+ global_state['saved_prompts'].unshift(new_prompt_obj);
78
+
79
+ global_state['selected_saved_prompt_title'] = prompt_title;
80
+
81
+ console.log(global_state);
82
+
83
+ localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));
84
+
85
+ return [JSON.stringify(global_state), prompt_title, prompt_title, prompt_table];
86
+ }"""
87
+
88
+
89
+ copy_prompt__click_js = """(prompt_title, prompt_table)=>{
90
+ try {
91
+ const txt = JSON.stringify({
92
+ title: prompt_title,
93
+ content: prompt_table,
94
+ }, null, 2);
95
+ console.log(txt);
96
+ const promise = navigator?.clipboard?.writeText?.(txt);
97
+ } catch(error) {console?.log?.(error);};
98
+ return [prompt_title, prompt_table];
99
+ }"""
100
+
101
+
102
+ paste_prompt__click_js = """async (prompt_title, prompt_table)=>{
103
+ console.log("flag1");
104
+ try {
105
+ const promise = navigator?.clipboard?.readText?.();
106
+ console.log(promise);
107
+ console.log("flag1 p");
108
+ const result = await promise?.then?.((txt)=>{
109
+ console.log("flag1 t");
110
+ const json = JSON.parse(txt);
111
+ const title = json?.title ?? "";
112
+ console.log("flag1 0");
113
+ console.log(title);
114
+ const content = json?.content ?? {data: [], headers: ['role', 'content']};
115
+ console.log(content);
116
+ const result = [title, content];
117
+ console.log("flag1 1");
118
+ console.log(result);
119
+ console.log("flag1 2");
120
+ return result;
121
+ });
122
+ console.log("flag1 3");
123
+ if (result!=null) {
124
+ return result;
125
+ };
126
+ } catch(error) {console?.log?.(error);};
127
+ console.log("flag2");
128
+ try {
129
+ const promise = navigator?.clipboard?.read?.();
130
+ console.log(promise);
131
+ promise?.then?.((data)=>{
132
+ console.log(data);
133
+ });
134
+ } catch(error) {console?.log?.(error);};
135
+ console.log("flag3");
136
+ return [prompt_title, prompt_table];
137
+ }"""
138
+
139
+
140
+ chat_copy_history_btn__click_js = """(txt)=>{
141
+ console.log(txt);
142
+ try {let promise = navigator?.clipboard?.writeText?.(txt);}
143
+ catch(error) {console?.log?.(error);};
144
+ }"""
145
+
146
+
147
+ chat_copy_history_md_btn__click_js = """(txt)=>{
148
+ console.log(txt);
149
+ try {let promise = navigator?.clipboard?.writeText?.(txt);}
150
+ catch(error) {console?.log?.(error);};
151
+ }"""
152
+
153
+
154
+ # api_key_refresh_btn__click_js = """()=>{
155
+ # const the_api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]') ?? '';
156
+ # return the_api_key;
157
+ # }"""
158
+
159
+
160
+ # api_key_save_btn__click_js = """(api_key_text)=>{
161
+ # localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
162
+ # return api_key_text;
163
+ # }"""
164
+
165
+
166
+ api_key_refresh_btn__click_js = """()=>{
167
+ const the_api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]') ?? '';
168
+ return the_api_key;
169
+ }"""
170
+
171
+
172
+ api_key_save_btn__click_js = """(api_key_text)=>{
173
+ localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
174
+ return api_key_text;
175
+ }"""
176
+
177
+
178
+ api_key__get_from_browser = """()=>{
179
+ const api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key]') ?? '';
180
+ const token = localStorage?.getItem?.('[gradio][chat-gpt-ui][token]') ?? '';
181
+ return [api_key, token];
182
+ }"""
183
+
184
+ api_key__save_to_browser = """(api_key, token)=>{
185
+ localStorage?.setItem?.('[gradio][chat-gpt-ui][api_key]', api_key);
186
+ token = localStorage?.getItem?.('[gradio][chat-gpt-ui][token]') ?? token ?? '';
187
+ if (!token?.length) {
188
+ const temp_url = URL.createObjectURL(new Blob());
189
+ const uuid = temp_url.toString();
190
+ URL.revokeObjectURL(temp_url);
191
+ token = uuid.substr(uuid.lastIndexOf("/") + 1);
192
+ };
193
+ localStorage.setItem('[gradio][chat-gpt-ui][token]', token);
194
+ return [api_key, token];
195
+ }"""
196
+