Solshine commited on
Commit
7165054
1 Parent(s): c5c3104

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +51 -13
README.md CHANGED
@@ -38,40 +38,78 @@ dtype: bfloat16
38
 
39
  ## 💻 Usage
40
 
41
- /still troubleshooting the jais family tokenizer deployment with trust remote code, in this merge/
42
 
43
  ```python
44
  !pip install -qU transformers accelerate
45
 
46
- from transformers import AutoTokenizer
47
- import transformers
48
  import torch
49
 
50
- model = "Solshine/Jais-590m-merged"
51
- messages = [{"role": "user", "content": "What is a large language model?"}]
 
52
 
53
- tokenizer = AutoTokenizer.from_pretrained(model, trust_remote_code=True)
 
54
 
55
- # Manually apply a basic chat template since it's not provided by the model
 
 
 
 
 
 
 
56
  def custom_chat_template(messages):
57
  chat_prompt = ""
58
  for message in messages:
59
  role = message["role"]
60
  content = message["content"]
61
  chat_prompt += f"{role}: {content}\n"
 
 
62
  return chat_prompt
63
 
 
64
  prompt = custom_chat_template(messages)
 
 
 
 
 
 
65
 
66
- # Trust Remote Code to use the unique tokenizer in the jais family series of models
67
- pipeline = transformers.pipeline(
 
 
 
 
68
  "text-generation",
69
  model=model,
 
 
70
  torch_dtype=torch.float16,
71
- device_map="auto",
72
- trust_remote_code=True,
73
  )
74
 
75
- outputs = pipeline(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
76
- print(outputs[0]["generated_text"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  ```
 
38
 
39
  ## 💻 Usage
40
 
41
+ /Due to the jais family tokenizer deployment with trust remote code, especially if handling Arabic, the following implementation is suggested for inferencing this merge model/
42
 
43
  ```python
44
  !pip install -qU transformers accelerate
45
 
46
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
47
  import torch
48
 
49
+ # Model and message setup
50
+ model_name = "Solshine/Jais-590m-merged"
51
+ user_message = "Explain how transformers work in machine learning" # This can be any user input
52
 
53
+ # Structure the message with role-content pairing for compatibility with Jais-chat format
54
+ messages = [{"role": "user", "content": user_message}]
55
 
56
+ # Initialize tokenizer with trust_remote_code for custom Arabic-English handling
57
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
58
+
59
+ # Check if tokenizer is valid
60
+ if tokenizer is None:
61
+ raise ValueError("Tokenizer initialization failed!")
62
+
63
+ # Custom chat template including assistant role
64
  def custom_chat_template(messages):
65
  chat_prompt = ""
66
  for message in messages:
67
  role = message["role"]
68
  content = message["content"]
69
  chat_prompt += f"{role}: {content}\n"
70
+ # Add assistant role to prompt the model's response
71
+ chat_prompt += "assistant:"
72
  return chat_prompt
73
 
74
+ # Generate the prompt
75
  prompt = custom_chat_template(messages)
76
+ print(f"Generated prompt:\n{prompt}")
77
+
78
+ # Initialize the model
79
+ model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
80
+ if model is None:
81
+ raise ValueError("Model initialization failed!")
82
 
83
+ # Move model to the appropriate device
84
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
85
+ model.to(device)
86
+
87
+ # Initialize the text generation pipeline
88
+ text_gen_pipeline = pipeline(
89
  "text-generation",
90
  model=model,
91
+ tokenizer=tokenizer,
92
+ device=device,
93
  torch_dtype=torch.float16,
94
+ trust_remote_code=True
 
95
  )
96
 
97
+ # Generate text
98
+ try:
99
+ outputs = text_gen_pipeline(
100
+ prompt,
101
+ max_new_tokens=256,
102
+ do_sample=True,
103
+ temperature=0.7,
104
+ top_k=50,
105
+ top_p=0.95,
106
+ pad_token_id=tokenizer.eos_token_id # Ensure proper stopping
107
+ )
108
+ # Extract and print the assistant's response
109
+ generated_text = outputs[0]["generated_text"]
110
+ assistant_response = generated_text.split("assistant:")[1].strip()
111
+ print(f"Assistant's response:\n{assistant_response}")
112
+ except Exception as e:
113
+ print(f"Error during text generation: {e}")
114
+
115
  ```