Skip to content

Commit

Permalink
Merge branch 'develop' of github.com-myrobotlab:MyRobotLab/myrobotlab…
Browse files Browse the repository at this point in the history
… into inmoov-and-statemachine-1
  • Loading branch information
supertick committed Nov 17, 2023
2 parents 2953d72 + 7dabffd commit 31a2c1b
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 25 deletions.
110 changes: 110 additions & 0 deletions src/main/java/org/myrobotlab/service/NovelAI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.myrobotlab.service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.myrobotlab.io.FileIO;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.service.abstracts.AbstractSpeechSynthesis;
import org.myrobotlab.service.config.NovelAIConfig;
import org.myrobotlab.service.data.AudioData;
import org.slf4j.Logger;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class NovelAI extends AbstractSpeechSynthesis<NovelAIConfig> {

private static final long serialVersionUID = 1L;

public final static Logger log = LoggerFactory.getLogger(NovelAI.class);

private transient OkHttpClient client = new OkHttpClient();

public NovelAI(String n, String id) {
super(n, id);
client = new OkHttpClient();
}

/**
* The methods apply and getConfig can be used, if more complex configuration
* handling is needed. By default, the framework takes care of most of it,
* including subscription handling.
*
* <pre>
&#64;Override
public ServiceConfig apply(ServiceConfig c) {
super.apply(c)
return c;
}
@Override
public ServiceConfig getConfig() {
super.getConfig()
return config;
}
* </pre>
**/

public static void main(String[] args) {
try {

LoggingFactory.init(Level.INFO);

Runtime.start("novelai", "NovelAI");
Runtime.start("webgui", "WebGui");

} catch (Exception e) {
log.error("main threw", e);
}
}

@Override
public AudioData generateAudioData(AudioData audioData, String toSpeak) throws Exception {

String baseUrl = "https://api.novelai.net/ai/generate-voice?voice=-1&seed=" + config.voice + "&opus=false&version=v2&text=";
String encodedText = URLEncoder.encode(toSpeak, StandardCharsets.UTF_8.toString());
String url = baseUrl + encodedText;

Request request = new Request.Builder().url(url).build();

try {
Response response = client.newCall(request).execute();

if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
if (responseBody != null) {
byte[] bytes = responseBody.bytes();
FileIO.toFile(audioData.getFileName(), bytes);
}
} else {
error("request failed with code: " + response.code());
}
} catch (Exception e) {
error(e);
}
return audioData;
}

