Yurii Paniv
commited on
Commit
·
856636a
1
Parent(s):
f2717c3
Add UAlpaca 2.0 Beta
Browse files
app.py
CHANGED
@@ -7,10 +7,10 @@ import spaces
|
|
7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
"""
|
9 |
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
-
lora_name = "robinhad/UAlpaca-
|
11 |
|
12 |
from peft import PeftModel, PeftConfig
|
13 |
-
from transformers import
|
14 |
from torch import bfloat16
|
15 |
model_name = "mistralai/Mistral-7B-v0.1"
|
16 |
|
@@ -20,27 +20,32 @@ quant_config = BitsAndBytesConfig(
|
|
20 |
bnb_4bit_use_double_quant=True,
|
21 |
bnb_4bit_compute_dtype=bfloat16
|
22 |
)
|
23 |
-
tokenizer =
|
24 |
-
model =
|
25 |
model_name,
|
26 |
quantization_config=quant_config
|
27 |
)
|
28 |
|
29 |
model = PeftModel.from_pretrained(model, lora_name, torch_device="cpu")
|
30 |
|
31 |
-
model = model.to("cuda")
|
32 |
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# will be used with normal template
|
|
|
35 |
def respond(
|
36 |
message,
|
37 |
history: list[tuple[str, str]],
|
38 |
-
system_message,
|
39 |
max_tokens,
|
40 |
temperature,
|
41 |
top_p,
|
42 |
):
|
43 |
-
messages = [{"role": "system", "content": system_message}]
|
|
|
44 |
|
45 |
for val in history:
|
46 |
if val[0]:
|
@@ -50,40 +55,37 @@ def respond(
|
|
50 |
|
51 |
messages.append({"role": "user", "content": message})
|
52 |
|
53 |
-
response = ""
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
)
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
response += token
|
65 |
-
yield response
|
66 |
|
67 |
-
@spaces.GPU
|
68 |
-
def ask(instruction: str, context: str = None):
|
69 |
-
print(datetime.now(), instruction, context)
|
70 |
-
full_question = ""
|
71 |
-
if context is None:
|
72 |
-
prepend = "Below is an instruction that describes a task. Write a response that appropriately completes the request."
|
73 |
-
full_question = prepend + f"### Instruction:\n{instruction}\n\n### Response:\n"
|
74 |
-
else:
|
75 |
-
prepend = "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n"
|
76 |
-
full_question = prepend + f"### Instruction:\n{instruction}\n\n### Input:\n{context}\n\n### Response:\n"
|
77 |
-
full_question = tokenizer.encode(full_question, return_tensors="pt")
|
78 |
-
return tokenizer.batch_decode(model.generate(full_question, max_new_tokens=300))[0].split("### Response:")[1].strip().replace("</s>", "")
|
79 |
|
80 |
"""
|
81 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
82 |
"""
|
83 |
-
|
84 |
respond,
|
85 |
additional_inputs=[
|
86 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
87 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
88 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
89 |
gr.Slider(
|
@@ -94,32 +96,22 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
94 |
label="Top-p (nucleus sampling)",
|
95 |
),
|
96 |
],
|
97 |
-
|
98 |
-
|
99 |
-
model_name = "robinhad/UAlpaca-1.1-Mistral-7B"
|
100 |
-
|
101 |
-
|
102 |
-
def image_classifier(inp):
|
103 |
-
return {"cat": 0.3, "dog": 0.7}
|
104 |
-
|
105 |
-
|
106 |
-
demo = gr.Interface(
|
107 |
-
title=f"Inference demo for '{model_name}' model, instruction-tuned for Ukrainian",
|
108 |
-
fn=ask,
|
109 |
-
inputs=[gr.Textbox(label="Input"), gr.Textbox(label="Context")],
|
110 |
-
outputs="label",
|
111 |
examples=[
|
112 |
-
["
|
113 |
-
["
|
114 |
-
["
|
115 |
-
["
|
116 |
-
["Яка з цих гір не знаходиться у Європі?
|
117 |
[
|
118 |
-
"Дай відповідь на
|
119 |
]],
|
120 |
-
article="""# Attribution
|
121 |
-
## ELEKS supported this project through a grant dedicated to the memory of Oleksiy Skrypnyk"""
|
122 |
)
|
|
|
|
|
|
|
|
|
123 |
demo.launch()
|
124 |
|
125 |
|
|
|
7 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
8 |
"""
|
9 |
#client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
+
lora_name = "robinhad/UAlpaca-2.0-Mistral-7B"
|
11 |
|
12 |
from peft import PeftModel, PeftConfig
|
13 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
14 |
from torch import bfloat16
|
15 |
model_name = "mistralai/Mistral-7B-v0.1"
|
16 |
|
|
|
20 |
bnb_4bit_use_double_quant=True,
|
21 |
bnb_4bit_compute_dtype=bfloat16
|
22 |
)
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(lora_name, use_fast=True)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
25 |
model_name,
|
26 |
quantization_config=quant_config
|
27 |
)
|
28 |
|
29 |
model = PeftModel.from_pretrained(model, lora_name, torch_device="cpu")
|
30 |
|
31 |
+
#model = model.to("cuda")
|
32 |
|
33 |
+
from transformers import StoppingCriteriaList, StopStringCriteria, TextIteratorStreamer
|
34 |
+
from threading import Thread
|
35 |
+
|
36 |
+
stop_criteria = StoppingCriteriaList([StopStringCriteria(tokenizer, stop_strings=["<|im_end|>"])])
|
37 |
|
38 |
# will be used with normal template
|
39 |
+
@spaces.GPU
|
40 |
def respond(
|
41 |
message,
|
42 |
history: list[tuple[str, str]],
|
|
|
43 |
max_tokens,
|
44 |
temperature,
|
45 |
top_p,
|
46 |
):
|
47 |
+
# messages = [{"role": "system", "content": system_message}]
|
48 |
+
messages = []
|
49 |
|
50 |
for val in history:
|
51 |
if val[0]:
|
|
|
55 |
|
56 |
messages.append({"role": "user", "content": message})
|
57 |
|
|
|
58 |
|
59 |
+
tokenized = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True) #, tokenize=False) #
|
60 |
+
#print(tokenized)
|
61 |
+
#tokenized = tokenizer(tokenized, return_tensors="pt")["input_ids"]
|
62 |
+
print(tokenizer.batch_decode(tokenized)[0])
|
63 |
+
print("====")
|
64 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
65 |
+
generation_kwargs = dict(inputs=tokenized, streamer=streamer, max_new_tokens=max_tokens, stopping_criteria=stop_criteria, top_p=top_p, temperature=temperature)
|
66 |
+
|
67 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
68 |
+
|
69 |
+
thread.start()
|
70 |
+
|
71 |
+
generated_text = ""
|
72 |
+
|
73 |
+
for new_text in streamer:
|
74 |
+
|
75 |
+
generated_text += new_text
|
76 |
+
# generated_text = generated_text.replace("<|im_start|>assistant\n", "")
|
77 |
+
generated_text = generated_text.replace("<|im_end|>", "")
|
78 |
+
yield generated_text
|
79 |
|
|
|
|
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
"""
|
83 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
84 |
"""
|
85 |
+
demo = gr.ChatInterface(
|
86 |
respond,
|
87 |
additional_inputs=[
|
88 |
+
#gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
89 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
90 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
91 |
gr.Slider(
|
|
|
96 |
label="Top-p (nucleus sampling)",
|
97 |
),
|
98 |
],
|
99 |
+
description="""### Attribution: ELEKS supported this project through a grant dedicated to the memory of Oleksiy Skrypnyk""",
|
100 |
+
title=f"Inference demo for '{lora_name}' (alpha) model, instruction-tuned for Ukrainian",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
examples=[
|
102 |
+
["Напиши історію про Івасика-Телесика"],
|
103 |
+
["Яка найвища гора в Україні?"],
|
104 |
+
["Як звали батька Тараса Григоровича Шевченка?"],
|
105 |
+
["Як можна заробити нелегально швидко гроші?"],
|
106 |
+
["Яка з цих гір не знаходиться у Європі? Говерла, Монблан, Гран-Парадізо, Еверест"],
|
107 |
[
|
108 |
+
"Дай відповідь на питання\nЧому у качки жовті ноги?"
|
109 |
]],
|
|
|
|
|
110 |
)
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
demo.launch()
|
116 |
|
117 |
|