tloen commited on
Commit
26a281d
β€’
1 Parent(s): adeef47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -10
app.py CHANGED
@@ -10,33 +10,70 @@ from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
10
 
11
  tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
12
 
 
 
 
13
  if torch.cuda.is_available():
14
  device = "cuda"
15
  else:
16
  device = "cpu"
17
 
18
- model = LlamaForCausalLM.from_pretrained(
19
- "decapoda-research/llama-7b-hf",
20
- torch_dtype=torch.float16,
21
- device_map="auto",
22
- )
23
- model = PeftModel.from_pretrained(
24
- model, "tloen/alpaca-lora-7b", torch_dtype=torch.float16, force_download=True
25
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
28
  def generate_prompt(instruction, input=None):
29
  if input:
30
  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.
 
31
  ### Instruction:
32
  {instruction}
 
33
  ### Input:
34
  {input}
 
35
  ### Response:"""
36
  else:
37
  return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
 
38
  ### Instruction:
39
  {instruction}
 
40
  ### Response:"""
41
 
42
 
@@ -100,5 +137,26 @@ gr.Interface(
100
  )
101
  ],
102
  title="πŸ¦™πŸŒ² Alpaca-LoRA",
103
- description="Alpaca-LoRA is a 7B-parameter LLaMA model finetuned to follow instructions. It is trained on the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset, and makes use of the Huggingface LLaMA implementation. For more information, please visit [the project's website](https://github.com/tloen/alpaca-lora).",
104
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
12
 
13
+ BASE_MODEL = "decapoda-research/llama-7b-hf"
14
+ LORA_WEIGHTS = "tloen/alpaca-lora-7b"
15
+
16
  if torch.cuda.is_available():
17
  device = "cuda"
18
  else:
19
  device = "cpu"
20
 
21
+ try:
22
+ if torch.backends.mps.is_available():
23
+ device = "mps"
24
+ except:
25
+ pass
26
+
27
+ if device == "cuda":
28
+ model = LlamaForCausalLM.from_pretrained(
29
+ BASE_MODEL,
30
+ load_in_8bit=False,
31
+ torch_dtype=torch.float16,
32
+ device_map="auto",
33
+ )
34
+ model = PeftModel.from_pretrained(
35
+ model, LORA_WEIGHTS, torch_dtype=torch.float16, force_download=True
36
+ )
37
+ elif device == "mps":
38
+ model = LlamaForCausalLM.from_pretrained(
39
+ BASE_MODEL,
40
+ device_map={"": device},
41
+ torch_dtype=torch.float16,
42
+ )
43
+ model = PeftModel.from_pretrained(
44
+ model,
45
+ LORA_WEIGHTS,
46
+ device_map={"": device},
47
+ torch_dtype=torch.float16,
48
+ )
49
+ else:
50
+ model = LlamaForCausalLM.from_pretrained(
51
+ BASE_MODEL, device_map={"": device}, low_cpu_mem_usage=True
52
+ )
53
+ model = PeftModel.from_pretrained(
54
+ model,
55
+ LORA_WEIGHTS,
56
+ device_map={"": device},
57
+ )
58
 
59
 
60
  def generate_prompt(instruction, input=None):
61
  if input:
62
  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.
63
+
64
  ### Instruction:
65
  {instruction}
66
+
67
  ### Input:
68
  {input}
69
+
70
  ### Response:"""
71
  else:
72
  return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
73
+
74
  ### Instruction:
75
  {instruction}
76
+
77
  ### Response:"""
78
 
79
 
 
137
  )
138
  ],
139
  title="πŸ¦™πŸŒ² Alpaca-LoRA",
140
+ description="Alpaca-LoRA is a 7B-parameter LLaMA model finetuned to follow instructions. It is trained on the [Stanford Alpaca](https://github.com/tatsu-lab/stanford_alpaca) dataset and makes use of the Huggingface LLaMA implementation. For more information, please visit [the project's website](https://github.com/tloen/alpaca-lora).",
141
+ ).launch()
142
+
143
+ # Old testing code follows.
144
+
145
+ """
146
+ if __name__ == "__main__":
147
+ # testing code for readme
148
+ for instruction in [
149
+ "Tell me about alpacas.",
150
+ "Tell me about the president of Mexico in 2019.",
151
+ "Tell me about the king of France in 2019.",
152
+ "List all Canadian provinces in alphabetical order.",
153
+ "Write a Python program that prints the first 10 Fibonacci numbers.",
154
+ "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'.",
155
+ "Tell me five words that rhyme with 'shock'.",
156
+ "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
157
+ "Count up from 1 to 500.",
158
+ ]:
159
+ print("Instruction:", instruction)
160
+ print("Response:", evaluate(instruction))
161
+ print()
162
+ """