@Override
public void loadVoices() throws Exception {
addVoice("Aini", "female", "en", "Aini");
addVoice("Ligeia", "female", "en", "Ligeia");
addVoice("Orea", "female", "en", "Orea");
addVoice("Claea", "female", "en", "Claea");
addVoice("Lim", "female", "en", "Lim");
addVoice("Aurae", "female", "en", "Aurae");
addVoice("Naia", "female", "en", "Naia");

addVoice("Aulon", "male", "en", "Aulon");
addVoice("Elei", "male", "en", "Elei");
addVoice("Ogma", "male", "en", "Ogma");
addVoice("Raid", "male", "en", "Raid");
addVoice("Pega", "male", "en", "Pega");
addVoice("Lam", "male", "en", "Lam");
}
}
14 changes: 12 additions & 2 deletions src/main/java/org/myrobotlab/service/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,16 @@ public RandomConfig getConfig() {
super.getConfig();

config.enabled = enabled;

if (config.randomMessages == null) {
config.randomMessages = new HashMap<>();
}

for (String key : randomData.keySet()) {
RandomMessage msg = randomData.get(key);
RandomMessageConfig m = new RandomMessageConfig();
m.service = msg.name;
m.method = msg.method;
m.maxIntervalMs = msg.maxIntervalMs;
m.minIntervalMs = msg.minIntervalMs;
m.data = msg.data;
Expand All @@ -316,12 +322,16 @@ public RandomConfig getConfig() {
@Override
public RandomConfig apply(RandomConfig c) {
super.apply(c);
enabled = c.enabled;
if (c.enabled) {
enable();
} else {
disable();
}

try {
for (String key : c.randomMessages.keySet()) {
RandomMessageConfig msgc = c.randomMessages.get(key);
addRandom(msgc.minIntervalMs, msgc.maxIntervalMs, key.substring(0, key.lastIndexOf(".")), key.substring(key.lastIndexOf(".") + 1), msgc.data);
addRandom(key, msgc.minIntervalMs, msgc.maxIntervalMs, msgc.service, msgc.method, msgc.data);
if (!msgc.enabled) {
disable(key);
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/myrobotlab/service/config/InMoov2Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,40 +383,40 @@ public Plan getDefault(Plan plan, String name) {
random.enabled = false;

// setup name references to different services
RandomMessageConfig rm = new RandomMessageConfig(3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
RandomMessageConfig rm = new RandomMessageConfig(name, "setLeftArmSpeed", 3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
random.randomMessages.put(name + ".setLeftArmSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
rm = new RandomMessageConfig(name, "setRightArmSpeed", 3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
random.randomMessages.put(name + ".setRightArmSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 0.0, 5.0, 85.0, 95.0, 25.0, 30.0, 10.0, 15.0);
rm = new RandomMessageConfig(name, "moveLeftArm", 000, 8000, 0.0, 5.0, 85.0, 95.0, 25.0, 30.0, 10.0, 15.0);
random.randomMessages.put(name + ".moveLeftArm", rm);

rm = new RandomMessageConfig(3000, 8000, 0.0, 5.0, 85.0, 95.0, 25.0, 30.0, 10.0, 15.0);
rm = new RandomMessageConfig(name, "moveRightArm", 3000, 8000, 0.0, 5.0, 85.0, 95.0, 25.0, 30.0, 10.0, 15.0);
random.randomMessages.put(name + ".moveRightArm", rm);

rm = new RandomMessageConfig(3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
rm = new RandomMessageConfig(name, "setLeftHandSpeed", 3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
random.randomMessages.put(name + ".setLeftHandSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
rm = new RandomMessageConfig(name, "setRightHandSpeed", 3000, 8000, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0, 8.0, 25.0);
random.randomMessages.put(name + ".setRightHandSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 10.0, 160.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 130.0, 175.0);
rm = new RandomMessageConfig(name, "moveLeftHand", 3000, 8000, 10.0, 160.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 130.0, 175.0);
random.randomMessages.put(name + ".moveLeftHand", rm);

rm = new RandomMessageConfig(3000, 8000, 10.0, 160.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 130.0, 175.0);
rm = new RandomMessageConfig(name, "moveRightHand", 3000, 8000, 10.0, 160.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 10.0, 60.0, 130.0, 175.0);
random.randomMessages.put(name + ".moveRightHand", rm);

rm = new RandomMessageConfig(3000, 8000, 8.0, 20.0, 8.0, 20.0, 8.0, 20.0);
rm = new RandomMessageConfig(name, "setHeadSpeed",3000, 8000, 8.0, 20.0, 8.0, 20.0, 8.0, 20.0);
random.randomMessages.put(name + ".setHeadSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 70.0, 110.0, 65.0, 115.0, 70.0, 110.0);
rm = new RandomMessageConfig(name, "moveHead", 3000, 8000, 70.0, 110.0, 65.0, 115.0, 70.0, 110.0);
random.randomMessages.put(name + ".moveHead", rm);

rm = new RandomMessageConfig(3000, 8000, 2.0, 5.0, 2.0, 5.0, 2.0, 5.0);
rm = new RandomMessageConfig(name , "setTorsoSpeed", 3000, 8000, 2.0, 5.0, 2.0, 5.0, 2.0, 5.0);
random.randomMessages.put(name + ".setTorsoSpeed", rm);

rm = new RandomMessageConfig(3000, 8000, 85.0, 95.0, 88.0, 93.0, 70.0, 110.0);
rm = new RandomMessageConfig(name, "moveTorso", 3000, 8000, 85.0, 95.0, 88.0, 93.0, 70.0, 110.0);
random.randomMessages.put(name + ".moveTorso", rm);

// == Peer - headTracking =============================
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/myrobotlab/service/config/NovelAIConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.myrobotlab.service.config;

import org.myrobotlab.framework.Plan;

public class NovelAIConfig extends SpeechSynthesisConfig {


public NovelAIConfig() {
voice = "Aini";
}

@Override
public Plan getDefault(Plan plan, String name) {
super.getDefault(plan, name);
return plan;
}

}
14 changes: 11 additions & 3 deletions src/main/java/org/myrobotlab/service/config/RandomConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ public static class RandomMessageConfig {
public long minIntervalMs;
public long maxIntervalMs;
public boolean enabled = true;
public String service;
public String method;

public RandomMessageConfig() {
}

public RandomMessageConfig(long minIntervalMs, long maxIntervalMs, double... ranges) {
public RandomMessageConfig(String service, String method, long minIntervalMs, long maxIntervalMs, double... ranges) {
this.service = service;
this.method = method;
this.minIntervalMs = minIntervalMs;
this.maxIntervalMs = maxIntervalMs;
this.data = new Range[ranges.length / 2];
Expand All @@ -29,7 +33,9 @@ public RandomMessageConfig(long minIntervalMs, long maxIntervalMs, double... ran
}
}

public RandomMessageConfig(long minIntervalMs, long maxIntervalMs, int... ranges) {
public RandomMessageConfig(String service, String method, long minIntervalMs, long maxIntervalMs, int... ranges) {
this.service = service;
this.method = method;
this.minIntervalMs = minIntervalMs;
this.maxIntervalMs = maxIntervalMs;
this.data = new Range[ranges.length / 2];
Expand All @@ -42,7 +48,9 @@ public RandomMessageConfig(long minIntervalMs, long maxIntervalMs, int... ranges
}
}

public RandomMessageConfig(long minIntervalMs, long maxIntervalMs, float... ranges) {
public RandomMessageConfig(String service, String method, long minIntervalMs, long maxIntervalMs, float... ranges) {
this.service = service;
this.method = method;
this.minIntervalMs = minIntervalMs;
this.maxIntervalMs = maxIntervalMs;
for (int i = 0; i < (ranges.length / 2); i += 2) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/myrobotlab/service/meta/NovelAIMeta.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.myrobotlab.service.meta;

import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.service.meta.abstracts.MetaData;
import org.slf4j.Logger;

public class NovelAIMeta extends MetaData {
private static final long serialVersionUID = 1L;
public final static Logger log = LoggerFactory.getLogger(NovelAIMeta.class);

/**
* This class is contains all the meta data details of a service. It's peers,
* dependencies, and all other meta data related to the service.
*
*/
public NovelAIMeta() {

// add a cool description
addDescription("service to interface with NovelAI");

// false will prevent it being seen in the ui
setAvailable(true);

// add it to one or many categories
addCategory("speech");

// add a sponsor to this service
// the person who will do maintenance
// setSponsor("GroG");

}

}
Binary file added src/main/resources/resource/NovelAI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions src/main/resources/resource/WebGui/app/service/js/NovelAIGui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
angular.module('mrlapp.service.NovelAIGui', []).controller('NovelAIGuiCtrl', ['peer', '$scope', 'mrl', '$uibModal', function(peer, $scope, mrl, $uibModal) {
console.info('NovelAIGuiCtrl')
var _self = this
var msg = this.msg
$scope.autoClear = true
$scope.textArea = false
$scope.spoken = ''

// new selected voice "container" - since it comes from a map next leaves are
// key & value ... value contains the entire voice selected
$scope.newVoice = {
selected: null
}

this.updateState = function(service) {
$scope.service = service
if (service.voice) {
$scope.newVoice.selected = {
'key': service.voice.name,
'value': service.voice
}
}
$scope.$apply()
}

this.onMsg = function(inMsg) {
let data = inMsg.data[0]
switch (inMsg.method) {
case 'onState':
_self.updateState(data)
break
case 'onStartSpeaking':
$scope.spoken = data
$scope.$apply()
break
default:
console.error("ERROR - unhandled method " + $scope.name + " " + inMsg.method)
break
}
}

$scope.speak = function(text) {
msg.send("speak", text)

if ($scope.autoClear) {
$scope.text = ''
}
}

$scope.setVoice = function(text) {
console.log($scope.service.voice.name)
msg.send("setVoice", text.name)
}

msg.subscribe('publishStartSpeaking')
msg.subscribe(this)
}
])
Loading

0 comments on commit 31a2c1b

Please sign in to comment.