Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,44 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import torch
|
5 |
|
6 |
+
model_name = "Qwen/Qwen2.5-7B-Instruct"
|
|
|
7 |
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
model_name,
|
10 |
+
torch_dtype=torch.bfloat16,
|
11 |
+
device_map="auto"
|
12 |
+
)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
|
15 |
+
@spaces.GPU
|
16 |
+
def generate(prompt, history):
|
17 |
+
messages = [
|
18 |
+
{"role": "system", "content": "Je bent een vriendelijke, behulpzame assistent."},
|
19 |
+
{"role": "user", "content": prompt}
|
20 |
+
]
|
21 |
+
text = tokenizer.apply_chat_template(
|
22 |
+
messages,
|
23 |
+
tokenize=False,
|
24 |
+
add_generation_prompt=True
|
25 |
+
)
|
26 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
27 |
+
|
28 |
+
generated_ids = model.generate(
|
29 |
+
**model_inputs,
|
30 |
+
max_new_tokens=512
|
31 |
+
)
|
32 |
+
generated_ids = [
|
33 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
34 |
+
]
|
35 |
+
|
36 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
37 |
+
return response
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
chat_interface = gr.ChatInterface(
|
42 |
+
fn=generate,
|
43 |
+
)
|
44 |
+
chat_interface.launch(share=True)
|