Download main.py from LencoDigitexer/BASHORG: direct link, hf CLI and curl.
- Browser
- Download file 861 Bytes
-
https://huggingface.co/LencoDigitexer/BASHORG/resolve/main/main.py
- Command line
-
hf download hf://LencoDigitexer/BASHORG/main.py
-
curl -L -o main.py https://huggingface.co/LencoDigitexer/BASHORG/resolve/main/main.py
861 Bytes
| from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
| import torch | |
| DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| # Для наглядности будем работать с русскоязычной GPT от Сбера. | |
| # Ниже команды для загрузки и инициализации модели и токенизатора. | |
| model_name_or_path = "ai-forever/rugpt3medium_based_on_gpt2" | |
| tokenizer = GPT2Tokenizer.from_pretrained(model_name_or_path) | |
| model = GPT2LMHeadModel.from_pretrained(model_name_or_path).to(DEVICE) | |
| # prompt engineering for QA | |
| text = "Вопрос: 'Сколько будет 2+2?'\nОтвет:" | |
| input_ids = tokenizer.encode(text, return_tensors="pt").to(DEVICE) | |
| out = model.generate(input_ids, do_sample=False) | |
| generated_text = list(map(tokenizer.decode, out))[0] | |
| print(generated_text) | |