Upload 2 files
Browse files- app.py +98 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from peft import PeftModel
|
| 2 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from torch.cuda import is_available
|
| 5 |
+
|
| 6 |
+
if is_available():
|
| 7 |
+
options = dict(
|
| 8 |
+
load_in_8bit=True,
|
| 9 |
+
device_map="auto",
|
| 10 |
+
)
|
| 11 |
+
else:
|
| 12 |
+
options = {}
|
| 13 |
+
|
| 14 |
+
tokenizer = LlamaTokenizer.from_pretrained("openlm-research/open_llama_7b")
|
| 15 |
+
model = LlamaForCausalLM.from_pretrained(
|
| 16 |
+
"openlm-research/open_llama_7b",
|
| 17 |
+
**options
|
| 18 |
+
)
|
| 19 |
+
model = PeftModel.from_pretrained(model, "robinhad/open_llama_7b_uk")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def generate_prompt(instruction, input=None, output=""):
|
| 23 |
+
if input:
|
| 24 |
+
return f"""Унизу надається інструкція, яка описує завдання разом із вхідними даними, які надають додатковий контекст. Напиши відповідь, яка правильно доповнює запит.
|
| 25 |
+
### Інструкція:
|
| 26 |
+
{instruction}
|
| 27 |
+
### Вхідні дані:
|
| 28 |
+
{input}
|
| 29 |
+
### Відповідь:
|
| 30 |
+
{output}"""
|
| 31 |
+
else:
|
| 32 |
+
return f"""Унизу надається інструкція, яка описує завдання. Напиши відповідь, яка правильно доповнює запит.
|
| 33 |
+
### Інструкція:
|
| 34 |
+
{instruction}
|
| 35 |
+
### Відповідь:
|
| 36 |
+
{output}"""
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
generation_config = GenerationConfig(
|
| 40 |
+
temperature=0.2,
|
| 41 |
+
top_p=0.75,
|
| 42 |
+
num_beams=4,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
def evaluate(instruction, input=None):
|
| 46 |
+
if input.strip() == "":
|
| 47 |
+
input = None
|
| 48 |
+
prompt = generate_prompt(instruction, input)
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 50 |
+
input_ids = inputs["input_ids"]
|
| 51 |
+
if is_available():
|
| 52 |
+
input_ids = input_ids.cuda()
|
| 53 |
+
generation_output = model.generate(
|
| 54 |
+
input_ids=input_ids,
|
| 55 |
+
generation_config=generation_config,
|
| 56 |
+
return_dict_in_generate=True,
|
| 57 |
+
output_scores=True,
|
| 58 |
+
max_new_tokens=64
|
| 59 |
+
)
|
| 60 |
+
for s in generation_output.sequences:
|
| 61 |
+
output = tokenizer.decode(s, skip_special_tokens=True)
|
| 62 |
+
print("============")
|
| 63 |
+
print(output)
|
| 64 |
+
return output.split("### Відповідь:")[1].strip()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
gr.Interface(
|
| 68 |
+
evaluate,
|
| 69 |
+
[
|
| 70 |
+
gr.inputs.Textbox(lines=5, label="Інструкція"),
|
| 71 |
+
gr.inputs.Textbox(lines=5, label="Вхідні дані (необов'язково)"),
|
| 72 |
+
],
|
| 73 |
+
gr.outputs.Textbox(label="Відповідь"),
|
| 74 |
+
title="Kruk",
|
| 75 |
+
description="Open Llama is a Ukrainian language model trained on the machine-translated Dolly dataset.",
|
| 76 |
+
examples=[
|
| 77 |
+
[
|
| 78 |
+
"Яка найвища гора в Україні?",
|
| 79 |
+
"",
|
| 80 |
+
],
|
| 81 |
+
[
|
| 82 |
+
"Розкажи історію про Івасика-Телесика.",
|
| 83 |
+
"",
|
| 84 |
+
],
|
| 85 |
+
[
|
| 86 |
+
"Яка з цих гір не знаходиться у Європі?",
|
| 87 |
+
"Говерла, Монблан, Гран-Парадізо, Еверест"
|
| 88 |
+
],
|
| 89 |
+
[
|
| 90 |
+
"Чому качки жовтоногі?",
|
| 91 |
+
"",
|
| 92 |
+
],
|
| 93 |
+
[
|
| 94 |
+
"Чому у качки жовті ноги?",
|
| 95 |
+
"",
|
| 96 |
+
],
|
| 97 |
+
]
|
| 98 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tqdm
|
| 2 |
+
datasets
|
| 3 |
+
transformers # for LLaMa support, should be available in 4.28.0
|
| 4 |
+
torch
|
| 5 |
+
peft
|
| 6 |
+
bitsandbytes
|
| 7 |
+
sentencepiece
|
| 8 |
+
tenacity
|
| 9 |
+
scipy # for bitsandbytes
|
| 10 |
+
gradio
|