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
| 7 |
+
AutoTokenizer,
|
| 8 |
+
AutoModelForSequenceClassification,
|
| 9 |
+
pipeline,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
model_names = [
|
| 13 |
+
'wangchanberta-base-att-spm-uncased',
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
tokenizers = {
|
| 17 |
+
'wangchanberta-base-att-spm-uncased': AutoTokenizer,
|
| 18 |
+
}
|
| 19 |
+
public_models = ['xlm-roberta-base', 'bert-base-multilingual-cased']
|
| 20 |
+
#Choose Pretrained Model
|
| 21 |
+
model_name = "wangchanberta-base-att-spm-uncased"
|
| 22 |
+
|
| 23 |
+
#create tokenizer
|
| 24 |
+
tokenizer = tokenizers[model_name].from_pretrained(
|
| 25 |
+
f'airesearch/{model_name}' if model_name not in public_models else f'{model_name}',
|
| 26 |
+
revision='main',
|
| 27 |
+
model_max_length=416,)
|
| 28 |
+
|
| 29 |
+
#pipeline
|
| 30 |
+
zero_classify = pipeline(task='zero-shot-classification',
|
| 31 |
+
tokenizer=tokenizer,
|
| 32 |
+
model=AutoModelForSequenceClassification.from_pretrained(
|
| 33 |
+
f'airesearch/{model_name}' if model_name not in public_models else f'airesearch/{model_name}-finetuned',
|
| 34 |
+
revision='finetuned@xnli_th')
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def intent_classifier(text_input, candidate_labels, zero_classify=zero_classify):
|
| 38 |
+
output_label = zero_classify(text_input, candidate_labels=candidate_labels)
|
| 39 |
+
return output_label['labels'][0]
|
| 40 |
+
|
| 41 |
+
customer_name = "จิรานุวัฒน์"
|
| 42 |
+
bot_identity = 'female'
|
| 43 |
+
bot_name = 'ท้องฟ้า'
|
| 44 |
+
pronoun = 'ดิฉัน' if bot_identity == 'female' else 'กระผม'
|
| 45 |
+
sentence_ending = ['ค่ะ','คะ'] if bot_identity == 'female' else ['ครับ','ครับ']
|
| 46 |
+
comany_name = 'แมวเหมียว'
|
| 47 |
+
|
| 48 |
+
# Create a directed graph
|
| 49 |
+
A = nx.DiGraph(section='A')
|
| 50 |
+
|
| 51 |
+
# Add nodes and edges
|
| 52 |
+
A.add_node("START A", response=f"สวัสดี{sentence_ending[0]} ขอเรียนสายคุณ {customer_name} {sentence_ending[0]}")
|
| 53 |
+
A.add_node("A1", response=f"{pronoun} ต้องกราบขอประทานโทษเป็นอย่างสูงที่โทรมารบกวนนะ{sentence_ending[1]} {pronoun} ชื่อ {bot_name} ใบอนุญาตนายหน้าประกันวินาศภัยเลขที่ XXXXXXXXXX ติดต่อจากบริษัท {comany_name} จำกัด โทรมาเพื่อขออนุญาตนำเสนอสิทธิประโยชน์สำหรับลูกค้าของธนาคาร{comany_name} ไม่ทราบว่าจะสะดวกหรือไม่{sentence_ending[1]}", intent_classify= lambda x :intent_classifier(x,["ได้","ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้"]))
|
| 54 |
+
A.add_node("A2", response=f"{pronoun} ขออนุญาตติดต่อกลับคุณ{customer_name} อีกครั้งในวันที่....ไม่ทราบว่า คุณ{customer_name} สะดวกไหม{sentence_ending[1]} ")
|
| 55 |
+
A.add_node("END", response=f"ต้องกราบขอประทานโทษเป็นอย่างสูงที่โทรมารบกวนนะ{sentence_ending[1]} {pronoun} หวังเป็นอย่างยิ่งว่าทางบริษัท {comany_name} จะได้ให้บริการคุณ{customer_name} ในโอกาสถัดไปนะ{sentence_ending[1]} หากคุณ{customer_name} ไม่ประสงค์ที่จะให้บริษัท {comany_name} ติดต่อเพื่อนำเสนอบริการของ บริษัท {comany_name} สามารถแจ้งผ่าน Call Center โทร 02-123-4567 ได้{sentence_ending[0]} ขอขอบพระคุณ ที่สละเวลาในการฟังข้อมูลของ บริษัท {comany_name} ขออนุญาตวางสาย{sentence_ending[0]} สวัสดี{sentence_ending[0]}")
|
| 56 |
+
A.add_node("A3", response=f"ขอบพระคุณ{sentence_ending[0]} และเพื่อเป็นการปรับปรุงคุณภาพในการให้บริการ ขออนุญาตบันทึกเสียงการสนทนาในครั้งนี้ด้วยนะ{sentence_ending[1]}", intent_classify= lambda x :intent_classifier(x,["ได้","ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้"]))
|
| 57 |
+
A.add_node("END A1", response=f"ขอบพระคุณ{sentence_ending[0]} ดิฉันจะไม่บันทึกเสียงการสนทนาในครั้งนี้{sentence_ending[0]}")
|
| 58 |
+
A.add_node("END A2", response=f"ขอบพระคุณ{sentence_ending[0]} ขณะนี้ได้เริ่มบันทึกการสนทนาแล้วนะ{sentence_ending[1]}")
|
| 59 |
+
|
| 60 |
+
A.add_edges_from((("START A","A1"),("A1","A2"),("A2","END"),("A1","A3"),("A3","END A1"),("A3","END A2")))
|
| 61 |
+
|
| 62 |
+
# Create a directed graph
|
| 63 |
+
B = nx.DiGraph(section='B')
|
| 64 |
+
|
| 65 |
+
# Add nodes and edges
|
| 66 |
+
B.add_node("START B", response=f"เนื่องในโอกาสที่ ธนาคาร{comany_name} ได้จัดตั้งบริษัท {comany_name} จำกัด เข้าเป็นบริษัทในกลุ่มธุรกิจการเงินของธนาคาร โดยมีวัตถุประสงค์ประกอบกิจการเป็นนายหน้าประกันวินาศภัย {pronoun} {bot_name} จึงติดต่อมาเพื่อขออนุญาตนำเสนอแผนประกันภัยรถยนต์แบบพิเศษเฉพาะลูกค้าของธนาคาร{comany_name}เท่านั้น {pronoun}ขอชี้แจงรายละเอียดนะ{sentence_ending[1]} ")
|
| 67 |
+
B.add_node("B1", response=f"เพื่อให้ท่านสมาชิกได้รับประโยชน์สูงสุด จึงขออนุญาตสอบถามข้อมูลรถยนต์ของคุณ{customer_name} นะ{sentence_ending[1]}")
|
| 68 |
+
B.add_node("B2", response=f"รถยนต์มีประกันประเภทใด (1,2,3,2+,3+) รับประกันภัยโดยบริษัทฯใด สิ้นสุดความคุ้มครองเมื่อใด")
|
| 69 |
+
B.add_node("END B", response=f"{comany_name}ได้คัดสรรค์แบบประกัน เพื่อเป็นทางเลือกที่คุ้มค่าไว้บริการสำหรับลูกค้าของธนาคาร{comany_name} ดังนี้")
|
| 70 |
+
|
| 71 |
+
B.add_edges_from((("START B","B1"),("B1","B2"),("B2","END B")))
|
| 72 |
+
|
| 73 |
+
Bot_dialog = nx.compose(A, B)
|
| 74 |
+
Bot_dialog.add_edges_from((("END A1","START B"),("END A2","START B")))
|
| 75 |
+
|
| 76 |
+
# Initialize session state
|
| 77 |
+
if "Bot_dialog" not in st.session_state:
|
| 78 |
+
st.session_state.Bot_dialog = Bot_dialog
|
| 79 |
+
if "messages" not in st.session_state:
|
| 80 |
+
st.session_state.messages = []
|
| 81 |
+
if "current_node" not in st.session_state:
|
| 82 |
+
st.session_state.current_node = "START A"
|
| 83 |
+
|
| 84 |
+
def main():
|
| 85 |
+
st.title("Voicebot's Chatbot Demo")
|
| 86 |
+
|
| 87 |
+
with st.chat_message("assistant"):
|
| 88 |
+
message_placeholder = st.empty()
|
| 89 |
+
full_response = ""
|
| 90 |
+
assistant_response = st.session_state.Bot_dialog.nodes["START A"]["response"]
|
| 91 |
+
|
| 92 |
+
# Simulate stream of response with milliseconds delay
|
| 93 |
+
for chunk in assistant_response.split():
|
| 94 |
+
full_response += chunk + " "
|
| 95 |
+
time.sleep(0.05)
|
| 96 |
+
# Add a blinking cursor to simulate typing
|
| 97 |
+
message_placeholder.markdown(full_response + "▌")
|
| 98 |
+
message_placeholder.markdown(full_response)
|
| 99 |
+
|
| 100 |
+
for message in st.session_state.messages:
|
| 101 |
+
with st.chat_message(message["role"]):
|
| 102 |
+
st.markdown(message["content"])
|
| 103 |
+
|
| 104 |
+
# Accept user input
|
| 105 |
+
if prompt := st.chat_input("Enter your message."):
|
| 106 |
+
# Add user message to chat history
|
| 107 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 108 |
+
# Display user message in chat message container
|
| 109 |
+
with st.chat_message("user"):
|
| 110 |
+
st.markdown(prompt)
|
| 111 |
+
|
| 112 |
+
next_nodes = list(st.session_state.Bot_dialog.successors(st.session_state.current_node))
|
| 113 |
+
if next_nodes:
|
| 114 |
+
if "intent_classify" in st.session_state.Bot_dialog.nodes[st.session_state.current_node]:
|
| 115 |
+
intent = st.session_state.Bot_dialog.nodes[st.session_state.current_node]["intent_classify"](prompt)
|
| 116 |
+
|
| 117 |
+
if len(next_nodes) == 1:
|
| 118 |
+
st.session_state.current_node = next_nodes[0]
|
| 119 |
+
else:
|
| 120 |
+
if intent == "ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้":
|
| 121 |
+
st.session_state.current_node = next_nodes[0]
|
| 122 |
+
else:
|
| 123 |
+
st.session_state.current_node = next_nodes[1]
|
| 124 |
+
|
| 125 |
+
# Display assistant response in chat message container
|
| 126 |
+
with st.chat_message("assistant"):
|
| 127 |
+
message_placeholder = st.empty()
|
| 128 |
+
full_response = ""
|
| 129 |
+
assistant_response = st.session_state.Bot_dialog.nodes[st.session_state.current_node]["response"]
|
| 130 |
+
if st.session_state.current_node == "END B" or st.session_state.current_node == "END":
|
| 131 |
+
st.warning("Conversation Ended. Please refresh this page to start a new conversation.")
|
| 132 |
+
|
| 133 |
+
# Simulate stream of response with milliseconds delay
|
| 134 |
+
for chunk in assistant_response.split():
|
| 135 |
+
full_response += chunk + " "
|
| 136 |
+
time.sleep(0.05)
|
| 137 |
+
# Add a blinking cursor to simulate typing
|
| 138 |
+
message_placeholder.markdown(full_response + "▌")
|
| 139 |
+
message_placeholder.markdown(full_response)
|
| 140 |
+
|
| 141 |
+
# Add assistant response to chat history
|
| 142 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|