Spaces:
Runtime error
Runtime error
maybe complete
Browse files- app.py +106 -136
- flagged/log.csv +4 -0
app.py
CHANGED
@@ -1,144 +1,114 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
warnings.filterwarnings("ignore")
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
time.sleep(1)
|
50 |
|
51 |
-
yon = "no"
|
52 |
-
else:
|
53 |
-
yon = input(
|
54 |
-
f"μ΄μ λν μ 보λ₯Ό κ·Έλλ‘ μ μ§ν κΉμ? (yes : μ μ§, no : μλ‘ μμ±) :"
|
55 |
-
)
|
56 |
-
|
57 |
-
if yon == "no":
|
58 |
-
info = "μΌμ λν "
|
59 |
-
|
60 |
-
topic = input("λν μ£Όμ λ₯Ό μ ν΄μ£ΌμΈμ (e.g. μ¬κ° μν, μΌκ³Ό μ§μ
, κ°μΈ λ° κ΄κ³, etc...) :")
|
61 |
-
if topic == "":
|
62 |
-
topic = random.choice(['μ¬κ° μν', 'μμ¬/κ΅μ‘', 'λ―Έμ©κ³Ό 건κ°', 'μμλ£', 'μκ±°λ(μΌν)', 'μΌκ³Ό μ§μ
', 'μ£Όκ±°μ μν', 'κ°μΈ λ° κ΄κ³', 'νμ¬'])
|
63 |
-
print(topic)
|
64 |
-
info += topic + "<sep>"
|
65 |
-
|
66 |
-
def ask_info(who, ment):
|
67 |
-
print(ment)
|
68 |
-
text = who + ":"
|
69 |
-
addr = input("μ΄λ μ¬μΈμ? (e.g. μμΈνΉλ³μ, μ μ£Όλ, etc...) :").strip()
|
70 |
-
if addr == "":
|
71 |
-
addr = random.choice(['μμΈνΉλ³μ', 'κ²½κΈ°λ', 'λΆμ°κ΄μμ', 'λμ κ΄μμ', 'κ΄μ£Όκ΄μμ', 'μΈμ°κ΄μμ', 'κ²½μλ¨λ', 'μΈμ²κ΄μμ', 'μΆ©μ²λΆλ', 'μ μ£Όλ', 'κ°μλ', 'μΆ©μ²λ¨λ', 'μ λΌλΆλ', 'λꡬκ΄μμ', 'μ λΌλ¨λ', 'κ²½μλΆλ', 'μΈμ’
νΉλ³μμΉμ', 'κΈ°ν'])
|
72 |
-
print(addr)
|
73 |
-
text += addr + " "
|
74 |
-
|
75 |
-
age = input("λμ΄κ°? (e.g. 20λ, 70λ μ΄μ, etc...) :").strip()
|
76 |
-
if age == "":
|
77 |
-
age = random.choice(['20λ', '30λ', '50λ', '20λ λ―Έλ§', '60λ', '40λ', '70λ μ΄μ'])
|
78 |
-
print(age)
|
79 |
-
text += age + " "
|
80 |
-
|
81 |
-
sex = input("μ±λ³μ΄? (e.g. λ¨μ±, μ¬μ±, etc... (?)) :").strip()
|
82 |
-
if sex == "":
|
83 |
-
sex = random.choice(['λ¨μ±', 'μ¬μ±'])
|
84 |
-
print(sex)
|
85 |
-
text += sex + "<sep>"
|
86 |
-
return text
|
87 |
-
|
88 |
-
info += ask_info(who="P01", ment=f"\nλΉμ μ λν΄ μλ €μ£ΌμΈμ.\n")
|
89 |
-
info += ask_info(who="P02", ment=f"\nμ±λ΄μ λν΄ μλ €μ£ΌμΈμ.\n")
|
90 |
-
|
91 |
-
pp = info.replace('<sep>', '\n')
|
92 |
-
print(
|
93 |
-
f"\n----------------\n"
|
94 |
-
f"<μ
λ ₯ μ 보 νμΈ> (P01 : λΉμ , P02 : μ±λ΄)\n"
|
95 |
-
f"{pp}"
|
96 |
-
f"----------------\n"
|
97 |
-
f"λνλ₯Ό μ’
λ£νκ³ μΆμΌλ©΄ μΈμ λ μ§ 'end' λΌκ³ λ§ν΄μ£ΌμΈμ~\n"
|
98 |
-
)
|
99 |
-
talk = []
|
100 |
-
switch = True
|
101 |
-
switch2 = True
|
102 |
-
while True:
|
103 |
-
inp = "P01<sos>"
|
104 |
-
myinp = input("λΉμ : ")
|
105 |
-
if myinp == "end":
|
106 |
-
print("λν μ’
λ£!")
|
107 |
-
break
|
108 |
-
inp += myinp + "<eos>"
|
109 |
-
talk.append(inp)
|
110 |
-
talk.append("P02<sos>")
|
111 |
-
|
112 |
-
while True:
|
113 |
-
now_inp = info + "".join(talk)
|
114 |
-
inpu = tokenizer(now_inp, max_length=1024, truncation='longest_first', return_tensors='pt')
|
115 |
-
seq_len = inpu.input_ids.size(1)
|
116 |
-
if seq_len > 512 * 0.8 and switch:
|
117 |
-
print(
|
118 |
-
f"<μ£Όμ> νμ¬ λν κΈΈμ΄κ° 곧 μ΅λ κΈΈμ΄μ λλ¬ν©λλ€. ({seq_len} / 512)"
|
119 |
-
)
|
120 |
-
switch = False
|
121 |
-
|
122 |
-
if seq_len >= 512 and switch2:
|
123 |
-
print("<μ£Όμ> λν κΈΈμ΄κ° λ무 κΈΈμ΄μ‘κΈ° λλ¬Έμ, μ΄ν λνλ 맨 μμ λ°νλ₯Ό μ‘°κΈμ© μ§μ°λ©΄μ μ§νλ©λλ€.")
|
124 |
-
talk = talk[1:]
|
125 |
-
switch2 = False
|
126 |
-
else:
|
127 |
-
break
|
128 |
-
|
129 |
-
out = model.generate(
|
130 |
-
inputs=inpu.input_ids.cuda(),
|
131 |
-
attention_mask=inpu.attention_mask.cuda(),
|
132 |
-
max_length=512,
|
133 |
-
do_sample=True,
|
134 |
-
pad_token_id=tokenizer.pad_token_id,
|
135 |
-
eos_token_id=tokenizer.encode('<eos>')[0]
|
136 |
-
)
|
137 |
-
output = tokenizer.batch_decode(out)
|
138 |
-
print("μ±λ΄ : " + output[0][len(now_inp):-5])
|
139 |
-
talk[-1] += output[0][len(now_inp):]
|
140 |
-
|
141 |
-
again = input(f"λ€λ₯Έ λνλ₯Ό μμν κΉμ? (yes : μλ‘μ΄ μμ, no : μ’
λ£) :")
|
142 |
-
if again == "no":
|
143 |
-
break
|
144 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import warnings
|
4 |
+
|
5 |
+
|
6 |
+
class Chatbot():
|
7 |
+
def __init__(self):
|
8 |
+
self.tokenizer = AutoTokenizer.from_pretrained('kakaobrain/kogpt', revision='KoGPT6B-ryan1.5b')
|
9 |
+
special_tokens_dict = {'additional_special_tokens': ['<sep>', '<eos>', '<sos>', '#@μ΄λ¦#', '#@κ³μ #', '#@μ μ#', '#@μ λ²#', '#@κΈμ΅#', '#@λ²νΈ#', '#@μ£Όμ#', '#@μμ#', '#@κΈ°ν#']}
|
10 |
+
num_added_toks = self.tokenizer.add_special_tokens(special_tokens_dict)
|
11 |
+
|
12 |
+
self.model = AutoModelForCausalLM.from_pretrained("/workspace/test_trainer/checkpoint-10000")
|
13 |
+
self.model.resize_token_embeddings(len(self.tokenizer))
|
14 |
+
self.model = self.model.cuda()
|
15 |
+
|
16 |
+
self.info = None
|
17 |
+
self.talk = []
|
18 |
+
|
19 |
+
def initialize(self, topic, bot_addr, bot_age, bot_sex, my_addr, my_age, my_sex):
|
20 |
+
def encode(age):
|
21 |
+
if age < 20:
|
22 |
+
age = "20λ λ―Έλ§"
|
23 |
+
elif age >= 70:
|
24 |
+
age = "70λ μ΄μ"
|
25 |
+
else:
|
26 |
+
age = str(age // 10 * 10) + "λ"
|
27 |
+
return age
|
28 |
+
bot_age = encode(bot_age)
|
29 |
+
my_age = encode(my_age)
|
30 |
+
self.info = f"μΌμ λν {topic}<sep>P01:{my_addr} {my_age} {my_sex}<sep>P02:{bot_addr} {bot_age} {bot_sex}<sep>"
|
31 |
+
return self.info_check()
|
32 |
+
|
33 |
+
def info_check(self):
|
34 |
+
return self.info.replace('<sep>', '\n').replace('P01', 'λΉμ ').replace('P02', 'μ±λ΄')
|
35 |
+
|
36 |
+
def reset_talk(self):
|
37 |
+
self.talk = []
|
38 |
+
|
39 |
+
def test(self, myinp):
|
40 |
+
state = None
|
41 |
+
inp = "P01<sos>" + myinp + "<eos>"
|
42 |
+
self.talk.append(inp)
|
43 |
+
self.talk.append("P02<sos>")
|
44 |
|
45 |
+
while True:
|
46 |
+
now_inp = self.info + "".join(self.talk)
|
47 |
+
inputs = self.tokenizer(now_inp, max_length=1024, truncation='longest_first', return_tensors='pt')
|
48 |
+
seq_len = inputs.input_ids.size(1)
|
49 |
+
if seq_len > 512 * 0.8:
|
50 |
+
state = f"<μ£Όμ> νμ¬ λν κΈΈμ΄κ° 곧 μ΅λ κΈΈμ΄μ λλ¬ν©λλ€. ({seq_len} / 512)"
|
51 |
+
|
52 |
+
if seq_len >= 512:
|
53 |
+
state = "<μ£Όμ> λν κΈΈμ΄κ° λ무 κΈΈμ΄μ‘κΈ° λλ¬Έμ, μ΄ν λνλ 맨 μμ λ°νλ₯Ό μ‘°κΈμ© μ§μ°λ©΄μ μ§νλ©λλ€."
|
54 |
+
talk = talk[1:]
|
55 |
+
else:
|
56 |
+
break
|
57 |
+
|
58 |
+
out = self.model.generate(
|
59 |
+
inputs=inputs.input_ids.cuda(),
|
60 |
+
attention_mask=inputs.attention_mask.cuda(),
|
61 |
+
max_length=512,
|
62 |
+
do_sample=True,
|
63 |
+
pad_token_id=self.tokenizer.pad_token_id,
|
64 |
+
eos_token_id=self.tokenizer.encode('<eos>')[0]
|
65 |
+
)
|
66 |
+
out = self.tokenizer.batch_decode(out)
|
67 |
+
real_out = out[0][len(now_inp):-5]
|
68 |
+
self.talk[-1] += out[0][len(now_inp):]
|
69 |
+
return [(self.talk[i][8:-5], self.talk[i+1][8:-5]) for i in range(0, len(self.talk)-1, 2)]
|
70 |
|
71 |
|
72 |
+
if __name__ == "__main__":
|
73 |
warnings.filterwarnings("ignore")
|
74 |
|
75 |
+
chatbot = Chatbot()
|
76 |
+
demo = gr.Blocks()
|
77 |
+
|
78 |
+
with demo:
|
79 |
+
gr.Markdown("# <center>MINDs Lab Brain's Fast Neural Chit-Chatbot</center>")
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column():
|
82 |
+
topic = gr.Radio(label="Topic", choices=['μ¬κ° μν', 'μμ¬/κ΅μ‘', 'λ―Έμ©κ³Ό 건κ°', 'μμλ£', 'μκ±°λ(μΌν)', 'μΌκ³Ό μ§μ
', 'μ£Όκ±°μ μν', 'κ°μΈ λ° κ΄κ³', 'νμ¬'])
|
83 |
+
with gr.Column():
|
84 |
+
gr.Markdown(f"Bot's persona")
|
85 |
+
bot_addr = gr.Dropdown(label="μ§μ", choices=['μμΈνΉλ³μ', 'κ²½κΈ°λ', 'λΆμ°κ΄μμ', 'λμ κ΄μμ', 'κ΄μ£Όκ΄μμ', 'μΈμ°κ΄μμ', 'κ²½μλ¨λ', 'μΈμ²κ΄μμ', 'μΆ©μ²λΆλ', 'μ μ£Όλ', 'κ°μλ', 'μΆ©μ²λ¨λ', 'μ λΌλΆλ', 'λꡬκ΄μμ', 'μ λΌλ¨λ', 'κ²½μλΆλ', 'μΈμ’
νΉλ³μμΉμ', 'κΈ°ν'])
|
86 |
+
bot_age = gr.Slider(label="λμ΄", minimum=10, maximum=80, value=45, step=1)
|
87 |
+
bot_sex = gr.Radio(label="μ±λ³", choices=["λ¨μ±", "μ¬μ±"])
|
88 |
+
with gr.Column():
|
89 |
+
gr.Markdown(f"Your persona")
|
90 |
+
my_addr = gr.Dropdown(label="μ§μ", choices=['μμΈνΉλ³μ', 'κ²½κΈ°λ', 'λΆμ°κ΄μμ', 'λμ κ΄μμ', 'κ΄μ£Όκ΄μμ', 'μΈμ°κ΄μμ', 'κ²½μλ¨λ', 'μΈμ²κ΄μμ', 'μΆ©μ²λΆλ', 'μ μ£Όλ', 'κ°μλ', 'μΆ©μ²λ¨λ', 'μ λΌλΆλ', 'λꡬκ΄μμ', 'μ λΌλ¨λ', 'κ²½μλΆλ', 'μΈμ’
νΉλ³μμΉμ', 'κΈ°ν'])
|
91 |
+
my_age = gr.Slider(label="λμ΄", minimum=10, maximum=80, value=45, step=1)
|
92 |
+
my_sex = gr.Radio(label="μ±λ³", choices=["λ¨μ±", "μ¬μ±"])
|
93 |
+
with gr.Row():
|
94 |
+
btn = gr.Button(label="μ μ©")
|
95 |
+
state = gr.Textbox(label="μν")
|
96 |
+
btn.click(
|
97 |
+
fn=chatbot.initialize,
|
98 |
+
inputs=[topic, bot_addr, bot_age, bot_sex, my_addr, my_age, my_sex],
|
99 |
+
outputs=state
|
100 |
+
)
|
101 |
+
|
102 |
+
with gr.Column():
|
103 |
+
screen = gr.Chatbot(label="μ΅λͺ
μ μλ")
|
104 |
+
with gr.Row():
|
105 |
+
speak = gr.Textbox(label="μ
λ ₯μ°½")
|
106 |
+
btn = gr.Button(label="Talk")
|
107 |
+
btn.click(
|
108 |
+
fn=chatbot.test,
|
109 |
+
inputs=speak,
|
110 |
+
outputs=screen
|
111 |
+
)
|
112 |
+
demo.launch(share=True)
|
113 |
|
|
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flagged/log.csv
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'self','output','flag','username','timestamp'
|
2 |
+
'λνκ³ κ³μΈμ?','[(''μλ
νμΈμ'', ''λ΅''), (''λνκ³ κ³μΈμ?'', ''μ κ²μνλ©΄μ μμ΄μ©'')]','','','2022-06-29 07:59:03.609856'
|
3 |
+
'λνκ³ κ³μΈμ?','[(''μλ
νμΈμ'', ''λ΅''), (''λνκ³ κ³μΈμ?'', ''μ κ²μνλ©΄μ μμ΄μ©'')]','','','2022-06-29 07:59:07.265460'
|
4 |
+
'μλ
νμΈμ?','[[''μλ
νμΈμ?'', ''μλ'']]','','','2022-06-29 08:15:33.284872'
|