Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,32 @@
|
|
1 |
-
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("Vikhrmodels/Vikhr-7B-instruct_0.4",
|
11 |
+
device_map="auto",
|
12 |
+
attn_implementation="flash_attention_2",
|
13 |
+
torch_dtype=torch.bfloat16)
|
14 |
+
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained("Vikhrmodels/Vikhr-7B-instruct_0.4")
|
16 |
+
from transformers import AutoTokenizer, pipeline
|
17 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
18 |
+
prompts = [
|
19 |
+
"В чем разница между фруктом и овощем?",
|
20 |
+
"Годы жизни колмагорова?"]
|
21 |
+
|
22 |
+
def test_inference(prompt):
|
23 |
+
prompt = pipe.tokenizer.apply_chat_template([{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True)
|
24 |
+
print(prompt)
|
25 |
+
outputs = pipe(prompt, max_new_tokens=512, do_sample=True, num_beams=1, temperature=0.25, top_k=50, top_p=0.98, eos_token_id=79097)
|
26 |
+
return outputs[0]['generated_text'][len(prompt):].strip()
|
27 |
+
|
28 |
+
|
29 |
+
for prompt in prompts:
|
30 |
+
print(f" prompt:\n{prompt}")
|
31 |
+
print(f" response:\n{test_inference(prompt)}")
|
32 |
+
print("-"*50)
|