File size: 3,702 Bytes
7218e93
 
788ec8d
7218e93
 
788ec8d
7218e93
 
 
 
788ec8d
7218e93
 
 
788ec8d
7218e93
788ec8d
7218e93
 
 
 
 
 
 
 
 
 
030066b
7218e93
 
 
 
 
 
 
61ca66a
7218e93
 
 
 
 
 
 
 
 
61ca66a
7218e93
 
 
 
 
 
 
 
 
 
61ca66a
7218e93
 
 
 
 
 
 
 
 
 
 
61ca66a
030066b
7218e93
 
 
 
 
 
 
 
 
 
 
 
 
 
61ca66a
7218e93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788ec8d
7218e93
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
from peft import PeftModel
import torch
import transformers
import gradio as gr

MODEL = "decapoda-research/llama-7b-hf"
LORA_WEIGHTS = "tloen/alpaca-lora-7b"
device = "cpu"
print(f"Model device = {device}", flush=True)

tokenizer = LlamaTokenizer.from_pretrained(MODEL)
model = LlamaForCausalLM.from_pretrained(MODEL, device_map={"": device}, low_cpu_mem_usage=True)
model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device},)

model.eval()

def generate_prompt(instruction, input=None):
    if input:
        return f""" Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
        
        ### Instruction: {instruction}
        
        ### Input: {input}
        
        ### Response:
        """

    else:
        return f""" Below is an instruction that describes a task.  Write a response that appropriately completes the request.
        
        ### Instruction: {instruction}
        
        ### Response:
        """

def eval_prompt(
        instruction: str,
        input = None,
        temparature = 0.7,
        top_p = 0.75,
        top_k = 40,
        num_beams = 3,
        max_new_tokens = 128,
        **kwargs):

        prompt = generate_prompt(instruction, input)
        inputs = tokenizer(prompt, return_tensor = 'pt')
        input_ids = inputs['input_ids']
        generation_config = GenerationConfig(
            temparatue = temparature,
            top_p = top_p,
            top_k = top_k,
            num_beams = num_beams,
            repetition_penalty = 1.17,
            ** 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.sequence[0]
            response = tokenizer.decode(s)
            return response.split("### Response: ")[1].strip()


# def run_app():
    # g = gr.Interface(
    #     fn = eval_prompt,
    #     inputs = [
    #         gr.components.Textbox(
    #             lines = 2, label = 'Instruction', placeholder= "Enter an instruction here."),
    #         gr.components.Textbox(lines = 2, label = 'Input', placeholder = "Add an input here.")
    #     ],
    #     outputs = [ gr.inputs.Textbox(lines = 5, label = 'Output') ],
    #     title = 'Alpaca Demo'
    # )
    #
    # g.queue(concurrency_count=1)
    # g.launch(share=True, debug=True)

if __name__ == "__main__":
    # testing code for readme
    for instruction in [
        # "Tell me about alpacas.",
        # "Tell me about the president of Mexico in 2019.",
        # "Tell me about the king of France in 2019.",
        # "List all Canadian provinces in alphabetical order.",
        # "Write a Python program that prints the first 10 Fibonacci numbers.",
        # "Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.",
        "Tell me five words that rhyme with 'shock'.",
        "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
        # "Count up from 1 to 500.",
    ]:
        print("Instruction:", instruction)
        print("Response:", eval_prompt(instruction))
        print()

    ## Run the actual gradio app
    # run_app()