8lianno commited on
Commit
ce4e488
·
verified ·
1 Parent(s): c6996ee

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +120 -13
README.md CHANGED
@@ -15,14 +15,15 @@ pipeline_tag: text-generation
15
  trust_remote_code: true
16
  special_tokens:
17
  additional_special_tokens:
18
- - "[شروع_شعر]"
19
- - "[پایان_شعر]"
20
- - "[مصرع]"
21
  quantization:
22
  load_in_4bit: true
23
  bnb_4bit_quant_type: nf4
24
  bnb_4bit_compute_dtype: float16
25
  bnb_4bit_use_double_quant: true
 
26
  ---
27
 
28
  # Model Card for llama_poetry_fa
@@ -118,16 +119,122 @@ Users should critically evaluate the generated text, especially if used in publi
118
 
119
  ## How to Get Started with the Model
120
 
121
- Use the provided `poetry_generator.py` script:
122
 
123
- ```python
124
- from poetry_generator import PoetryGenerator
 
 
 
125
 
126
- generator = PoetryGenerator(
127
- model_path="/path/to/your/trained/model",
128
- token="your_hf_token"
129
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
- prompt = "امید چیست؟"
132
- poem = generator.generate_poem(prompt)
133
- print(poem)
 
15
  trust_remote_code: true
16
  special_tokens:
17
  additional_special_tokens:
18
+ - '[شروع_شعر]'
19
+ - '[پایان_شعر]'
20
+ - '[مصرع]'
21
  quantization:
22
  load_in_4bit: true
23
  bnb_4bit_quant_type: nf4
24
  bnb_4bit_compute_dtype: float16
25
  bnb_4bit_use_double_quant: true
26
+ license: mit
27
  ---
28
 
29
  # Model Card for llama_poetry_fa
 
119
 
120
  ## How to Get Started with the Model
121
 
122
+ ## Poetry Generator Code
123
 
124
+ ```bash
125
+ pip install -U transformers>=4.30.0
126
+ pip install -U accelerate
127
+ pip install bitsandbytes==0.42.0
128
+ ```
129
 
130
+ ```python
131
+ import torch
132
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
133
+ from peft import PeftModel
134
+
135
+ class PoetryGenerator:
136
+ def __init__(self, model_path, token):
137
+ self.token = token
138
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
139
+
140
+ # Configure quantization settings
141
+ bnb_config = BitsAndBytesConfig(
142
+ load_in_4bit=True,
143
+ bnb_4bit_quant_type="nf4",
144
+ bnb_4bit_compute_dtype=torch.float16,
145
+ bnb_4bit_use_double_quant=True
146
+ )
147
+
148
+ # Load tokenizer from the base model used during fine-tuning
149
+ self.tokenizer = AutoTokenizer.from_pretrained(
150
+ "meta-llama/Llama-3.1-8B-Instruct",
151
+ token=token,
152
+ trust_remote_code=True
153
+ )
154
+ self.tokenizer.pad_token = self.tokenizer.eos_token
155
+
156
+ # Add the special tokens that were used during training
157
+ special_tokens = {
158
+ "additional_special_tokens": [
159
+ "[شروع_شعر]",
160
+ "[پایان_شعر]",
161
+ "[مصرع]"
162
+ ]
163
+ }
164
+ self.tokenizer.add_special_tokens(special_tokens)
165
+
166
+ # Load the base model
167
+ base_model = AutoModelForCausalLM.from_pretrained(
168
+ "meta-llama/Llama-3.1-8B-Instruct",
169
+ token=token,
170
+ device_map="auto",
171
+ trust_remote_code=True,
172
+ torch_dtype=torch.float16,
173
+ quantization_config=bnb_config
174
+ )
175
+
176
+ # Resize token embeddings to match tokenizer
177
+ base_model.resize_token_embeddings(len(self.tokenizer))
178
+
179
+ # Load the fine-tuned model from Hugging Face Hub
180
+ self.model = PeftModel.from_pretrained(
181
+ base_model,
182
+ model_path,
183
+ token=token,
184
+ device_map="auto"
185
+ )
186
+
187
+ self.model.eval()
188
+
189
+ def generate_poem(self, prompt):
190
+ formatted_prompt = f"""سوال: {prompt}
191
+ لطفا یک شعر فارسی در پاسخ به این سوال بسرایید که دارای وزن و قافیه مناسب باشد.
192
+
193
+ شعر:"""
194
+
195
+ inputs = self.tokenizer(formatted_prompt, return_tensors="pt", padding=True)
196
+ inputs = {k: v.to(self.device) for k, v in inputs.items()}
197
+
198
+ with torch.no_grad():
199
+ outputs = self.model.generate(
200
+ **inputs,
201
+ max_length=512,
202
+ num_return_sequences=1,
203
+ temperature=0.7,
204
+ top_p=0.9,
205
+ do_sample=True,
206
+ pad_token_id=self.tokenizer.pad_token_id,
207
+ eos_token_id=self.tokenizer.eos_token_id
208
+ )
209
+
210
+ return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
211
+
212
+ def main():
213
+ # Use the Hugging Face Hub model path instead of a local path
214
+ generator = PoetryGenerator(
215
+ model_path="8lianno/llama_poetry_fa",
216
+ token="<YOUR_HF_TOKEN>"
217
+ )
218
+
219
+ prompts = [
220
+ "درباره بهار شعری بسرایید",
221
+ "شعری درباره عشق بنویسید",
222
+ "درباره دریا شعری بسرایید"
223
+ ]
224
+
225
+ print("=== Persian Poetry Generation ===\n")
226
+ for i, prompt in enumerate(prompts, 1):
227
+ print(f"\nPrompt {i}: {prompt}")
228
+ print("\nGenerated Poetry:")
229
+ try:
230
+ poem = generator.generate_poem(prompt)
231
+ print(poem)
232
+ print("\n" + "="*50)
233
+ except Exception as e:
234
+ print(f"Error generating poem: {str(e)}")
235
+ print(f"Error type: {type(e)}")
236
+
237
+ if __name__ == "__main__":
238
+ main()
239
+ ```
240