File size: 2,957 Bytes
889d59b
 
 
 
 
 
1aa7d35
 
 
 
9c90ca7
58e687a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
81
82
83
84
---
license: apache-2.0
language:
- id
base_model:
- sarahlintang/mistral-indo-7b
pipeline_tag: text-generation
library_name: transformers
tags:
- mistral
- text-generation-inference
---
# CiptakerLM v1
Dataset used for Fine-Tuning: <a href="https://github.com/Willy030125/LLM_Ciptaker/blob/main/Notebook/Ciptaker-sft-data-preparation.ipynb">Ciptaker-sft-data-preparation.ipynb</a><br>
Base model: <a href="https://huggingface.co/sarahlintang/mistral-indo-7b">sarahlintang/mistral-indo-7b</a><br>
Trained on 1x3090 @ 24 epochs<br>

Train logs, metrics, and params: https://wandb.ai/willy030125/MistralCiptaker_v0.2_SFT/runs/c9so5vf8 <br>
Inference example using Colab T4: <a href="https://github.com/Willy030125/LLM_Ciptaker/blob/main/Notebook/CiptakerLM-fine-tune-inference.ipynb">CiptakerLM-fine-tune-inference.ipynb</a><br>
Eval results using Colab T4: <a href="https://github.com/Willy030125/LLM_Ciptaker/blob/main/Notebook/CiptakerLM-fin-tune-eval.ipynb">CiptakerLM-fine-tune-eval.ipynb</a><br>

### Prompt template:
```
### Human: {Instruction} ### Assistant: {response}
```

### Usage example:
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoTokenizer, GenerationConfig

model_id = "Willy030125/CiptakerLM-v1"
device = "cuda" if torch.cuda.is_available() else "cpu"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id).to(device)

def create_instruction(instruction):
    prompt = f"### Human: {instruction} ### Assistant: "
    return prompt

def generate(
    instruction,
    max_new_tokens=2048,
    temperature=0.1,
    top_p=0.95,
    top_k=40,
    num_beams=4,
    **kwargs
):

    prompt = create_instruction(instruction)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].to(device)
    attention_mask = inputs["attention_mask"].to(device)
    generation_config = GenerationConfig(
        temperature=temperature,
        top_p=top_p,
        top_k=top_k,
        num_beams=num_beams,
        do_sample=True,
        **kwargs,
    )
    with torch.no_grad():
        generation_output = model.generate(
            input_ids=input_ids,
            attention_mask=attention_mask,
            generation_config=generation_config,
            pad_token_id=tokenizer.pad_token_id,
            eos_token_id=tokenizer.eos_token_id,
            return_dict_in_generate=True,
            output_scores=True,
            max_new_tokens=max_new_tokens,
            early_stopping=True
        )
    s = generation_output.sequences[0]
    output = tokenizer.decode(s, skip_special_tokens=True)
    return output.split("### Assistant:")[1].strip()

instruction = "Apa sanksi bagi pengusaha yang melanggar ketentuan dalam Pasal 42 ayat (2) tentang pekerja asing?"
print(generate(instruction))
```
Output:
> Pengusaha dapat dikenai sanksi pidana penjara 1-4 tahun dan/atau denda antara Rp100.000.000 hingga Rp400.000.000.