Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,41 +11,128 @@ function() {
|
|
11 |
globalThis.getStorage = (key, value)=>{
|
12 |
return JSON.parse(localStorage.getItem(key));
|
13 |
}
|
14 |
-
const local_data =
|
15 |
console.log(local_data);
|
16 |
return local_data;
|
17 |
}
|
18 |
"""
|
19 |
|
20 |
-
def add_pingpong(ld, ping
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
else:
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
with gr.Blocks() as block:
|
|
|
32 |
local_data = gr.JSON(
|
33 |
{},
|
34 |
-
label="Local Storage"
|
|
|
35 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
None, local_data, None,
|
47 |
_js="(v)=>{ setStorage('local_data',v) }"
|
48 |
-
|
49 |
|
50 |
block.load(
|
51 |
None,
|
@@ -54,4 +141,10 @@ with gr.Blocks() as block:
|
|
54 |
_js=get_local_storage,
|
55 |
)
|
56 |
|
57 |
-
block.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
globalThis.getStorage = (key, value)=>{
|
12 |
return JSON.parse(localStorage.getItem(key));
|
13 |
}
|
14 |
+
const local_data = getStorage('local_data');
|
15 |
console.log(local_data);
|
16 |
return local_data;
|
17 |
}
|
18 |
"""
|
19 |
|
20 |
+
def add_pingpong(idx, ld, ping):
|
21 |
+
res = [
|
22 |
+
GradioAlpacaChatPPManager.from_json(json.dumps(ppm))
|
23 |
+
for ppm in ld
|
24 |
+
]
|
25 |
+
|
26 |
+
ppm = res[idx]
|
27 |
+
ppm.add_pingpong(PingPong(ping, "dang!!!!!!!"))
|
28 |
+
return ppm.build_uis(), str(res)
|
29 |
+
|
30 |
+
def set_chatbot(btn, ld):
|
31 |
+
choice = 0
|
32 |
+
if btn == "1st":
|
33 |
+
choice = 0
|
34 |
+
elif btn == "2nd":
|
35 |
+
choice = 1
|
36 |
+
elif btn == "3rd":
|
37 |
+
choice = 2
|
38 |
+
elif btn == "4th":
|
39 |
+
choice = 3
|
40 |
+
elif btn == "5th":
|
41 |
+
choice = 4
|
42 |
+
|
43 |
+
res = [
|
44 |
+
GradioAlpacaChatPPManager.from_json(json.dumps(ppm_str))
|
45 |
+
for ppm_str in ld
|
46 |
+
]
|
47 |
+
return res[choice].build_uis(), choice
|
48 |
+
|
49 |
+
def initialize(local_data):
|
50 |
+
if local_data is None:
|
51 |
+
list_conv = [
|
52 |
+
GradioAlpacaChatPPManager()
|
53 |
+
for _ in range(5)
|
54 |
+
]
|
55 |
+
local_data = str(list_conv)
|
56 |
+
|
57 |
+
return local_data
|
58 |
+
|
59 |
+
def initialize_t(ld):
|
60 |
+
print(ld)
|
61 |
+
|
62 |
+
if ld == {} or id is None:
|
63 |
+
list_conv = [
|
64 |
+
GradioAlpacaChatPPManager()
|
65 |
+
for _ in range(5)
|
66 |
+
]
|
67 |
+
local_data = str(list_conv)
|
68 |
else:
|
69 |
+
list_conv = [
|
70 |
+
GradioAlpacaChatPPManager.from_json(json.dumps(ppm))
|
71 |
+
for ppm in ld
|
72 |
+
]
|
73 |
+
local_data = str(list_conv)
|
74 |
+
|
75 |
+
return list_conv[0].build_uis(), local_data
|
76 |
|
77 |
with gr.Blocks() as block:
|
78 |
+
idx = gr.State(0)
|
79 |
local_data = gr.JSON(
|
80 |
{},
|
81 |
+
label="Local Storage",
|
82 |
+
visible=False
|
83 |
)
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column(scale=1, min_width=100):
|
87 |
+
first = gr.Button("1st")
|
88 |
+
second = gr.Button("2nd")
|
89 |
+
third = gr.Button("3rd")
|
90 |
+
fourth = gr.Button("4th")
|
91 |
+
fifth = gr.Button("5th")
|
92 |
+
with gr.Column(scale=4):
|
93 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
94 |
+
instruction_txtbox = gr.Textbox(placeholder="What do you want to say to AI?", label="Instruction")
|
95 |
+
|
96 |
+
first.click(
|
97 |
+
set_chatbot,
|
98 |
+
[first, local_data],
|
99 |
+
[chatbot, idx]
|
100 |
+
)
|
101 |
+
|
102 |
+
second.click(
|
103 |
+
set_chatbot,
|
104 |
+
[second, local_data],
|
105 |
+
[chatbot, idx]
|
106 |
+
)
|
107 |
+
|
108 |
+
third.click(
|
109 |
+
set_chatbot,
|
110 |
+
[third, local_data],
|
111 |
+
[chatbot, idx]
|
112 |
+
)
|
113 |
+
|
114 |
+
fourth.click(
|
115 |
+
set_chatbot,
|
116 |
+
[fourth, local_data],
|
117 |
+
[chatbot, idx]
|
118 |
+
)
|
119 |
|
120 |
+
fifth.click(
|
121 |
+
set_chatbot,
|
122 |
+
[fifth, local_data],
|
123 |
+
[chatbot, idx]
|
124 |
+
)
|
125 |
|
126 |
+
instruction_txtbox.submit(
|
127 |
+
add_pingpong,
|
128 |
+
[idx, local_data, instruction_txtbox],
|
129 |
+
[chatbot, local_data]
|
130 |
+
)
|
131 |
+
|
132 |
+
local_data.change(
|
133 |
None, local_data, None,
|
134 |
_js="(v)=>{ setStorage('local_data',v) }"
|
135 |
+
)
|
136 |
|
137 |
block.load(
|
138 |
None,
|
|
|
141 |
_js=get_local_storage,
|
142 |
)
|
143 |
|
144 |
+
block.load(
|
145 |
+
initialize_t,
|
146 |
+
local_data,
|
147 |
+
[chatbot, local_data]
|
148 |
+
)
|
149 |
+
|
150 |
+
block.queue().launch(debug=True)
|