File size: 2,969 Bytes
419b7c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM
import torch
import gradio as gr


BASE_MODEL_NAME = "tiiuae/falcon-7b"
MODEL_NAME = "ohtaman/falcon-7b-kokkai2022-lora"

tokenizer = transformers.AutoTokenizer.from_pretrained(BASE_MODEL_NAME, torch_dtype=torch.bfloat16, trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_NAME, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
model = PeftModel.from_pretrained(base_model, MODEL_NAME)


def generate_prompt(question: str, questioner: str="", answerer: str=""):
    return f"""# question
  {questioner}

  {question}

  # answer
  {answerer}

"""

def evaluate(
    quetion: str,
    questioner: str="",
    answerer: str="",
    temperature: float=0.1,
    top_p: float=0.75,
    top_k: int=40,
    num_beams: int=4,
    repetition_penalty: float=1.05,
    outputs.sequences[0, input_length:-1]_tokens: int=256,
    **kwargs
):
    prompt = generate_prompt(question, questioner, answerer)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].to(model.device)
    n_input_tokens = input_ids.shape[1]

    generation_config = GenerationConfig(
        temperature=temperature,
        top_p=top_p,
        top_k=top_k,
        num_beams=num_beams,
        repetition_penalty=repetition_penalty,
        **kwargs,
    )
    with torch.no_grad():
        generation_output = model.generate(
            input_ids=input_ids,
            generation_config=generation_config,
            return_dict_in_generate=True,
            output_scores=True,
            max_new_tokens=max_new_tokens,
        )
    s = generation_output.sequences[0, n_input_tokens:-1]
    return tokenizer.decode(s)


g = gr.Interface(
    fn=evaluate,
    inputs=[
        gr.components.Textbox(lines=5, label="Question", placeholder="Question"),
        gr.components.Textbox(lines=1, label="Questioner", placeholder="Questioner"),
        gr.components.Textbox(lines=1, label="Answerer", placeholder="Answerer"),
        gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
        gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
        gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
        gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
        gr.components.Slider(minimum=0, maximum=2, step=0.01, value=1.05, label="Repetition Penalty"),
        gr.components.Slider(minimum=1, maximum=512, step=1, value=128, label="Max tokens"),
    ],
    outputs=[
        gr.inputs.Textbox(
            lines=5,
            label="Output",
        )
    ],
    title="🏛️: Kokkai 2022",
    description="falcon-7b-kokkai2022 is a 7B-parameter model trained on Japan's 2022 Diet proceedings using LoRA based on [tiiuae/falcon-7b](https://huggingface.co/tiiuae/falcon-7b).",
)
g.queue(concurrency_count=1)
g.launch()