Jiranuwat commited on
Commit
1205120
1 Parent(s): be6a9ca

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +200 -0
  2. requirements.txt +8 -0
  3. tts_temp.wav +0 -0
app.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import networkx as nx
4
+ import speech_recognition as sr
5
+ import edge_tts
6
+ import asyncio
7
+ from transformers import (
8
+ AutoTokenizer,
9
+ AutoModelForSequenceClassification,
10
+ pipeline,
11
+ )
12
+
13
+ model_names = [
14
+ 'wangchanberta-base-att-spm-uncased',
15
+ ]
16
+
17
+ tokenizers = {
18
+ 'wangchanberta-base-att-spm-uncased': AutoTokenizer,
19
+ }
20
+ public_models = ['xlm-roberta-base', 'bert-base-multilingual-cased']
21
+ #Choose Pretrained Model
22
+ model_name = "wangchanberta-base-att-spm-uncased"
23
+
24
+ #create tokenizer
25
+ tokenizer = tokenizers[model_name].from_pretrained(
26
+ f'airesearch/{model_name}' if model_name not in public_models else f'{model_name}',
27
+ revision='main',
28
+ model_max_length=416,)
29
+
30
+ #pipeline
31
+ zero_classify = pipeline(task='zero-shot-classification',
32
+ tokenizer=tokenizer,
33
+ model=AutoModelForSequenceClassification.from_pretrained(
34
+ f'airesearch/{model_name}' if model_name not in public_models else f'airesearch/{model_name}-finetuned',
35
+ revision='finetuned@xnli_th')
36
+ )
37
+
38
+ def intent_classifier(text_input, candidate_labels, zero_classify=zero_classify):
39
+ output_label = zero_classify(text_input, candidate_labels=candidate_labels)
40
+ return output_label['labels'][0]
41
+
42
+ customer_name = "จิรานุวัฒน์"
43
+ bot_identity = 'female'
44
+ bot_name = 'ท้องฟ้า'
45
+ pronoun = 'ดิฉัน' if bot_identity == 'female' else 'กระผม'
46
+ sentence_ending = ['ค่ะ','คะ'] if bot_identity == 'female' else ['ครับ','ครับ']
47
+ comany_name = 'แมวเหมียว'
48
+
49
+ # Create a directed graph
50
+ A = nx.DiGraph(section='A')
51
+
52
+ # Add nodes and edges
53
+ A.add_node("START A", response=f"สวัสดี{sentence_ending[0]} ขอเรียนสายคุณ {customer_name}{sentence_ending[0]}")
54
+ 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,["ได้","ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้"]))
55
+ A.add_node("A2", response=f"{pronoun} ขออนุญาตติดต่อกลับคุณ{customer_name} อีกครั้งในวันที่....ไม่ทราบว่า คุณ{customer_name} สะดวกไหม{sentence_ending[1]} ")
56
+ 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]}")
57
+ A.add_node("A3", response=f"ขอบพระคุณ{sentence_ending[0]} และเพื่อเป็นการปรับปรุงคุณภาพในการให้บริการ ขออนุญาตบันทึกเสียงการสนทนาในครั้งนี้ด้วยนะ{sentence_ending[1]}", intent_classify= lambda x :intent_classifier(x,["ได้","ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้"]))
58
+ A.add_node("END A1", response=f"ขอบพระคุณ{sentence_ending[0]} ดิฉันจะไม่บันทึกเสียงการสนทนาในครั้งนี้{sentence_ending[0]}")
59
+ A.add_node("END A2", response=f"ขอบพระคุณ{sentence_ending[0]} ขณะนี้ได้เริ่มบันทึกการสนทนาแล้วนะ{sentence_ending[1]}")
60
+
61
+ A.add_edges_from((("START A","A1"),("A1","A2"),("A2","END"),("A1","A3"),("A3","END A1"),("A3","END A2")))
62
+
63
+ # Create a directed graph
64
+ B = nx.DiGraph(section='B')
65
+
66
+ # Add nodes and edges
67
+ B.add_node("START B", response=f"เนื่องในโอกาสที่ ธนาคาร{comany_name} ได้จัดตั้งบริษัท {comany_name} จำกัด เข้าเป็นบริษัทในกลุ่มธุรกิจการเงินของธนาคาร โดยมีวัตถุประสงค์ประกอบกิจการเป็นนายหน้าประกันวินาศภัย {pronoun} {bot_name} จึงติดต่อมาเพื่อขออนุญาตนำเสนอแผนประกันภัยรถยนต์แบบพิเศษเฉพาะลูกค้าของธนาคาร{comany_name}เท่านั้น {pronoun}ขอชี้แจงรายละเอียดนะ{sentence_ending[1]} ")
68
+ B.add_node("B1", response=f"เพื่อให้ท่านสมาชิกได้รับประโยชน์สูงสุด จึงขออนุญาตสอบถามข้อมูลรถยนต์ของคุณ{customer_name} นะ{sentence_ending[1]}")
69
+ B.add_node("B2", response=f"รถยนต์มีประกันประเภทใด (1,2,3,2+,3+) รับประกันภัยโดยบริษัทฯใด สิ้นสุดความคุ้มครองเมื่อใด")
70
+ B.add_node("END B", response=f"{comany_name}ได้คัดสรรค์แบบประกัน เพื่อเป็นทางเลือกที่คุ้มค่าไว้บริการสำหรับลูกค้าของธนาคาร{comany_name} ดังนี้")
71
+
72
+ B.add_edges_from((("START B","B1"),("B1","B2"),("B2","END B")))
73
+
74
+ Bot_dialog = nx.compose(A, B)
75
+ Bot_dialog.add_edges_from((("END A1","START B"),("END A2","START B")))
76
+
77
+ current_node = "START A"
78
+
79
+ def addfile(filepath):
80
+ return filepath
81
+ def submit_file(filepath):
82
+ return filepath
83
+ def clear_audio():
84
+ return None
85
+ def speech_to_text(audiofile_path):
86
+ recognizer = sr.Recognizer()
87
+
88
+ try:
89
+ with sr.WavFile(audiofile_path) as source:
90
+ audio = recognizer.record(source)
91
+ transcription = recognizer.recognize_google(audio,language = "th-TH")
92
+ return transcription
93
+ except:
94
+ return "Could not understand audio"
95
+
96
+ #bot response
97
+ current_node = "START A"
98
+ def user(user_input, history):
99
+ global current_node, Bot_dialog
100
+
101
+ next_nodes = list(Bot_dialog.successors(current_node))
102
+
103
+ if next_nodes != []:
104
+ if "intent_classify" in Bot_dialog.nodes[current_node]:
105
+ intent = Bot_dialog.nodes[current_node]["intent_classify"](user_input)
106
+
107
+ if len(next_nodes) == 1:
108
+ current_node = next_nodes[0]
109
+ else:
110
+ if intent == "ไม่ได้ ไม่ตกลง ยังไม่ตกลง ยังไม่ได้":
111
+ current_node = next_nodes[0]
112
+ else:
113
+ current_node = next_nodes[1]
114
+
115
+ return user_input, history + [[user_input, None]]
116
+
117
+ def bot(history):
118
+ global current_node, Bot_dialog
119
+ history[-1][1] = ""
120
+ bot_message = Bot_dialog.nodes[current_node]["response"]
121
+
122
+ if current_node == "END B" or current_node == "END":
123
+ bot_message += """<span style="color:red"> \n**Conversation Endded** </span>"""
124
+
125
+ for character in bot_message:
126
+ history[-1][1] += character
127
+ time.sleep(0.01)
128
+ yield history
129
+
130
+ async def text_to_speech(inputtext_history: list[str], filename: str = "tts_temp.wav"):
131
+ input_text = inputtext_history[-1][1]
132
+ communicate = edge_tts.Communicate(input_text, "th-TH-PremwadeeNeural")
133
+
134
+ with open(filename, "wb") as file:
135
+ async for chunk in communicate.stream():
136
+ if chunk["type"] == "audio":
137
+ file.write(chunk["data"])
138
+ elif chunk["type"] == "WordBoundary":
139
+ pass
140
+
141
+ return filename
142
+
143
+ async def restart(history):
144
+ global current_node, Bot_dialog
145
+ current_node = "START A"
146
+ await text_to_speech([[None, Bot_dialog.nodes["START A"]["response"]]])
147
+ return [history[0]], '', 'tts_temp.wav'
148
+
149
+ async def main():
150
+ # init first audio greeting
151
+ await text_to_speech([[None, Bot_dialog.nodes["START A"]["response"]]])
152
+
153
+ # Create a Gradio interface
154
+ with gr.Blocks(title='Chatbot Demo') as demo:
155
+ gr.Markdown('DESCRIPTION HERE')
156
+
157
+ chatbot = gr.Chatbot(
158
+ [[None, Bot_dialog.nodes["START A"]["response"]]],
159
+ elem_id="chatbot",
160
+ bubble_full_width=False,
161
+ avatar_images=('https://aui.atlassian.com/aui/9.3/docs/images/avatar-person.svg',
162
+ 'https://avatars.githubusercontent.com/u/51063788?s=200&v=4')
163
+ )
164
+
165
+ with gr.Row():
166
+ txt = gr.Textbox(
167
+ scale=3,
168
+ show_label=False,
169
+ placeholder="transcription is Here.",
170
+ container=False,
171
+ interactive=True,
172
+ )
173
+ voice_btn = gr.Audio(sources="microphone",type="filepath", scale=4)
174
+ voicesubmit_btn = gr.Button(value="Submit Voice✔️", scale=1,variant='primary')
175
+
176
+ with gr.Row():
177
+ sentence = gr.Textbox(visible=False)
178
+ audio = gr.Audio(
179
+ value="tts_temp.wav",
180
+ label="Generated audio response",
181
+ streaming=True,
182
+ autoplay=False,
183
+ interactive=False,
184
+ show_label=True,
185
+ )
186
+
187
+ restart_btn = gr.Button(value='Restart🔄')
188
+
189
+ voice_btn.stop_recording(addfile, inputs=voice_btn, outputs=txt)
190
+ voicesubmit_btn.click(submit_file, inputs=voice_btn, outputs=txt).then(speech_to_text, inputs=voice_btn, outputs=txt).then(user, [txt, chatbot], [txt, chatbot], queue=False).then(bot, chatbot, chatbot).then(clear_audio, outputs=voice_btn).then(text_to_speech, inputs=chatbot, outputs=audio)
191
+ restart_btn.click(restart, chatbot, [chatbot,txt,audio])
192
+
193
+ demo.launch(debug=False)
194
+
195
+ if __name__ == "__main__":
196
+ loop = asyncio.get_event_loop_policy().get_event_loop()
197
+ try:
198
+ loop.run_until_complete(main())
199
+ finally:
200
+ loop.close()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ tensorflow
3
+ gradio
4
+ networkx
5
+ sentencepiece
6
+ transformers
7
+ SpeechRecognition
8
+ edge-tts
tts_temp.wav ADDED
Binary file (21.3 kB). View file