Spaces:
Runtime error
Runtime error
Darpan
commited on
Commit
•
b5160f5
1
Parent(s):
c8701af
Fixes for demo
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from peft import PeftModel
|
|
3 |
import torch
|
4 |
import transformers
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
MODEL = "decapoda-research/llama-7b-hf"
|
8 |
LORA_WEIGHTS = "tloen/alpaca-lora-7b"
|
@@ -10,43 +11,51 @@ device = "cpu"
|
|
10 |
print(f"Model device = {device}", flush=True)
|
11 |
|
12 |
tokenizer = LlamaTokenizer.from_pretrained(MODEL)
|
13 |
-
model = LlamaForCausalLM.from_pretrained(MODEL, device_map={"": device}, low_cpu_mem_usage=True)
|
14 |
-
model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device},)
|
15 |
|
16 |
model.eval()
|
17 |
|
18 |
-
def generate_prompt(
|
19 |
-
if
|
20 |
-
return f""" Below
|
21 |
|
22 |
-
### Instruction:
|
23 |
|
24 |
-
###
|
25 |
|
26 |
### Response:
|
27 |
"""
|
28 |
|
29 |
else:
|
30 |
-
return
|
31 |
|
32 |
-
###
|
33 |
|
34 |
### Response:
|
35 |
"""
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def eval_prompt(
|
38 |
-
|
39 |
-
|
40 |
temparature = 0.7,
|
41 |
top_p = 0.75,
|
42 |
top_k = 40,
|
43 |
-
num_beams =
|
44 |
max_new_tokens = 128,
|
45 |
**kwargs):
|
46 |
|
47 |
-
|
48 |
-
inputs = tokenizer(
|
49 |
-
input_ids = inputs[
|
50 |
generation_config = GenerationConfig(
|
51 |
temparatue = temparature,
|
52 |
top_p = top_p,
|
@@ -55,6 +64,7 @@ def eval_prompt(
|
|
55 |
repetition_penalty = 1.17,
|
56 |
** kwargs,)
|
57 |
|
|
|
58 |
with torch.no_grad():
|
59 |
generation_output = model.generate(
|
60 |
input_ids = input_ids,
|
@@ -63,10 +73,12 @@ def eval_prompt(
|
|
63 |
output_scores = True,
|
64 |
max_new_tokens = max_new_tokens,
|
65 |
)
|
66 |
-
s = generation_output.
|
67 |
response = tokenizer.decode(s)
|
68 |
-
|
69 |
-
|
|
|
|
|
70 |
|
71 |
# def run_app():
|
72 |
# g = gr.Interface(
|
@@ -84,21 +96,30 @@ def eval_prompt(
|
|
84 |
# g.launch(share=True, debug=True)
|
85 |
|
86 |
if __name__ == "__main__":
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
#
|
91 |
-
#
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
## Run the actual gradio app
|
104 |
# run_app()
|
|
|
3 |
import torch
|
4 |
import transformers
|
5 |
import gradio as gr
|
6 |
+
import time
|
7 |
|
8 |
MODEL = "decapoda-research/llama-7b-hf"
|
9 |
LORA_WEIGHTS = "tloen/alpaca-lora-7b"
|
|
|
11 |
print(f"Model device = {device}", flush=True)
|
12 |
|
13 |
tokenizer = LlamaTokenizer.from_pretrained(MODEL)
|
14 |
+
model = LlamaForCausalLM.from_pretrained(MODEL, device_map={"": device}, low_cpu_mem_usage=True, )
|
15 |
+
model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device}, torch_dtype=torch.float16)
|
16 |
|
17 |
model.eval()
|
18 |
|
19 |
+
def generate_prompt(input, history):
|
20 |
+
if not history:
|
21 |
+
return f""" Below A dialog, where User interacts with you - the AI.
|
22 |
|
23 |
+
### Instruction: AI is helpful, kind, obedient, honest, and knows its own limits.
|
24 |
|
25 |
+
### User: {input}
|
26 |
|
27 |
### Response:
|
28 |
"""
|
29 |
|
30 |
else:
|
31 |
+
return f"""{history}
|
32 |
|
33 |
+
### User: {input}
|
34 |
|
35 |
### Response:
|
36 |
"""
|
37 |
|
38 |
+
# else:
|
39 |
+
# return f""" Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
40 |
+
#
|
41 |
+
# ### Instruction: {instruction}
|
42 |
+
#
|
43 |
+
# ### Response:
|
44 |
+
# """
|
45 |
+
|
46 |
def eval_prompt(
|
47 |
+
input: str,
|
48 |
+
history = "",
|
49 |
temparature = 0.7,
|
50 |
top_p = 0.75,
|
51 |
top_k = 40,
|
52 |
+
num_beams = 1,
|
53 |
max_new_tokens = 128,
|
54 |
**kwargs):
|
55 |
|
56 |
+
history = generate_prompt(input, history)
|
57 |
+
inputs = tokenizer(history, return_tensors = "pt")
|
58 |
+
input_ids = inputs["input_ids"]
|
59 |
generation_config = GenerationConfig(
|
60 |
temparatue = temparature,
|
61 |
top_p = top_p,
|
|
|
64 |
repetition_penalty = 1.17,
|
65 |
** kwargs,)
|
66 |
|
67 |
+
# with torch.inference_mode():
|
68 |
with torch.no_grad():
|
69 |
generation_output = model.generate(
|
70 |
input_ids = input_ids,
|
|
|
73 |
output_scores = True,
|
74 |
max_new_tokens = max_new_tokens,
|
75 |
)
|
76 |
+
s = generation_output.sequences[0]
|
77 |
response = tokenizer.decode(s)
|
78 |
+
# print(response.split('### Response:')[-1].strip())
|
79 |
+
bot_response = response.split("### Response:")[-1].strip()
|
80 |
+
history += bot_response
|
81 |
+
return history, bot_response
|
82 |
|
83 |
# def run_app():
|
84 |
# g = gr.Interface(
|
|
|
96 |
# g.launch(share=True, debug=True)
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
+
history = ""
|
100 |
+
|
101 |
+
while True:
|
102 |
+
# testing code for readme
|
103 |
+
# for instruction in [
|
104 |
+
# "Tell me about alpacas.",
|
105 |
+
# "Tell me about the president of Mexico in 2019.",
|
106 |
+
# "Tell me about the king of France in 2019.",
|
107 |
+
# "List all Canadian provinces in alphabetical order.",
|
108 |
+
# "Write a Python program that prints the first 10 Fibonacci numbers.",
|
109 |
+
# "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'.",
|
110 |
+
# "Tell me five words that rhyme with 'shock'.",
|
111 |
+
# "Translate the sentence 'I have no mouth but I must scream' into Spanish.",
|
112 |
+
# "Count up from 1 to 500.",
|
113 |
+
# ]:
|
114 |
+
|
115 |
+
print("Input text here: ", end=' ')
|
116 |
+
user_input = input()
|
117 |
+
tick = time.time()
|
118 |
+
history, response = eval_prompt(user_input, history)
|
119 |
+
print(f"Bot: {response}")
|
120 |
+
print(f"Present history: {history}")
|
121 |
+
print(f"Inference time = {time.time() - tick} seconds")
|
122 |
+
print()
|
123 |
|
124 |
## Run the actual gradio app
|
125 |
# run_app()
|