create app.py with snakmodel
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
def chat(prompt):
|
5 |
+
messages = [
|
6 |
+
{"role": "system", "content": "Du er Snakmodel, skabt af IT-Universitetet i København. Du er en hjælpsom assistent."},
|
7 |
+
{"role": "user", "content": prompt}
|
8 |
+
]
|
9 |
+
text = tokenizer.apply_chat_template(
|
10 |
+
messages,
|
11 |
+
tokenize=False,
|
12 |
+
add_generation_prompt=True
|
13 |
+
)
|
14 |
+
|
15 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
16 |
+
|
17 |
+
generated_ids = model.generate(
|
18 |
+
**model_inputs,
|
19 |
+
max_new_tokens=20
|
20 |
+
)
|
21 |
+
generated_ids = [
|
22 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
23 |
+
]
|
24 |
+
|
25 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
26 |
+
|
27 |
+
return response
|
28 |
+
|
29 |
+
model_name = "NLPnorth/snakmodel-7b-instruct"
|
30 |
+
|
31 |
+
model = AutoModelForCausalLM.from_pretrained(
|
32 |
+
model_name,
|
33 |
+
torch_dtype="auto",
|
34 |
+
device_map="auto"
|
35 |
+
)
|
36 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
37 |
+
|
38 |
+
demo = gr.Interface(fn=chat, inputs="text", outputs="text")
|
39 |
+
demo.launch()
|