Skip to content

Instantly share code, notes, and snippets.

@BytefishMedium
Created February 7, 2024 09:13
Show Gist options
  • Select an option

  • Save BytefishMedium/163c45264c9d10d54053ce84e6085832 to your computer and use it in GitHub Desktop.

Select an option

Save BytefishMedium/163c45264c9d10d54053ce84e6085832 to your computer and use it in GitHub Desktop.
# Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer
import torch
# Bug: ValueError: Tokenizer class LlamaTokenizer does not exist or is not currently imported.
# Solution: pip3 install sentencepiece
# Info: https://github.com/huggingface/transformers/issues/22222
# Bug: ImportError: cannot import name 'LlamaTokenizer' from 'transformers'
# Solution: pip3 install git+https://github.com/huggingface/transformers
# Info: https://stackoverflow.com/questions/75907910/importerror-cannot-import-name-llamatokenizer-from-transformers
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2", padding_side="left")
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
while True:
prompt = input("Input your prompt: ")
# https://stackoverflow.com/questions/74748116/huggingface-automodelforcasuallm-decoder-only-architecture-warning-even-after
input_ids = tokenizer.encode(tokenizer.eos_token + prompt, return_tensors="pt")
print('generating response...')
output = model.generate(input_ids, max_length=20, pad_token_id=tokenizer.eos_token_id)
decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
print("Response: ", decoded_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment