Skip to content

Commit

Permalink
move Whispermodel and whisper_load_model ot rcpp_whisper.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijffels committed Oct 6, 2024
1 parent e97ae4d commit fa2ae97
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 40 deletions.
26 changes: 13 additions & 13 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// whisper_load_model
SEXP whisper_load_model(std::string model, bool use_gpu);
RcppExport SEXP _audio_whisper_whisper_load_model(SEXP modelSEXP, SEXP use_gpuSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type model(modelSEXP);
Rcpp::traits::input_parameter< bool >::type use_gpu(use_gpuSEXP);
rcpp_result_gen = Rcpp::wrap(whisper_load_model(model, use_gpu));
return rcpp_result_gen;
END_RCPP
}
// whisper_print_benchmark
void whisper_print_benchmark(SEXP model, int n_threads);
RcppExport SEXP _audio_whisper_whisper_print_benchmark(SEXP modelSEXP, SEXP n_threadsSEXP) {
Expand All @@ -31,23 +43,11 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// whisper_load_model
SEXP whisper_load_model(std::string model, bool use_gpu);
RcppExport SEXP _audio_whisper_whisper_load_model(SEXP modelSEXP, SEXP use_gpuSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type model(modelSEXP);
Rcpp::traits::input_parameter< bool >::type use_gpu(use_gpuSEXP);
rcpp_result_gen = Rcpp::wrap(whisper_load_model(model, use_gpu));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_audio_whisper_whisper_load_model", (DL_FUNC) &_audio_whisper_whisper_load_model, 2},
{"_audio_whisper_whisper_print_benchmark", (DL_FUNC) &_audio_whisper_whisper_print_benchmark, 2},
{"_audio_whisper_whisper_language_info", (DL_FUNC) &_audio_whisper_whisper_language_info, 0},
{"_audio_whisper_whisper_load_model", (DL_FUNC) &_audio_whisper_whisper_load_model, 2},
{NULL, NULL, 0}
};

Expand Down
26 changes: 26 additions & 0 deletions src/rcpp_whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,32 @@ int main(int argc, char ** argv) {




// Functionality to free the Rcpp::XPtr
class WhisperModel {
public:
struct whisper_context * ctx;
WhisperModel(std::string model, bool use_gpu = false){
struct whisper_context_params cparams;
cparams.use_gpu = use_gpu;
ctx = whisper_init_from_file_with_params(model.c_str(), cparams);
}
~WhisperModel(){
whisper_free(ctx);
}
};

// [[Rcpp::export]]
SEXP whisper_load_model(std::string model, bool use_gpu = false) {
// Load language model and return the pointer to be used by whisper_encode
//struct whisper_context * ctx = whisper_init(model.c_str());
//Rcpp::XPtr<whisper_context> ptr(ctx, false);
WhisperModel * wp = new WhisperModel(model, use_gpu);
Rcpp::XPtr<WhisperModel> ptr(wp, false);
return ptr;
}


// [[Rcpp::export]]
void whisper_print_benchmark(SEXP model, int n_threads = 1) {
whisper_params params;
Expand Down
27 changes: 0 additions & 27 deletions src/rcpp_whisper_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,3 @@ Rcpp::DataFrame whisper_language_info() {
Rcpp::Named("language_label") = label,
Rcpp::Named("stringsAsFactors") = false);
}


// Functionality to free the Rcpp::XPtr
class WhisperModel {
public:
struct whisper_context * ctx;
WhisperModel(std::string model, bool use_gpu = false){
struct whisper_context_params cparams;
cparams.use_gpu = use_gpu;
ctx = whisper_init_from_file_with_params(model.c_str(), cparams);
}
~WhisperModel(){
whisper_free(ctx);
}
};

// [[Rcpp::export]]
SEXP whisper_load_model(std::string model, bool use_gpu = false) {
// Load language model and return the pointer to be used by whisper_encode
//struct whisper_context * ctx = whisper_init(model.c_str());
//Rcpp::XPtr<whisper_context> ptr(ctx, false);
WhisperModel * wp = new WhisperModel(model, use_gpu);
Rcpp::XPtr<WhisperModel> ptr(wp, false);
return ptr;
}


0 comments on commit fa2ae97

Please sign in to comment.