File size: 3,850 Bytes
b24805e
6b8a805
b24805e
 
6b8a805
76c51c1
398e0e0
 
 
 
74bda91
 
 
d8ef9f9
398e0e0
 
76c51c1
3c6ebcd
029ff92
1e6519e
6c33d2e
 
 
 
 
 
 
2e0a307
 
 
d8ef9f9
 
2e0a307
 
 
 
d8ef9f9
 
 
398e0e0
 
 
 
d8ef9f9
 
398e0e0
 
 
d8ef9f9
335a2de
 
 
 
 
76c51c1
 
6b8a805
d4b2d90
 
 
 
 
 
 
061316d
d4b2d90
 
 
 
 
 
 
061316d
 
 
 
 
 
 
 
 
 
 
 
d4b2d90
061316d
d4b2d90
 
6b8a805
f8567da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398e0e0
f8567da
061316d
f8567da
 
398e0e0
 
029ff92
 
335a2de
 
 
 
 
 
029ff92
398e0e0
f8567da
1e6519e
398e0e0
1e6519e
 
f8567da
061316d
 
 
 
 
 
 
c542f6a
 
 
 
 
 
 
 
 
b24805e
 
 
 
061316d
b24805e
 
6b8a805
b6e89e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import gradio as gr
from pingpong import PingPong
from pingpong.gradio import GradioAlpacaChatPPManager

STYLE = """
#container-col {
  
}

.custom-btn {
    border: none !important;
    background: none !important;
    box-shadow: none !important;
    display: block !important;
    text-align: left !important;
}
.custom-btn:hover {
    background: rgb(243 244 246) !important;
}
#prompt-txt > label > span {
    display: none !important;
}
#prompt-txt > label > textarea {
    border: transparent;
    box-shadow: none;
}
#chatbot {
    height: 800px; 
    overflow: auto;
    box-shadow: none !important;
    border: none !important;
}
#chatbot > .wrap {
    max-height: 780px;
}
#left-pane {
    background-color: #f9fafb;
    border-radius: 15px;
    padding: 10px;
}

#left-top {
    padding-left: 10px;
    padding-right: 10px;
    text-align: center;
    font-weight: bold;
    font-size: large;    
}

#chat-history-accordion {
    background: transparent;
    border: 0.8px !important;  
}
"""

get_local_storage = """
function() {
  globalThis.setStorage = (key, value)=>{
    localStorage.setItem(key, JSON.stringify(value));
  }
  globalThis.getStorage = (key, value)=>{
    return JSON.parse(localStorage.getItem(key));
  }
  var local_data = getStorage('local_data');
  const history = [];
  if(local_data) {
    console.log(local_data);
    local_data[0].pingpongs.forEach(element => 
      history.push([element.ping, element.pong])
    );
  }
  else {
    local_data = [
      {'ctx': '', 'pingpongs':[]},
      {'ctx': '', 'pingpongs':[]},
      {'ctx': '', 'pingpongs':[]},
      {'ctx': '', 'pingpongs':[]},
      {'ctx': '', 'pingpongs':[]},
    ];
    local_data[0].pingpongs.forEach(element => 
      history.push([element.ping, element.pong])
    );    
  }
  console.log(history);
  return [history, local_data];
}
"""

def add_pingpong(idx, ld, ping):
  res = [
      GradioAlpacaChatPPManager.from_json(json.dumps(ppm))
      for ppm in ld
  ]

  ppm = res[idx]
  ppm.add_pingpong(PingPong(ping, "dang!!!!!!!"))
  return ppm.build_uis(), str(res)

def set_chatbot(btn, ld):
  choice = 0
  if btn == "1st":
    choice = 0
  elif btn == "2nd":
    choice = 1
  elif btn == "3rd":
    choice = 2
  elif btn == "4th":
    choice = 3
  elif btn == "5th":
    choice = 4

  res = [
      GradioAlpacaChatPPManager.from_json(json.dumps(ppm_str))
      for ppm_str in ld
  ]
  return res[choice].build_uis(), choice

with gr.Blocks(css=STYLE, elem_id='container-col') as block:
  idx = gr.State(0)
  local_data = gr.JSON({},visible=False)
  
  with gr.Row():
    with gr.Column(scale=1, min_width=180):
      gr.Markdown("GradioChat", elem_id="left-top")
        
      with gr.Column(elem_id="left-pane"):
        with gr.Accordion("Histories", elem_id="chat-history-accordion"):
          first = gr.Button("1st", elem_classes=["custom-btn"])
          second = gr.Button("2nd", elem_classes=["custom-btn"])
          third = gr.Button("3rd", elem_classes=["custom-btn"])
          fourth = gr.Button("4th", elem_classes=["custom-btn"])
          fifth = gr.Button("5th", elem_classes=["custom-btn"])
          
    with gr.Column(scale=8):
      chatbot = gr.Chatbot(elem_id='chatbot')
      instruction_txtbox = gr.Textbox(
          placeholder="Ask anything", label="",
          elem_id="prompt-txt"
      )

  btns = [first, second, third, fourth, fifth]
  for btn in btns:
    btn.click(
      set_chatbot,
      [btn, local_data],
      [chatbot, idx]
    )
  
  instruction_txtbox.submit(
    add_pingpong,
    [idx, local_data, instruction_txtbox],
    [chatbot, local_data]
  ).then(
    None, local_data, None, 
    _js="(v)=>{ setStorage('local_data',v) }"
  )

  block.load(
      None,
      inputs=None,
      outputs=[chatbot, local_data],
      _js=get_local_storage,
  )

block.launch(debug=True)