C++ Example
The following steps can be followed to create a minimal C++ end to end setup using realtime Whisper transcribed text and performing inference on the text with Flora Engine models.
- Create a C++ Actor from the content browser in Unreal Engine
- In the
.Build.csof the C++ project add"FloraEngine"and"WhisperASR"toPublicDependencyModuleNames - In your Actor's header file add
#include "FloraEngineSubsystem"and#inclue "WhisperSubsystem"in the .cpp file - Add a
UNLM*variable andUFloraEngineSubsystem*to hold references to the loaded model and subsystem for ease of use - Create the function
void OnWhisperProcessed(const TArray<FString>& TranscribedText)and add the macroUFUNCTION()above it - In the
BeginPlay()in the Actor's .cpp file get a reference to theFloraEngineSubsystemand initialize it as well as theNLMyou would like to perform inference on - Get a reference to the
WhisperSubsystemand initialize it withWhisperInit() - Then bind
OnWhisperProcessedtoOnRealtimeWhisperProcessedin the subsystem and callRealtimeCaptureStart(true, false, "") - Create your
OnWhisperProcessedfunction body - Append the transcribed text together into one string
- Create a
FModelAsyncPrompt*withWorldContextObjectset to your actor,Modelset to yourNLM*,Promptset to the transcribed string andInstructionLineset to the instruction in the .json template file that you wish to use (for.csvmodels, "StateMachineInstruction" must be used) - Call
AsyncInferon theFloraEngineSubsystemusing theFModelAsyncPrompt. A function delegate must be provided that will run on the game thread after inference - After inference is complete, call
GetOutputwith the created model to retrieve the output and reaction
void AMyActor::BeginPlay()
{
Super::BeginPlay();
FloraSubsystem = GetGameInstance()->GetSubsystem<UFloraEngineSubsystem>();
FloraSubsystem->Init();
Model = FloraSubsystem->InitNLM(this, "ModelName", TArray<FString>());
UWhisperSubsystem* WhisperSubsystem = GetGameInstance()->GetSubsystem<UWhisperSubsystem>();
WhisperSubsystem->WhisperInit();
WhisperSubsystem->OnRealtimeWhisperProcessed.AddDynamic(this, &AMyActor::OnWhisperProcessed);
WhisperSubsystem->RealtimeCaptureStart(true, false, "");
}
void AMyActor::OnWhisperProcessed(const TArray<FString>& TranscribedText)
{
FString FullText;
for (const FString& Text : TranscribedText)
{
FullText.Append(Text);
}
FModelAsyncPrompt* Prompt = new FModelAsyncPrompt{
.WorldContextObject = this,
.Model = Model,
.Prompt = FullText,
.InstructionLine = "StateMachineInstruction"
};
FloraSubsystem->AsyncInfer(Prompt, [this, Prompt]() {
uint8 InstructionIndex, ReactionIndex;
FString Output, Reaction;
float TokenSpeed;
FloraSubsystem->GetOutput(this, Prompt->Model, Output, InstructionIndex, ReactionIndex, Reaction, TokenSpeed);
UE_LOG(LogTemp, Warning, TEXT("Output: %s"), Output);
UE_LOG(LogTemp, Warning, TEXT("Reaction: %s"), Reaction);
});
}
Databiomes