add support for "/emote xxx" in talkinghead

This commit is contained in:
Juha Jeronen
2024-01-02 02:26:51 +02:00
parent 45c2ae5ea5
commit f9804dc30c
2 changed files with 21 additions and 1 deletions

View File

@@ -688,6 +688,15 @@ def start_talking():
def stop_talking():
return talkinghead.stop_talking()
@app.route('/api/talkinghead/set_emotion', methods=["POST"])
@require_module("talkinghead")
def emote():
data = request.get_json()
if "emotion_name" not in data or not isinstance(data["emotion_name"], str):
abort(400, '"emotion_name" is required')
emotion_name = data["emotion_name"]
return talkinghead.setEmotion([{"label": emotion_name, "score": 1.0}]) # mimic the `classify` API result
@app.route('/api/talkinghead/result_feed')
@require_module("talkinghead")
def result_feed():

View File

@@ -66,6 +66,9 @@ def setEmotion(_emotion: Dict[str, float]) -> None:
Currently, we pick the emotion with the highest confidence score.
The `set_emotion` API endpoint also uses this function to set the current emotion,
with a manually formatted dictionary containing just one entry.
_emotion: result of sentiment analysis: {emotion0: confidence0, ...}
"""
global current_emotion
@@ -78,8 +81,16 @@ def setEmotion(_emotion: Dict[str, float]) -> None:
highest_score = item["score"]
highest_label = item["label"]
logger.debug(f"setEmotion: applying emotion {highest_label}")
# Never triggered currently, because `setSpriteSlashCommand` at the client end (`SillyTavern/public/scripts/extensions/expressions/index.js`)
# searches for a static sprite for the given expression, and does not proceed to `sendExpressionCall` if not found.
# So beside `talkinghead.png`, your character also needs the static sprites for "/emote xxx" to work.
if highest_label not in global_animator_instance.emotions:
logger.warning(f"setEmotion: emotion '{highest_label}' does not exist, setting to 'neutral'")
highest_label = "neutral"
logger.info(f"setEmotion: applying emotion {highest_label}")
current_emotion = highest_label
return f"emotion set to {highest_label}"
def unload() -> str:
"""Stop animation."""