-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_indonesian.py
148 lines (128 loc) · 4.03 KB
/
main_indonesian.py
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
import eel
import json
import time
import numpy as np
import pyaudio
import random
import soundfile as sf
from tts import tts_infer
from translate import translator
from llm_models import ChainingModel
import threading
from pysentimiento import create_analyzer
eel.init('chat')
with open('config.json') as user_config:
configs = json.load(user_config)
name = configs['user_name']
assistant_name = configs['bot_name']
generator = None
emotion_analyzer = None
ts = None
tl = None
@eel.expose
def initialize_model():
global generator, emotion_analyzer, ts, tl
with open('config.json') as user_config:
configs = json.load(user_config)
name = configs['user_name']
assistant_name = configs['bot_name']
generator = ChainingModel(
model='RedPajama-INCITE-Chat-3B-v1-q5_1',
name=name,
assistant_name=assistant_name
)
ts = tts_infer(model_name=configs['vits_model'])
tl = translator(indonesian=True)
emotion_analyzer = create_analyzer(task="emotion", lang="en", model_name='bertweet-base-emotion-analysis')
generator, emotion_analyzer, ts, tl = initialize_model()
words_to_clean = ["\n<human", "\n<bot"]
def change_words(words, name, assistant_name):
new_words = []
for word in words:
new_word = word.replace('human', name)
new_words.append(new_word)
new_word = word.replace('bot', assistant_name)
new_words.append(new_word)
return new_words
words_clean = change_words(words_to_clean, name, assistant_name)
def clean_res(result, words_to_clean):
cleaned_result = result
for word in words_clean:
cleaned_result = cleaned_result.replace(word, "")
return cleaned_result
p = pyaudio.PyAudio()
@eel.expose
def play_audio():
filename = 'dialog.wav'
data, samplerate = sf.read(filename, dtype='float32')
data_int = (data * 32767).astype(np.int16)
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=samplerate,
output=True)
chunk_size = 1024
i = 0
while i < len(data_int):
chunk_end = min(i + chunk_size, len(data_int))
stream.write(data_int[i:chunk_end].tobytes())
i += chunk_size
stream.stop_stream()
stream.close()
@eel.expose
def read_config_file():
try:
with open('config.json') as file:
data = json.load(file)
return data
except FileNotFoundError:
return None
@eel.expose
def save_config_file(data):
try:
with open('config.json', 'w') as file:
json.dump(data, file, indent=2)
return True
except:
return False
idle = [('waifu/idle.png')]
blink = [('waifu/blink.png')]
others = [(f"waifu/other{i}.png") for i in range(1, 6)]
anger = [(f"waifu/anger{i}.png") for i in range(1, 3)]
disgust = [(f"waifu/disgust{i}.png") for i in range(1, 3)]
fear = [(f"waifu/fear{i}.png") for i in range(1, 3)]
joy = [(f"waifu/joy{i}.png") for i in range(1, 3)]
surprise = [('waifu/surprise.png')]
sadness = [('waifu/sad.png')]
emotion_images = {
'joy': joy,
'others': others,
'surprise': surprise,
'disgust': disgust,
'sadness': sadness,
'fear': fear,
'anger': anger
}
@eel.expose
def handleinput(x):
eel.disableText()
eel.statusBot('mengetik...')
input_user = tl.id_en(x)
result = generator.chain(input_user)
result = result["text"]
en_answer = clean_res(result, words_to_clean)
id_answer = tl.en_id(en_answer)
eel.botResponse(id_answer)
eel.statusBot('merekam audio...')
emotion = emotion_analyzer.predict(en_answer).output
jp_answer = tl.en_jp(en_answer)
ts.convert(jp_answer)
eel.Audio()
if jp_answer is not None:
if emotion in emotion_images:
image_list=emotion_images[emotion]
random_index = random.randint(0, len(image_list) - 1)
image = image_list[random_index]
eel.emotion(image)
eel.disableText('enable')
eel.statusBot('males ngomong gw')
eel.start('index.html', size =(903,860))