𝒜𝓁𝒾𝓇𝑒𝓏𝒶
commited on
Update generate.py
Browse files- generate.py +81 -79
generate.py
CHANGED
|
@@ -1,80 +1,82 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
|
| 3 |
-
seed = 0
|
| 4 |
-
|
| 5 |
-
def generate_text(model_data, input_text, max_new_token):
|
| 6 |
-
"""
|
| 7 |
-
Generate text using the given model and tokenizer.
|
| 8 |
-
"""
|
| 9 |
-
if "pipeline" in model_data:
|
| 10 |
-
# اگر مدل از pipeline پشتیبانی میکند
|
| 11 |
-
model_pipeline = model_data["pipeline"]
|
| 12 |
-
generated_text = model_pipeline(
|
| 13 |
-
input_text,
|
| 14 |
-
max_length=max_new_token + len(input_text.split()), # افزایش max_length
|
| 15 |
-
do_sample=False, # غیرفعال کردن نمونهگیری (حالت حریصانه)
|
| 16 |
-
truncation=True # فعال کردن truncation
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
seed = 0
|
| 4 |
+
|
| 5 |
+
def generate_text(model_data, input_text, max_new_token):
|
| 6 |
+
"""
|
| 7 |
+
Generate text using the given model and tokenizer.
|
| 8 |
+
"""
|
| 9 |
+
if "pipeline" in model_data:
|
| 10 |
+
# اگر مدل از pipeline پشتیبانی میکند
|
| 11 |
+
model_pipeline = model_data["pipeline"]
|
| 12 |
+
generated_text = model_pipeline(
|
| 13 |
+
input_text,
|
| 14 |
+
max_length=max_new_token + len(input_text.split()), # افزایش max_length
|
| 15 |
+
do_sample=False, # غیرفعال کردن نمونهگیری (حالت حریصانه)
|
| 16 |
+
truncation=True, # فعال کردن truncation
|
| 17 |
+
repetition_penalty=1.5,
|
| 18 |
+
no_repeat_ngram_size=3,
|
| 19 |
+
)[0]["generated_text"]
|
| 20 |
+
return generated_text
|
| 21 |
+
else:
|
| 22 |
+
# روش قدیمی برای مدلهایی که از pipeline پشتیبانی نمیکنند
|
| 23 |
+
model = model_data["model"]
|
| 24 |
+
tokenizer = model_data["tokenizer"]
|
| 25 |
+
|
| 26 |
+
if tokenizer.pad_token is None:
|
| 27 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 28 |
+
|
| 29 |
+
torch.manual_seed(seed)
|
| 30 |
+
torch.cuda.manual_seed_all(seed)
|
| 31 |
+
|
| 32 |
+
encodings = tokenizer(
|
| 33 |
+
input_text,
|
| 34 |
+
return_tensors="pt",
|
| 35 |
+
padding=True,
|
| 36 |
+
truncation=True, # فعال کردن truncation
|
| 37 |
+
max_length=512
|
| 38 |
+
)
|
| 39 |
+
input_ids = encodings.input_ids
|
| 40 |
+
attention_mask = encodings.attention_mask
|
| 41 |
+
|
| 42 |
+
outputs = model.generate(
|
| 43 |
+
input_ids=input_ids,
|
| 44 |
+
attention_mask=attention_mask,
|
| 45 |
+
max_new_tokens=max_new_token, # استفاده از max_new_tokens
|
| 46 |
+
do_sample=False, # غیرفعال کردن نمونهگیری (حالت حریصانه)
|
| 47 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 48 |
+
repetition_penalty=1.5,
|
| 49 |
+
no_repeat_ngram_size=3,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
def generate_code(model_data, prompt, max_new_tokens):
|
| 55 |
+
"""
|
| 56 |
+
Generate code based on the provided prompt using a code-specific model.
|
| 57 |
+
"""
|
| 58 |
+
model = model_data["model"]
|
| 59 |
+
tokenizer = model_data["tokenizer"]
|
| 60 |
+
|
| 61 |
+
# تنظیم seed برای خروجی ثابت
|
| 62 |
+
torch.manual_seed(seed)
|
| 63 |
+
torch.cuda.manual_seed_all(seed)
|
| 64 |
+
|
| 65 |
+
# توکنایز کردن ورودی
|
| 66 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
| 67 |
+
|
| 68 |
+
# ایجاد attention mask
|
| 69 |
+
attention_mask = torch.ones(input_ids.shape, device=input_ids.device)
|
| 70 |
+
|
| 71 |
+
# تولید کد
|
| 72 |
+
outputs = model.generate(
|
| 73 |
+
input_ids=input_ids,
|
| 74 |
+
attention_mask=attention_mask,
|
| 75 |
+
max_new_tokens=max_new_tokens,
|
| 76 |
+
do_sample=False, # غیرفعال کردن نمونهگیری (حالت حریصانه)
|
| 77 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 78 |
+
repetition_penalty=1.5,
|
| 79 |
+
no_repeat_ngram_size=3,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|