1
0

messages.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. export type Role = "assistant" | "system" | "user";
  2. // ChatGPT API
  3. export type Message = {
  4. role: Role;
  5. content: string;
  6. };
  7. const talkStyles = [
  8. "talk",
  9. "happy",
  10. "sad",
  11. "angry",
  12. "fear",
  13. "surprised",
  14. ] as const;
  15. export type TalkStyle = (typeof talkStyles)[number];
  16. export type Talk = {
  17. style: TalkStyle;
  18. message: string;
  19. };
  20. //Name of all the expression in the vrm
  21. export const emotionNames: string[] = [];
  22. console.log("All emotion names, ",emotionNames);
  23. export const emotions =
  24. ["neutral", "happy", "angry", "sad", "relaxed", "Surprised",
  25. "Shy", "Jealous", "Bored", "Serious", "Suspicious", "Victory",
  26. "Sleep", "Love"] as const;
  27. // Convert user input to system format e.g. ["suspicious"] -> ["Sus"], ["sleep"] -> ["Sleep"]
  28. const userInputToSystem = (input: string) => {
  29. const mapping: { [key: string]: string } = {
  30. ...Object.fromEntries(emotions
  31. .filter(e => e[0] === e[0].toUpperCase())
  32. .map(e => [e.toLowerCase(), e]))
  33. };
  34. return mapping[input.toLowerCase()] || input;
  35. };
  36. type EmotionType = (typeof emotions)[number];
  37. /**
  38. * A set that includes utterances, voice emotions, and model emotional expressions.
  39. */
  40. export type Screenplay = {
  41. expression: EmotionType;
  42. talk: Talk;
  43. text: string;
  44. };
  45. export const textsToScreenplay = (
  46. texts: string[],
  47. ): Screenplay[] => {
  48. const screenplays: Screenplay[] = [];
  49. let prevExpression = "neutral";
  50. for (let i = 0; i < texts.length; i++) {
  51. const text = texts[i];
  52. const match = text.match(/\[(.*?)\]/);
  53. const tag = (match && match[1]) || prevExpression;
  54. const message = text.replace(/\[(.*?)\]/g, "");
  55. let expression = prevExpression;
  56. const systemTag = userInputToSystem(tag);
  57. if (emotions.includes(systemTag as any)) {
  58. console.log("Emotion detect :",systemTag);
  59. expression = systemTag;
  60. prevExpression = systemTag;
  61. }
  62. screenplays.push({
  63. expression: expression as EmotionType,
  64. talk: {
  65. style: emotionToTalkStyle(expression as EmotionType),
  66. message: message,
  67. },
  68. text: text,
  69. });
  70. }
  71. return screenplays;
  72. };
  73. const emotionToTalkStyle = (emotion: EmotionType): TalkStyle => {
  74. switch (emotion) {
  75. case "angry":
  76. return "angry";
  77. case "happy":
  78. return "happy";
  79. case "sad":
  80. return "sad";
  81. default:
  82. return "talk";
  83. }
  84. };