Commit ·
58c87e8
1
Parent(s): 9d83e90
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include <iostream>
|
| 2 |
+
#include <string>
|
| 3 |
+
#include <torch/torch.h>
|
| 4 |
+
#include <transformers/generation_utils.h>
|
| 5 |
+
|
| 6 |
+
int main() {
|
| 7 |
+
// Load the pre-trained GPT model
|
| 8 |
+
std::string modelPath = "path/to/pretrained/model";
|
| 9 |
+
torch::jit::script::Module model = torch::jit::load(modelPath);
|
| 10 |
+
|
| 11 |
+
// Set the device (CPU or GPU)
|
| 12 |
+
torch::Device device(torch::kCPU);
|
| 13 |
+
model.to(device);
|
| 14 |
+
|
| 15 |
+
// Initialize the tokenizer
|
| 16 |
+
std::string tokenizerPath = "path/to/tokenizer";
|
| 17 |
+
transformers::GPT2Tokenizer tokenizer(tokenizerPath);
|
| 18 |
+
|
| 19 |
+
// Start the conversation loop
|
| 20 |
+
std::string userMessage;
|
| 21 |
+
while (true) {
|
| 22 |
+
std::cout << "User: ";
|
| 23 |
+
std::getline(std::cin, userMessage);
|
| 24 |
+
|
| 25 |
+
// Tokenize the user's message
|
| 26 |
+
std::vector<std::string> tokens = tokenizer.tokenize(userMessage);
|
| 27 |
+
|
| 28 |
+
// Convert tokens to input tensor
|
| 29 |
+
torch::Tensor inputIds = tokenizer.convertTokensToTensor(tokens).to(device);
|
| 30 |
+
|
| 31 |
+
// Generate a response from the GPT model
|
| 32 |
+
torch::Tensor outputIds = transformers::generate(model, inputIds);
|
| 33 |
+
|
| 34 |
+
// Convert output tensor to tokens
|
| 35 |
+
std::vector<std::string> responseTokens = tokenizer.convertIdsToTokens(outputIds);
|
| 36 |
+
|
| 37 |
+
// Convert tokens to text
|
| 38 |
+
std::string responseText = tokenizer.convertTokensToText(responseTokens);
|
| 39 |
+
|
| 40 |
+
std::cout << "GPT: " << responseText << std::endl;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
return 0;
|
| 44 |
+
}
|