prince-canuma commited on
Commit
9a546a6
1 Parent(s): b1d5ece

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +10 -47
README.md CHANGED
@@ -23,7 +23,7 @@ inference: false
23
  To use this model, install the required packages:
24
 
25
  ```bash
26
- pip install -U mistral-common transformers torch
27
  ```
28
 
29
  ## Usage Example
@@ -31,62 +31,25 @@ pip install -U mistral-common transformers torch
31
  Here's a Python script demonstrating how to use the model for chat completion:
32
 
33
  ```python
34
- import torch
35
- from pathlib import Path
36
- from transformers import AutoModelForCausalLM
37
- from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
38
- from mistral_common.tokens.tokenizers.tekken import SpecialTokenPolicy
39
- from mistral_common.protocol.instruct.messages import UserMessage
40
- from mistral_common.protocol.instruct.request import ChatCompletionRequest
41
- from huggingface_hub import snapshot_download
42
-
43
- def get_model_path(path_or_hf_repo: str, revision: str = None) -> Path:
44
- """Ensures the model is available locally, downloading if necessary."""
45
- model_path = Path(path_or_hf_repo)
46
- if not model_path.exists():
47
- model_path = Path(
48
- snapshot_download(
49
- repo_id=path_or_hf_repo,
50
- revision=revision,
51
- allow_patterns=[
52
- "*.json",
53
- "*.safetensors",
54
- "*.py",
55
- "tokenizer.model",
56
- "*.tiktoken",
57
- "*.txt",
58
- ],
59
- resume_download=True,
60
- )
61
- )
62
- return model_path
63
-
64
- def load_chat_request(message: str) -> ChatCompletionRequest:
65
- """Creates a chat completion request with a user message."""
66
- return ChatCompletionRequest(messages=[UserMessage(content=message)])
67
 
68
  # Model setup
69
  model_name = "prince-canuma/Ministral-8B-Instruct-2410-HF"
70
  model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
71
- model_path = get_model_path(model_name)
72
-
73
- tokenizer = MistralTokenizer.from_file(f"{model_path}/tokenizer.json")
74
- tekken = tokenizer.instruct_tokenizer.tokenizer
75
- tekken.special_token_policy = SpecialTokenPolicy.IGNORE
76
 
77
  # Chat interaction
78
- user_message = "Tell me a short story about a robot learning to paint."
79
- completion_request = load_chat_request(user_message)
80
- tokens = tokenizer.encode_chat_completion(completion_request).tokens
81
- input_ids = torch.tensor(tokens).unsqueeze(0)
82
 
83
  # Generate response
84
- output = model.generate(input_ids, max_new_tokens=500, temperature=0.7, do_sample=True)
85
- response = tokenizer.decode(output[0][input_ids.shape[1]:].tolist())
86
 
87
- print("User:", user_message)
88
  print("Model:", response)
89
-
90
  ```
91
 
92
  ## Model Details
 
23
  To use this model, install the required packages:
24
 
25
  ```bash
26
+ pip install -U transformers
27
  ```
28
 
29
  ## Usage Example
 
31
  Here's a Python script demonstrating how to use the model for chat completion:
32
 
33
  ```python
34
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Model setup
37
  model_name = "prince-canuma/Ministral-8B-Instruct-2410-HF"
38
  model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
39
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
40
 
41
  # Chat interaction
42
+ prompt = "Tell me a short story about a robot learning to paint."
43
+ messages = [{"role": "user", "content": prompt}]
44
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
45
+ input_ids = tokenizer(text, return_tensors="pt").to(model.device)
46
 
47
  # Generate response
48
+ output = model.generate(**input_ids, max_new_tokens=500, temperature=0.7, do_sample=True)
49
+ response = tokenizer.decode(output[0][input_ids.input_ids.shape[1]:])
50
 
51
+ print("User:", prompt)
52
  print("Model:", response)
 
53
  ```
54
 
55
  ## Model Details