🚀 ruDialoGpt3-medium-finetuned-telegram
ruDialoGpt3-medium-finetuned-telegram 是一個基於俄語訓練,並在個人 Telegram 聊天記錄上微調的對話模型,能為俄語對話場景提供支持。
🚀 快速開始
DialoGPT 以俄語進行訓練,並在我的 Telegram 聊天記錄上進行了微調。
該模型由 sberbank-ai 創建,在俄語論壇上進行訓練(詳見 Grossmend 的模型)。你可以在 habr 上找到關於其訓練方式的信息(俄語)。我創建了一個簡單的流程,並在自己導出的 Telegram 聊天記錄(約 30MB 的 JSON 文件)上對該模型進行了微調。實際上,從 Telegram 獲取數據並微調模型非常容易。因此,我為此製作了一個 Colab 教程:https://colab.research.google.com/drive/1fnAVURjyZRK9VQg1Co_-SKUQnRES8l9R?usp=sharing
⚠️ 重要提示
由於數據的特殊性,託管推理 API 可能無法正常工作。
🤗你可以使用我的 Spaces 演示來嘗試這個模型🤗
💻 使用示例
基礎用法
checkpoint = "Kirili4ik/ruDialoGpt3-medium-finetuned-telegram"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint)
model.eval()
def get_length_param(text: str, tokenizer) -> str:
tokens_count = len(tokenizer.encode(text))
if tokens_count <= 15:
len_param = '1'
elif tokens_count <= 50:
len_param = '2'
elif tokens_count <= 256:
len_param = '3'
else:
len_param = '-'
return len_param
def get_user_param(text: dict, machine_name_in_chat: str) -> str:
if text['from'] == machine_name_in_chat:
return '1'
else:
return '0'
chat_history_ids = torch.zeros((1, 0), dtype=torch.int)
while True:
next_who = input("Who's phrase?\t")
if next_who == "H" or next_who == "Human":
input_user = input("===> Human: ")
new_user_input_ids = tokenizer.encode(f"|0|{get_length_param(input_user, tokenizer)}|" \
+ input_user + tokenizer.eos_token, return_tensors="pt")
chat_history_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
if next_who == "G" or next_who == "GPT":
next_len = input("Phrase len? 1/2/3/-\t")
new_user_input_ids = tokenizer.encode(f"|1|{next_len}|", return_tensors="pt")
chat_history_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
input_len = chat_history_ids.shape[-1]
chat_history_ids = model.generate(
chat_history_ids,
num_return_sequences=1,
max_length=512,
no_repeat_ngram_size=3,
do_sample=True,
top_k=50,
top_p=0.9,
temperature = 0.6,
mask_token_id=tokenizer.mask_token_id,
eos_token_id=tokenizer.eos_token_id,
unk_token_id=tokenizer.unk_token_id,
pad_token_id=tokenizer.pad_token_id,
device='cpu'
)
print(f"===> GPT-3: {tokenizer.decode(chat_history_ids[:, input_len:][0], skip_special_tokens=True)}")