naot97 commited on
Commit
dbbfa01
1 Parent(s): 3b9962d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel
3
+ import transformers
4
+ import gradio as gr
5
+
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained("bigscience/bloom-1b1")
9
+
10
+ BASE_MODEL = "bigscience/bloom-1b1"
11
+ LORA_WEIGHTS = "naot97/bloom1b1-zalo-test"
12
+
13
+ if torch.cuda.is_available():
14
+ device = "cuda"
15
+ else:
16
+ device = "cpu"
17
+
18
+ try:
19
+ if torch.backends.mps.is_available():
20
+ device = "mps"
21
+ except:
22
+ pass
23
+
24
+ if device == "cuda":
25
+ model = AutoModelForCausalLM.from_pretrained(
26
+ BASE_MODEL,
27
+ load_in_8bit=False,
28
+ torch_dtype=torch.float16,
29
+ device_map="auto",
30
+ )
31
+ model = PeftModel.from_pretrained(
32
+ model, LORA_WEIGHTS, torch_dtype=torch.float16, force_download=True
33
+ )
34
+ elif device == "mps":
35
+ model = LlamaForCausalLM.from_pretrained(
36
+ BASE_MODEL,
37
+ device_map={"": device},
38
+ torch_dtype=torch.float16,
39
+ )
40
+ model = PeftModel.from_pretrained(
41
+ model,
42
+ LORA_WEIGHTS,
43
+ device_map={"": device},
44
+ torch_dtype=torch.float16,
45
+ )
46
+ else:
47
+ model = AutoModelForCausalLM.from_pretrained(
48
+ BASE_MODEL, device_map={"": device}, low_cpu_mem_usage=True
49
+ )
50
+ model = PeftModel.from_pretrained(
51
+ model,
52
+ LORA_WEIGHTS,
53
+ device_map={"": device},
54
+ )
55
+
56
+
57
+ def generate_prompt(instruction, input=None):
58
+ if input:
59
+ 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.
60
+ ### Instruction:
61
+ {instruction}
62
+ ### Input:
63
+ {input}
64
+ ### Response:"""
65
+ else:
66
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
67
+ ### Instruction:
68
+ {instruction}
69
+ ### Response:"""
70
+
71
+ if device != "cpu":
72
+ model.half()
73
+ model.eval()
74
+ if torch.__version__ >= "2":
75
+ model = torch.compile(model)
76
+
77
+
78
+ def evaluate(
79
+ instruction,
80
+ input=None,
81
+ temperature=0.1,
82
+ top_p=0.75,
83
+ top_k=40,
84
+ num_beams=4,
85
+ max_new_tokens=128,
86
+ **kwargs,
87
+ ):
88
+ prompt = generate_prompt(instruction, input)
89
+ inputs = tokenizer(prompt, return_tensors="pt")
90
+ input_ids = inputs["input_ids"].to(device)
91
+ generation_config = GenerationConfig(
92
+ temperature=temperature,
93
+ top_p=top_p,
94
+ top_k=top_k,
95
+ num_beams=num_beams,
96
+ **kwargs,
97
+ )
98
+ with torch.no_grad():
99
+ generation_output = model.generate(
100
+ input_ids=input_ids,
101
+ generation_config=generation_config,
102
+ return_dict_in_generate=True,
103
+ output_scores=True,
104
+ max_new_tokens=max_new_tokens,
105
+ )
106
+ s = generation_output.sequences[0]
107
+ output = tokenizer.decode(s)
108
+ return output.split("### Response:")[1].strip()
109
+
110
+
111
+ g = gr.Interface(
112
+ fn=evaluate,
113
+ inputs=[
114
+ gr.components.Textbox(
115
+ lines=2, label="Instruction", placeholder="Tell me about alpacas."
116
+ ),
117
+ gr.components.Textbox(lines=2, label="Input", placeholder="none"),
118
+ gr.components.Slider(minimum=0, maximum=1, value=0.1, label="Temperature"),
119
+ gr.components.Slider(minimum=0, maximum=1, value=0.75, label="Top p"),
120
+ gr.components.Slider(minimum=0, maximum=100, step=1, value=40, label="Top k"),
121
+ gr.components.Slider(minimum=1, maximum=4, step=1, value=4, label="Beams"),
122
+ gr.components.Slider(
123
+ minimum=1, maximum=512, step=1, value=128, label="Max tokens"
124
+ ),
125
+ ],
126
+ outputs=[
127
+ gr.inputs.Textbox(
128
+ lines=5,
129
+ label="Output",
130
+ )
131
+ ],
132
+ title="bloom 1b1",
133
+ description="",
134
+ )
135
+ g.queue(concurrency_count=1)
136
+ g.launch()
137
+
138
+ # Old testing code follows.
139
+
140
+ """
141
+ if __name__ == "__main__":
142
+ # testing code for readme
143
+ for instruction in [
144
+ "Tell me about alpacas.",
145
+ "Tell me about the president of Mexico in 2019.",
146
+ "Tell me about the king of France in 2019.",
147
+ "List all Canadian provinces in alphabetical order.",
148
+ "Write a Python program that prints the first 10 Fibonacci numbers.",
149
+ "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'.",
150
+ "Tell me five words that rhyme with 'shock'.",
151
+ "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
152
+ "Count up from 1 to 500.",
153
+ ]:
154
+ print("Instruction:", instruction)
155
+ print("Response:", evaluate(instruction))
156
+ print()
157
+ """