NATURESPEAK-ML-UTTER/speak_metric_broadcast.py

159 lines
3.2 KiB
Python
Raw Normal View History

2022-03-13 17:09:05 +01:00
import argparse, json, sys, time, random
import utterance.voice
import utterance.utils
import examine.metric
from osc4py3.as_eventloop import *
from osc4py3 import oscbuildparse
voices_broadcast = {}
conf = {}
def init_broadcast():
global conf, voices_broadcast
with open('voice.config.json') as f:
conf = json.load(f)
# configure voices and their channels
for v in conf['voices']:
voices_broadcast[v['name']] = v['osc_channel']['root'] + v['osc_channel']['utterance']
print(voices_broadcast)
# Start the system.
osc_startup()
# Make client channels to send packets.
osc_udp_client(conf['host'], conf['port'], "aaaa")
def broadcast_utterance(name, utterance):
global conf, voices_broadcast
sentences = utterance.split('\n')
text = name + ":"
for t in sentences:
if ',' in t:
phrases = t.split(',')
for i in range(0, len(phrases)):
p = phrases[i]
if p != "":
text += " " + p
if i != len(phrases) - 1 :
text += ','
msg = oscbuildparse.OSCMessage(voices_broadcast[name], None, [text])
osc_send(msg, "aaaa")
osc_process()
time.sleep(2)
else:
if text == "":
text += t
else:
text += "\n" + t
msg = oscbuildparse.OSCMessage(voices_broadcast[name], None, [text])
osc_send(msg, "aaaa")
osc_process()
time.sleep(2)
msg = oscbuildparse.OSCMessage(conf['command_osc_channel'], None, ["clear"])
osc_send(msg, "aaaa")
osc_process()
def format_str(text: str) -> str:
t = utterance.utils.clean(text)
return utterance.utils.format(t)
def main() -> int:
p = argparse.ArgumentParser()
p.add_argument("-c", "--config", type=str, default="config.json", help="configuratin file")
p.add_argument("-i", "--iterations", type=int, default=10, help="number of iterations")
args = p.parse_args()
print(args)
with open(args.config) as f:
conf = json.load(f)
voices = []
for v in conf['voices']:
voice = utterance.voice.Voice(name=v["name"].upper(), model=v['model_dir'], tokenizer=v['tokeniser_file'], temp=float(v["temperature"]), lenght=7)
voices.append(voice)
init_broadcast()
nbr_voices = len(voices)
state = 'c'
metric = examine.metric.Metric(model_input='data/models/doc2vec.model')
s = set(range(0, nbr_voices - 1))
# rindex = random.sample(s, 1)[0]
rindex = 4
v = voices[rindex]
uv = v.utter_one()
uv = format_str(uv)
v_vec = metric.vector(uv)
while 1:
candidates = random.sample(s, 2)
results = []
for c in candidates:
if c == rindex:
continue
vc = voices[c]
vc_texts = vc.utter_n(n=25)
for t in vc_texts:
t = format_str(t)
t_vec = metric.vector(t)
d = examine.metric.cos_dist(v_vec, t_vec)
results.append([d, t, c])
results.sort(key=lambda t: t[0], reverse=True)
# print('----------------------------')
# print(v.name + ":")
# print(uv)
# print('----------------------------')
broadcast_utterance(v.name, uv)
# for r in results[:2]:
# print('-->' + str(r[0]))
# print(r[1])
# print('+++++++++++++++++++++++++')
# new round
top = results[0]
rindex = top[2]
v = voices[rindex]
uv = top[1]
v_vec = metric.vector(top[1])
# state = input("Continue? ")
osc_terminate()
if __name__ == '__main__':
sys.exit(main())