Krotos Modules 3
Loading...
Searching...
No Matches
TextToAssets.h
Go to the documentation of this file.
1#pragma once
2
3namespace krotos
4{
5
6constexpr char ttfEmbeddingsFileName[] = ".ttfEmbCache";
7constexpr char ttfStatusFileName[] = ".ttfStatus";
8constexpr char ttfStatusDateProperty[] = "date modified";
9constexpr char ttfStatusSizeProperty[] = "folder size";
10constexpr char ttfNumericalMetadata[] = "-0123456789";
11
12static const Identifier FilesEmbeddingsIdentifier{"FilesEmbeddings"};
13static const Identifier fileNameIdentifier{"fileName"};
14static const Identifier catIDNameIdentifier{"catID"};
15static const Identifier ttfEmbeddingsNameIdentifier{"embeddings"};
16static const Identifier customParamIdentifier{"CUSTOMPARAM"};
17static const Identifier samplePathsIdentifier{"SamplePaths"};
18
19
20static const Identifier contentIdentifier{"content"};
21static const Identifier textIdentifier{"text"};
22static const Identifier soundscapeIdentifier{"Soundscape"};
23static const Identifier layer1Identifier{"Layer-1"};
24static const Identifier layer2Identifier{"Layer-2"};
25static const Identifier layer3Identifier{"Layer-3"};
26static const Identifier layer4Identifier{"Layer-4"};
27
35
40 class TextToAssets : public Thread
41 {
42 public:
43
44 // struct to hold asset returned by text-to-preset
45 struct TTPAsset {
46 String path; // path to the asset
47 String label; // label for display in XYPad
48 String engine; // CoreEngine mode "granular" or "sampler"
49 };
50
53
56
61 std::tuple<String, std::array<TTPAsset, 4> > findPresetFiles(String searchTerm);
62
67 std::vector<float> rankify(const std::vector<float>& x) const;
68
73 bool processFileList(Array<File>& filesToProcess);
74
79
88 static void searchForTagAndProperty(ValueTree& root, Identifier tag, Identifier property, String newSample,
89 Identifier target);
90
98 static void updateTabLabel(ValueTree& root, int index, const String& label);
99
101 static void updateCoreEngineParamValue(ValueTree& tree, const var& coreEngineID, const var& paramID, const String& value);
102
104 virtual File createFilePath(String filename);
105
107 bool isUCSValid(String catID);
108
110 static std::unique_ptr<XmlElement> getInitTTPData();
111
117 void setClaudeApiKey(const String& key) { m_claudeApiKeyEncrypted = key; };
118
123 {
124 public:
125 virtual ~Listener() = default;
126
131
138 virtual void onTextToAssetsProgressUpdate(int processedFiles, int totalFiles,
139 const String& estimatedTimeRemaining) = 0;
140 };
141
145 void addListener(Listener* listener);
146
150 void removeListener(Listener* listener);
151
153 bool isAvailable() { return m_ttaAvailable; };
154
162 bool isStrictMode() { return m_strictMode; };
163
172
173 protected:
176 virtual void calculateEmbeddingsForFile(const File& file);
177
179 virtual bool isModelFileAvailable() const { return m_modelFileAvailable; }
180
181 // model file available
183
184 // Call listeners synchronously, helper for unit testing
186
187 private:
188 // struct to hold asset metadata and embeddings
189 struct Data
190 {
191 String catID; // UCS CatID
192 String path; // full file path
193 std::vector<float> embeddings; // SBERT embedding
194 Data(String catID = String(), String path = String(), std::vector<float> embeddings = std::vector<float>())
196 {
197 }
198 };
199
200 // struct for preset layers
201 struct Layer {
203 float bias;
206 String layerName;
210 String engine;
211 std::vector<float> layerEmbedding;
212 std::vector<float> assetEmbedding;
213 };
214
216 void addBiasToLayers(const String& query, std::vector<Layer>& layers);
217
219 StringArray parseAnthropicResponse(std::unique_ptr<InputStream>& stream);
220
222 StringArray getLLMLayers(std::unique_ptr<InputStream>& stream);
223
228 StringArray anthropicAPICall(String query);
229
231 std::vector<float> calculateLLMLayerScores(const std::vector<Layer>& layers, const StringArray& generatedLayerNames);
232
234 float dot(const std::vector<float>& a, const std::vector<float>& b) const;
235
237 void normalise(std::vector<float>& x) const;
238
240 std::vector<float> getVectorScores(String catID, const std::vector<float>& x) const;
241
243 std::tuple<String, StringArray> preprocessQuery(String query) const;
244
246 std::vector<std::pair<float, int>> topk(std::vector<std::pair<float, int>> data, int topk) const;
247
249 std::vector<float> calculateReciprocalRankFusion(const std::vector<float>& scores1,
250 const std::vector<float>& scores2, float k = 2.0f,
251 float alpha = 0.5f) const;
252
254 bool matchesCategory(const String& catID, StringArray tags) const;
255
257 void clear();
258
260 void run() override;
261
264
266 bool readFromFile(File file);
267
272 String sanitizeString(String text);
273
278 String capitalize(const String& text);
279
282
285
291 std::tuple<String, String> findClosestAmbienceCatID(const String& query, const std::vector<float>& queryEmbedding);
292
294 std::vector<int> argsort(const std::vector<float>& x) const;
295
297 bool containsKeyword(const String& text, const StringArray& keywords);
298
300 void maskLayersContainingTag(String tag, std::vector<Layer>& layers);
301
303 void excludeLayers(const StringArray& excludeList, std::vector<Layer>& layers);
304
312 std::tuple<float, int, bool> findLayer(const std::vector<float>& queryEmbedding,
313 const std::vector<float>& presetEmbedding,
314 const std::vector<Layer>& layers,
315 const std::vector<float>& layerScoresLLM);
316
321 std::tuple<String, float> findAsset(const Layer& layer);
322
329
337 void notifyProgressUpdate(const int processedFiles, const int totalFiles, const String& estimatedTimeRemaining);
338
339 // Best Match 25 (BM25) algorithm
340 std::unique_ptr<BM25> m_bm25;
341
342 // vector to hold corpus of data for BM25 algorithm
343 std::vector<String> m_bm25Corpus;
344
345 // keep assets organised by UCS CatID
346 std::unordered_map<String, std::vector<Data> > m_datasetMap;
347 std::unordered_map<String, std::vector<std::size_t> > m_datasetIDMap;
348
349 // map to keep track of how many assets a user has for each UCS CatID
350 std::map<String, int> m_assetCounter;
351
352 // asset sample paths
353 StringArray m_samplePaths;
354
355 // asset file names
356 StringArray m_sampleFilenames;
357
358 // indicates should save the file
359 bool m_shouldSave = true;
360
361 // the sentence transformer (SBERT)
363
364 // class that holds UCS CatIDs
365 SharedResourcePointer<UniversalCategorySystem> m_UCS;
366
367 // critical section for thread safety
368 CriticalSection m_cs;
369
370 // File processing is in progress
371 bool m_ttaAvailable{true};
372
373 // stores SBERT embeddings and associated labels for classifying a query
374 std::vector<std::pair<String, std::vector<float> > > m_queryEmbeddings;
375
376 // organises preset layers by ambience subcategory
377 std::unordered_map<String, std::vector<Layer> > m_layersMap;
378
380
381 ListenerList<Listener> m_listeners;
382
383 // Async listener updaters for status and progress notifications
384 // These are used to coalesce multiple updates
385 // to reduce spamming the message thread during file processing
387
388 // Strict mode disables bias in layer selection, potentially reducing output likelihood.
389 bool m_strictMode{false};
390 };
391
394 {
395 public:
396 SharedResourcePointer<TextToAssets> textToAssets;
397 };
398
399 class AnthropicAPIException : public std::runtime_error {
400 public:
401 AnthropicAPIException(const String& message) : std::runtime_error(message.toStdString()) {}
402 };
403
404 class LLMJsonParseException : public std::runtime_error {
405 public:
406 LLMJsonParseException(const String& message) : std::runtime_error(message.toStdString()) {}
407 };
408
409} // namespace krotos
Definition TextToAssets.h:399
AnthropicAPIException(const String &message)
Definition TextToAssets.h:401
Definition helpers.h:92
Definition TextToAssets.h:404
LLMJsonParseException(const String &message)
Definition TextToAssets.h:406
Definition SentenceTransformer.h:9
Listener for the TextToAssets module.
Definition TextToAssets.h:123
virtual void onTextToAssetsProcessingStatusChanged(bool isAvailable)=0
called when the text to assets module has finished processing files
virtual void onTextToAssetsProgressUpdate(int processedFiles, int totalFiles, const String &estimatedTimeRemaining)=0
Callback to notify progress updates on file processing.
take a user text query and return 4 assets that can be loaded as an ambience preset
Definition TextToAssets.h:41
std::vector< std::pair< String, std::vector< float > > > m_queryEmbeddings
Definition TextToAssets.h:374
std::tuple< String, StringArray > preprocessQuery(String query) const
return preprocessed query and exclude tags
bool m_synchronousUpdates
Definition TextToAssets.h:185
bool matchesCategory(const String &catID, StringArray tags) const
check if UCS catID matches one of our target tags
static void searchForTagAndProperty(ValueTree &root, Identifier tag, Identifier property, String newSample, Identifier target)
this will add a sample that comes from ttp to the corresponding core egine but it also update the xyp...
Definition TextToAssets.cpp:1240
virtual bool isModelFileAvailable() const
Returns whether there is a model file available for use, in order to generate embeddings.
Definition TextToAssets.h:179
void loadFileEmbeddings()
will load embeddings data from file to all modules that need it
~TextToAssets()
Definition TextToAssets.cpp:18
bool readFromFile(File file)
load embeddings from file
String capitalize(const String &text)
make first letter of string upper case and the rest lower case
virtual void calculateEmbeddingsForFile(const File &file)
create text embeddings file for the given file
StringArray m_samplePaths
Definition TextToAssets.h:353
void notifyAvailabilityStatus(bool isAvailable)
Update TTA availability status and notify listeners. TTA should not available whilst file processing ...
Definition TextToAssets.cpp:1339
SentenceTransformer m_sentenceTransformer
Definition TextToAssets.h:362
std::tuple< String, std::array< TTPAsset, 4 > > findPresetFiles(String searchTerm)
find 4 assets suitable for the given query
void run() override
run thread
virtual File createFilePath(String filename)
creates the path for the embeddings file
void clear()
clears internal data
static std::unique_ptr< XmlElement > getInitTTPData()
xml for automated preset generation
Definition TextToAssets.cpp:1333
std::tuple< float, int, bool > findLayer(const std::vector< float > &queryEmbedding, const std::vector< float > &presetEmbedding, const std::vector< Layer > &layers, const std::vector< float > &layerScoresLLM)
find a relevant preset layer
String m_claudeApiKeyEncrypted
Definition TextToAssets.h:379
std::vector< float > rankify(const std::vector< float > &x) const
output the rank of each vector element
static void updateTabLabel(ValueTree &root, int index, const String &label)
This function searches for a specific TABLABEL by index within the CUSTOMDATA node and updates its te...
Definition TextToAssets.cpp:1273
bool m_strictMode
Definition TextToAssets.h:389
void maskLayersContainingTag(String tag, std::vector< Layer > &layers)
set layer.isValid to false for layers containing tag.
void removeListener(Listener *listener)
Definition TextToAssets.cpp:1373
String sanitizeString(String text)
removes digits and underscores from a string
bool m_shouldSave
Definition TextToAssets.h:359
std::vector< float > calculateLLMLayerScores(const std::vector< Layer > &layers, const StringArray &generatedLayerNames)
return similarity scores for layers based on the LLM generated layer names
std::map< String, int > m_assetCounter
Definition TextToAssets.h:350
void setStrictMode(bool isStrictMode)
Sets whether "Strict Mode" is enabled or disabled (default: disabled).
Definition TextToAssets.h:171
bool isAvailable()
Returns the status of the file processing.
Definition TextToAssets.h:153
std::unordered_map< String, std::vector< std::size_t > > m_datasetIDMap
Definition TextToAssets.h:347
bool isStrictMode()
Returns if "Strict Mode" is enabled (default: disabled).
Definition TextToAssets.h:162
CriticalSection m_cs
Definition TextToAssets.h:368
void normalise(std::vector< float > &x) const
L2 normalise the input vector.
Definition TextToAssets.cpp:1214
float dot(const std::vector< float > &a, const std::vector< float > &b) const
return dot product between a and b
Definition TextToAssets.cpp:1209
void addBiasToLayers(const String &query, std::vector< Layer > &layers)
add a non-zero bias to layers that match certain keywords
static void updateCoreEngineParamValue(ValueTree &tree, const var &coreEngineID, const var &paramID, const String &value)
update a CoreEngine PARAM value
Definition TextToAssets.cpp:1302
std::tuple< String, String > findClosestAmbienceCatID(const String &query, const std::vector< float > &queryEmbedding)
search for closest match to query
bool isUCSValid(String catID)
check UCS CatID is valid
void setClaudeApiKey(const String &key)
Set the encrypted API key string for the Claude API.
Definition TextToAssets.h:117
bool appendDataToEmbeddingsFile()
appends data to the main embeddings file
StringArray getLLMLayers(std::unique_ptr< InputStream > &stream)
returns a StringArray of preset layer names generated by an LLM
Definition TextToAssets.cpp:80
std::vector< float > calculateReciprocalRankFusion(const std::vector< float > &scores1, const std::vector< float > &scores2, float k=2.0f, float alpha=0.5f) const
calculate the reciprocal rank fusion for scores1 and scores2
StringArray anthropicAPICall(String query)
send query to an LLM using the anthropic API
Definition TextToAssets.cpp:115
bool processFileList(Array< File > &filesToProcess)
processes a file list to create text embeddings
StringArray parseAnthropicResponse(std::unique_ptr< InputStream > &stream)
parse Anthropic API response and return results in a StringArray
Definition TextToAssets.cpp:31
std::vector< std::pair< float, int > > topk(std::vector< std::pair< float, int > > data, int topk) const
return topk elements
bool m_modelFileAvailable
Definition TextToAssets.h:182
AsyncListenerUpdater< Listener > m_progressUpdater
Definition TextToAssets.h:386
std::unordered_map< String, std::vector< Layer > > m_layersMap
Definition TextToAssets.h:377
std::vector< float > getVectorScores(String catID, const std::vector< float > &x) const
return vector scores between query embedding and dataset embeddings
Definition TextToAssets.cpp:1223
AsyncListenerUpdater< Listener > m_statusUpdater
Definition TextToAssets.h:386
bool containsKeyword(const String &text, const StringArray &keywords)
returns true if the text contains any keyword in keywords.
void addListener(Listener *listener)
Definition TextToAssets.cpp:1371
std::unique_ptr< BM25 > m_bm25
Definition TextToAssets.h:340
bool m_ttaAvailable
Definition TextToAssets.h:371
void notifyProgressUpdate(const int processedFiles, const int totalFiles, const String &estimatedTimeRemaining)
Notifies listeners of the progress of the current file processing operation.
Definition TextToAssets.cpp:1353
std::vector< int > argsort(const std::vector< float > &x) const
returns the indices that would sort the vector.
std::unordered_map< String, std::vector< Data > > m_datasetMap
Definition TextToAssets.h:346
void excludeLayers(const StringArray &excludeList, std::vector< Layer > &layers)
uses the contents of exclude list to set some preset layers as invalid if certain keywords are detect...
Definition TextToAssets.cpp:1193
void loadPresetLayers()
load preset layers from json stored as binary data
StringArray m_sampleFilenames
Definition TextToAssets.h:356
std::tuple< String, float > findAsset(const Layer &layer)
find a relevant asset for a given preset layer
void loadQueryEmbeddings()
load quer embeddings from json stored as binary data
ListenerList< Listener > m_listeners
Definition TextToAssets.h:381
std::vector< String > m_bm25Corpus
Definition TextToAssets.h:343
TextToAssets()
Definition TextToAssets.cpp:5
SharedResourcePointer< UniversalCategorySystem > m_UCS
Definition TextToAssets.h:365
wrapper class for text to file module shared resource pointer
Definition TextToAssets.h:394
SharedResourcePointer< TextToAssets > textToAssets
Definition TextToAssets.h:396
Definition AirAbsorptionFilter.cpp:2
static const Identifier layer3Identifier
Definition TextToAssets.h:25
static const Identifier fileNameIdentifier
Definition TextToAssets.h:13
constexpr char ttfStatusDateProperty[]
Definition TextToAssets.h:8
static const Identifier customParamIdentifier
Definition TextToAssets.h:16
static const Identifier layer1Identifier
Definition TextToAssets.h:23
static const Identifier layer4Identifier
Definition TextToAssets.h:26
constexpr char ttfEmbeddingsFileName[]
Definition TextToAssets.h:6
static const Identifier soundscapeIdentifier
Definition TextToAssets.h:22
constexpr char ttfNumericalMetadata[]
Definition TextToAssets.h:10
AssetsStatus
Definition TextToAssets.h:29
@ kAssetsRemoved
Definition TextToAssets.h:30
@ kAssetsAdded
Definition TextToAssets.h:31
@ kAssetsInit
Definition TextToAssets.h:33
@ kAssetsNoChange
Definition TextToAssets.h:32
static const Identifier samplePathsIdentifier
Definition TextToAssets.h:17
static const Identifier contentIdentifier
Definition TextToAssets.h:20
constexpr char ttfStatusFileName[]
Definition TextToAssets.h:7
static const Identifier textIdentifier
Definition TextToAssets.h:21
constexpr char ttfStatusSizeProperty[]
Definition TextToAssets.h:9
static const Identifier ttfEmbeddingsNameIdentifier
Definition TextToAssets.h:15
static const Identifier layer2Identifier
Definition TextToAssets.h:24
static const Identifier catIDNameIdentifier
Definition TextToAssets.h:14
static const Identifier FilesEmbeddingsIdentifier
Definition TextToAssets.h:12
Definition TextToAssets.h:190
Data(String catID=String(), String path=String(), std::vector< float > embeddings=std::vector< float >())
Definition TextToAssets.h:194
std::vector< float > embeddings
Definition TextToAssets.h:193
String catID
Definition TextToAssets.h:191
String path
Definition TextToAssets.h:192
Definition TextToAssets.h:201
bool isValid
Definition TextToAssets.h:202
std::vector< float > assetEmbedding
Definition TextToAssets.h:212
String assetCatID
Definition TextToAssets.h:208
String collection
Definition TextToAssets.h:205
String engine
Definition TextToAssets.h:210
String parentCatID
Definition TextToAssets.h:204
float bias
Definition TextToAssets.h:203
String layerDescription
Definition TextToAssets.h:207
std::vector< float > layerEmbedding
Definition TextToAssets.h:211
String layerName
Definition TextToAssets.h:206
String assetDescription
Definition TextToAssets.h:209
Definition TextToAssets.h:45
String path
Definition TextToAssets.h:46
String engine
Definition TextToAssets.h:48
String label
Definition TextToAssets.h:47