-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
523 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// whisper.h | ||
// | ||
// Copyright (c) 2023-2024 Junpei Kawamoto | ||
// | ||
// This software is released under the MIT License. | ||
// | ||
// http://opensource.org/licenses/mit-license.php | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include <ctranslate2/models/whisper.h> | ||
|
||
#include "rust/cxx.h" | ||
|
||
#include "config.h" | ||
|
||
using ctranslate2::StorageView; | ||
|
||
struct VecStr; | ||
struct VecDetectionResult; | ||
struct WhisperOptions; | ||
struct WhisperGenerationResult; | ||
|
||
class Whisper { | ||
private: | ||
std::unique_ptr<ctranslate2::models::Whisper> impl; | ||
|
||
public: | ||
Whisper(std::unique_ptr<ctranslate2::models::Whisper> impl) | ||
: impl(std::move(impl)) { } | ||
|
||
rust::Vec<WhisperGenerationResult> | ||
generate(const StorageView& features, const rust::Slice<const VecStr> prompts, const WhisperOptions& options) const; | ||
|
||
rust::Vec<VecDetectionResult> | ||
detect_language(const StorageView& features) const; | ||
|
||
inline bool is_multilingual() const { | ||
return impl->is_multilingual(); | ||
} | ||
|
||
inline size_t n_mels() const { | ||
return impl->n_mels(); | ||
} | ||
|
||
inline size_t num_languages() const { | ||
return impl->num_languages(); | ||
} | ||
|
||
inline size_t num_queued_batches() const { | ||
return impl->num_queued_batches(); | ||
} | ||
|
||
inline size_t num_active_batches() const { | ||
return impl->num_active_batches(); | ||
} | ||
|
||
inline size_t num_replicas() const { | ||
return impl->num_replicas(); | ||
} | ||
}; | ||
|
||
inline std::unique_ptr<Whisper> whisper( | ||
rust::Str model_path, | ||
std::unique_ptr<Config> config | ||
) { | ||
return std::make_unique<Whisper>( | ||
std::make_unique<ctranslate2::models::Whisper>( | ||
static_cast<std::string>(model_path), | ||
config->device, | ||
config->compute_type, | ||
std::vector<int>(config->device_indices.begin(), config->device_indices.end()), | ||
config->tensor_parallel, | ||
*config->replica_pool_config | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// whisper.cpp | ||
// | ||
// Copyright (c) 2023-2024 Junpei Kawamoto | ||
// | ||
// This software is released under the MIT License. | ||
// | ||
// http://opensource.org/licenses/mit-license.php | ||
|
||
#include <utility> | ||
|
||
#include "ct2rs/src/whisper.rs.h" | ||
|
||
#include "ct2rs/include/types.h" | ||
|
||
using rust::Slice; | ||
using rust::Vec; | ||
|
||
Vec<WhisperGenerationResult> Whisper::generate( | ||
const StorageView& features, | ||
const Slice<const VecStr> prompts, | ||
const WhisperOptions& opts | ||
) const { | ||
auto futures = impl->generate( | ||
features, | ||
from_rust(prompts), | ||
ctranslate2::models::WhisperOptions { | ||
opts.beam_size, | ||
opts.patience, | ||
opts.length_penalty, | ||
opts.repetition_penalty, | ||
opts.no_repeat_ngram_size, | ||
opts.max_length, | ||
opts.sampling_topk, | ||
opts.sampling_temperature, | ||
opts.num_hypotheses, | ||
opts.return_scores, | ||
opts.return_no_speech_prob, | ||
opts.max_initial_timestamp_index, | ||
opts.suppress_blank, | ||
from_rust(opts.suppress_tokens), | ||
} | ||
); | ||
|
||
Vec<WhisperGenerationResult> res; | ||
for (auto& future : futures) { | ||
const auto& r = future.get(); | ||
res.push_back(WhisperGenerationResult { | ||
to_rust<VecString>(r.sequences), | ||
to_rust<VecUSize>(r.sequences_ids), | ||
to_rust(r.scores), | ||
r.no_speech_prob, | ||
}); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
Vec<VecDetectionResult> Whisper::detect_language(const StorageView& features) const { | ||
auto futures = impl->detect_language(features); | ||
|
||
Vec<VecDetectionResult> res; | ||
for (auto& future : futures) { | ||
const auto& r = future.get(); | ||
|
||
Vec<DetectionResult> pairs; | ||
for (auto& pair : r) { | ||
pairs.push_back(DetectionResult { | ||
std::get<0>(pair), | ||
std::get<1>(pair), | ||
}); | ||
} | ||
|
||
res.push_back(VecDetectionResult { pairs }); | ||
} | ||
|
||
return res; | ||
} |
Oops, something went wrong.