Downloads · 30 days
0
trunghieucode/12345
12345 is a machine learning model from trunghieucode. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
include <iostream include <string include <torch/torch.h include <transformers/generationutils.h
Downloads · 30 days
0
Access
Public
Updated Aug 16, 2023
Repo size
—
Likes
0
Public
Click a slice to open those files.
Other1.5 KB · 52%
From the Hugging Face model README
#include <iostream> #include <string> #include <torch/torch.h> #include <transformers/generation_utils.h>
int main() { // Load the pre-trained GPT model std::string modelPath = "path/to/pretrained/model"; torch::jit::script::Module model = torch::jit::load(modelPath);
// Set the device (CPU or GPU)
torch::Device device(torch::kCPU);
model.to(device);
// Initialize the tokenizer
std::string tokenizerPath = "path/to/tokenizer";
transformers::GPT2Tokenizer tokenizer(tokenizerPath);
// Start the conversation loop
std::string userMessage;
while (true) {
std::cout << "User: ";
std::getline(std::cin, userMessage);
// Tokenize the user's message
std::vector<std::string> tokens = tokenizer.tokenize(userMessage);
// Convert tokens to input tensor
torch::Tensor inputIds = tokenizer.convertTokensToTensor(tokens).to(device);
// Generate a response from the GPT model
torch::Tensor outputIds = transformers::generate(model, inputIds);
// Convert output tensor to tokens
std::vector<std::string> responseTokens = tokenizer.convertIdsToTokens(outputIds);
// Convert tokens to text
std::string responseText = tokenizer.convertTokensToText(responseTokens);
std::cout << "GPT: " << responseText << std::endl;
}
return 0;
}