noch mehr sprechen

This commit is contained in:
2024-04-06 21:43:51 +02:00
parent 2f5f25d0bd
commit 2ecb40333c
7 changed files with 174 additions and 45 deletions

View File

@ -12,6 +12,7 @@ using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Google.Cloud.TextToSpeech.V1;
using Palmmedia.ReportGenerator.Core;
[Serializable]
public class TextToSpeechResponse
@ -35,7 +36,7 @@ public class Text2Speech : MonoBehaviour
[SerializeField]
public bool generate = false;
[SerializeField]
public bool newVoiceAndGenerate = false;
public bool newVoice = false;
[SerializeField]
public string Voice;
@ -49,8 +50,10 @@ public class Text2Speech : MonoBehaviour
private OpenAIAPI _openAiApi;
private Conversation? _conversation;
private readonly string _openAiApiKey = "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";
private readonly string _prompt = "Write a short text for a Developer as an NPC in a game. The Developer works at a small gamedevelopement office and its manager is called Gottfried who is responsable for all the Developers needs. The text should be based on the following bullet-point context, which describes the events of the last moments. Remember to only respond with the short text that only this ONE Developer should speak and nothing else! The context is: ";
private readonly string _defaultPrompt = "Write a short text for a Developer as an NPC in a game. The Developer works at a small gamedevelopement office and its manager is called Gottfried who is responsable for all the Developers needs and protection. The text should be based on the following bullet-point context, which describes the events of the last moments. Remember to only respond with the short text that only this ONE Developer should speak and nothing else! The context is: ";
private readonly string _shortPrompt = "Write a relatively short text for a Developer as an NPC in a game. The Developer works at a small gamedevelopement office and its manager is called Gottfried who is responsable for all the Developers needs and protection. The text should be based on the following bullet-point context, which describes the events of the last moments. Remember to only respond with the relatively short text that only this ONE Developer should speak and nothing else! The context is: ";
private readonly string _veryShortPrompt = "Write a very short text for a Developer as an NPC in a game. The Developer works at a small gamedevelopement office and its manager is called Gottfried who is responsable for all the Developers needs and protection. The text should be based on the following bullet-point context, which describes the events of the last moments. Remember to only respond with the very short text that only this ONE Developer should speak and nothing else! The context is: ";
private int _shortnessLevel = 0;
void Start()
{
_tmpPath = "tmp_audio_" + GetInstanceID().ToString() + ".wav";
@ -69,9 +72,9 @@ public class Text2Speech : MonoBehaviour
generate = false;
if (voice == null)
{
GetRandomGermanVoice(gender, (v) => {
GetRandomEnglishVoice(gender, (v) => {
voice = v;
Voice = voice.ToString();
Voice = voice.ToString();
//Debug.Log($"GoogleCloud: Choosen voice is\n{voice}");
StartCoroutine(GenerateAndSynthesizeText(context));
});
@ -81,27 +84,27 @@ public class Text2Speech : MonoBehaviour
StartCoroutine(GenerateAndSynthesizeText(context));
}
}
if (newVoiceAndGenerate)
if (newVoice)
{
newVoiceAndGenerate = false;
newVoice = false;
voice = null;
Voice = null;
generate = true;
}
}
public void Generate(string c)
public void Generate(string c, int shortnessLevel = 0)
{
context = c;
_shortnessLevel = shortnessLevel;
generate = true;
}
public void GetRandomGermanVoice(string gender, Action<JToken> callback)
public void GetRandomEnglishVoice(string gender, Action<JToken> callback)
{
StartCoroutine(GetRandomGermanVoiceCoroutine(gender, callback));
StartCoroutine(GetRandomEnglishVoiceCoroutine(gender, callback));
}
private IEnumerator GetRandomGermanVoiceCoroutine(string gender, Action<JToken> callback)
private IEnumerator GetRandomEnglishVoiceCoroutine(string gender, Action<JToken> callback)
{
string url = $"https://texttospeech.googleapis.com/v1beta1/voices?key={_googelCloudApiKey}";
@ -191,7 +194,19 @@ public class Text2Speech : MonoBehaviour
};
_conversation = _openAiApi.Chat.CreateConversation(chatRequest);
_conversation.AppendUserInput(_prompt + context);
string prompt = _defaultPrompt;
switch (_shortnessLevel)
{
case 1:
prompt = _shortPrompt; break;
case 2:
prompt = _veryShortPrompt; break;
default:
break;
}
_conversation.AppendUserInput(prompt + context);
string response = await _conversation.GetResponseFromChatbotAsync();
//Debug.Log($"ChatGPT: {response}");
return response;