From 3d08a2cae10d9b56f5a1a64b5cdb57aa1ad9a4d5 Mon Sep 17 00:00:00 2001 From: Viet Anh Nguyen Date: Wed, 2 Aug 2023 21:48:14 +0700 Subject: [PATCH] Refactoring --- CMakeLists.txt | 15 ++++++++------ customchar/embeddb/CMakeLists.txt | 8 -------- customchar/embeddb/embed_search.h | 8 ++++---- customchar/embeddb/{embed_db.h => embeddb.h} | 4 ++-- customchar/embeddb/types.h | 8 +++++--- customchar/executors/plugin_executor.h | 18 ++++++++--------- .../executors/plugins/video_record_plugin.h | 20 +++++++++---------- .../embeddb => libs}/hnswlib/bruteforce.h | 0 .../embeddb => libs}/hnswlib/hnswalg.h | 0 .../embeddb => libs}/hnswlib/hnswlib.h | 0 .../embeddb => libs}/hnswlib/space_ip.h | 0 .../embeddb => libs}/hnswlib/space_l2.h | 0 .../hnswlib/visited_list_pool.h | 0 13 files changed, 39 insertions(+), 42 deletions(-) delete mode 100644 customchar/embeddb/CMakeLists.txt rename customchar/embeddb/{embed_db.h => embeddb.h} (90%) rename {customchar/embeddb => libs}/hnswlib/bruteforce.h (100%) rename {customchar/embeddb => libs}/hnswlib/hnswalg.h (100%) rename {customchar/embeddb => libs}/hnswlib/hnswlib.h (100%) rename {customchar/embeddb => libs}/hnswlib/space_ip.h (100%) rename {customchar/embeddb => libs}/hnswlib/space_l2.h (100%) rename {customchar/embeddb => libs}/hnswlib/visited_list_pool.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28701f5..227522f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,14 @@ find_package(OpenCV REQUIRED) add_subdirectory(libs/whisper-cpp) add_subdirectory(libs/subprocess) -# Build embeding search module -add_subdirectory(customchar/embeddb) +include_directories(libs .) + +# Build embeding search library +add_library( + embeddb + customchar/embeddb/document.cpp + customchar/embeddb/embed_search.cpp +) # Build CustomChar-core set(TARGET customchar-core) @@ -47,10 +53,8 @@ add_library( target_include_directories( ${TARGET} PUBLIC ${SDL2_INCLUDE_DIRS} - libs - . ) -target_link_libraries(${TARGET} PUBLIC ${SDL2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS} whisper subprocess) +target_link_libraries(${TARGET} PUBLIC ${SDL2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS} whisper subprocess embeddb) # Set binary output directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -113,7 +117,6 @@ if(BUILD_GUI) ${CMAKE_SOURCE_DIR}/fonts $/fonts) endif() - add_executable( test_ffmpeg_as_input tests/test_ffmpeg_as_input.cpp ) diff --git a/customchar/embeddb/CMakeLists.txt b/customchar/embeddb/CMakeLists.txt deleted file mode 100644 index 8d46a99..0000000 --- a/customchar/embeddb/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -include_directories( - hnswlib -) -add_library( - cc_embed_search - embed_search.cpp - test_emd.cpp -) diff --git a/customchar/embeddb/embed_search.h b/customchar/embeddb/embed_search.h index d08b310..bd6ab16 100644 --- a/customchar/embeddb/embed_search.h +++ b/customchar/embeddb/embed_search.h @@ -1,5 +1,5 @@ -#ifndef CUSTOMCHAR_EMBED_SEARCH_EMBED_SEARCH_H_ -#define CUSTOMCHAR_EMBED_SEARCH_EMBED_SEARCH_H_ +#ifndef CUSTOMCHAR_EMBEDDB_EMBED_SEARCH_H_ +#define CUSTOMCHAR_EMBEDDB_EMBED_SEARCH_H_ #include #include @@ -7,7 +7,7 @@ #include #include -#include "customchar/embed_search/search_embed_result.h" +#include "customchar/embeddb/types.h" #include "hnswlib/hnswlib.h" namespace CC { @@ -47,4 +47,4 @@ class EmbedSearch { } // namespace embeddb } // namespace CC -#endif // CUSTOMCHAR_EMBED_SEARCH_EMBED_SEARCH_H_ +#endif diff --git a/customchar/embeddb/embed_db.h b/customchar/embeddb/embeddb.h similarity index 90% rename from customchar/embeddb/embed_db.h rename to customchar/embeddb/embeddb.h index 8718b38..4272430 100644 --- a/customchar/embeddb/embed_db.h +++ b/customchar/embeddb/embeddb.h @@ -1,5 +1,5 @@ -#ifndef CUSTOMCHAR_EMBEDDB_EMBED_DB_H_ -#define CUSTOMCHAR_EMBEDDB_EMBED_DB_H_ +#ifndef CUSTOMCHAR_EMBEDDB_EMBEDDB_H_ +#define CUSTOMCHAR_EMBEDDB_EMBEDDB_H_ #include #include diff --git a/customchar/embeddb/types.h b/customchar/embeddb/types.h index be26f37..c5ce808 100644 --- a/customchar/embeddb/types.h +++ b/customchar/embeddb/types.h @@ -1,12 +1,14 @@ -#ifndef CUSTOMCHAR_CHARACTER_CHARACTER_H_ -#define CUSTOMCHAR_CHARACTER_CHARACTER_H_ +#ifndef CUSTOMCHAR_EMBEDDB_TYPES_H_ +#define CUSTOMCHAR_EMBEDDB_TYPES_H_ + namespace CC { namespace embeddb { struct SearchEmbedResult { int id; float score; }; + } // namespace embeddb } // namespace CC -#endif // CUSTOMCHAR_CHARACTER_CHARACTER_H_ +#endif // CUSTOMCHAR_EMBEDDB_TYPES_H_ diff --git a/customchar/executors/plugin_executor.h b/customchar/executors/plugin_executor.h index 14eaca4..4371575 100644 --- a/customchar/executors/plugin_executor.h +++ b/customchar/executors/plugin_executor.h @@ -36,15 +36,15 @@ class PluginExecutor { {"(.*)stop video recording(.*)", "VIDEO_RECORD"}, {"(.*)record (a|some) video(.*)", "VIDEO_RECORD"}}; - std::function IsRecording_; - std::function StartVideoRecoding_; - std::function StopVideoCapture_; + std::function is_recording_; + std::function start_video_recording_; + std::function stop_video_capture_; std::vector> plugins_ = { std::make_shared("OPEN_APP"), - std::make_shared("VIDEO_RECORD", IsRecording_, - StartVideoRecoding_, - StopVideoCapture_)}; + std::make_shared("VIDEO_RECORD", is_recording_, + start_video_recording_, + stop_video_capture_)}; std::shared_ptr current_plugin_; std::string reflect(const std::string& word); @@ -55,9 +55,9 @@ class PluginExecutor { std::function is_recording, std::function start_video_recording, std::function stop_video_capture) - : IsRecording_(is_recording), - StartVideoRecoding_(start_video_recording), - StopVideoCapture_(stop_video_capture) {} + : is_recording_(is_recording), + start_video_recording_(start_video_recording), + stop_video_capture_(stop_video_capture) {} /// @brief Parse and execute input /// @param input user input string diff --git a/customchar/executors/plugins/video_record_plugin.h b/customchar/executors/plugins/video_record_plugin.h index cbd2e8c..dff193b 100644 --- a/customchar/executors/plugins/video_record_plugin.h +++ b/customchar/executors/plugins/video_record_plugin.h @@ -14,9 +14,9 @@ namespace executors { /// @brief Plugin for record videos class VideoRecordPlugin : public Plugin { private: - std::function IsRecording_; - std::function StartVideoRecoding_; - std::function StopVideoCapture_; + std::function is_recording_; + std::function start_video_recording_; + std::function stop_video_capture_; public: VideoRecordPlugin( @@ -24,9 +24,9 @@ class VideoRecordPlugin : public Plugin { std::function start_video_recording, std::function stop_video_capture) : Plugin(name), - IsRecording_(is_recording), - StartVideoRecoding_(start_video_recording), - StopVideoCapture_(stop_video_capture) {} + is_recording_(is_recording), + start_video_recording_(start_video_recording), + stop_video_capture_(stop_video_capture) {} bool handle(const std::string& input, std::string& response, bool& finished) override { // Normalize app name @@ -40,17 +40,17 @@ class VideoRecordPlugin : public Plugin { normalized_input.find("end") != std::string::npos || normalized_input.find("finish") != std::string::npos; if (contains_stop) { - if (!IsRecording_()) { + if (!is_recording_()) { finished = true; return false; } - StopVideoCapture_(); + stop_video_capture_(); response = "Stopping video recording"; finished = true; return true; } - if (IsRecording_()) { + if (is_recording_()) { finished = true; return false; } @@ -59,7 +59,7 @@ class VideoRecordPlugin : public Plugin { std::stringstream ss; ss << "video_" << std::time(nullptr) << ".mkv"; - StartVideoRecoding_(ss.str()); + start_video_recording_(ss.str()); response = "Starting video recording..."; finished = true; return true; diff --git a/customchar/embeddb/hnswlib/bruteforce.h b/libs/hnswlib/bruteforce.h similarity index 100% rename from customchar/embeddb/hnswlib/bruteforce.h rename to libs/hnswlib/bruteforce.h diff --git a/customchar/embeddb/hnswlib/hnswalg.h b/libs/hnswlib/hnswalg.h similarity index 100% rename from customchar/embeddb/hnswlib/hnswalg.h rename to libs/hnswlib/hnswalg.h diff --git a/customchar/embeddb/hnswlib/hnswlib.h b/libs/hnswlib/hnswlib.h similarity index 100% rename from customchar/embeddb/hnswlib/hnswlib.h rename to libs/hnswlib/hnswlib.h diff --git a/customchar/embeddb/hnswlib/space_ip.h b/libs/hnswlib/space_ip.h similarity index 100% rename from customchar/embeddb/hnswlib/space_ip.h rename to libs/hnswlib/space_ip.h diff --git a/customchar/embeddb/hnswlib/space_l2.h b/libs/hnswlib/space_l2.h similarity index 100% rename from customchar/embeddb/hnswlib/space_l2.h rename to libs/hnswlib/space_l2.h diff --git a/customchar/embeddb/hnswlib/visited_list_pool.h b/libs/hnswlib/visited_list_pool.h similarity index 100% rename from customchar/embeddb/hnswlib/visited_list_pool.h rename to libs/hnswlib/visited_list_pool.h