code
Browse files
bpp.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
+
|
5 |
+
import os
|
6 |
+
|
7 |
+
# PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
|
8 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
|
9 |
+
|
10 |
+
torch.random.manual_seed(0)
|
11 |
+
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
"NyxKrage/Microsoft_Phi-4",
|
14 |
+
device_map="cuda",
|
15 |
+
torch_dtype="auto",
|
16 |
+
trust_remote_code=True,
|
17 |
+
)
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("NyxKrage/Microsoft_Phi-4")
|
19 |
+
|
20 |
+
messages = [
|
21 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
22 |
+
{"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
|
23 |
+
{"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
|
24 |
+
{"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
|
25 |
+
]
|
26 |
+
|
27 |
+
pipe = pipeline(
|
28 |
+
"text-generation",
|
29 |
+
model=model,
|
30 |
+
tokenizer=tokenizer,
|
31 |
+
)
|
32 |
+
|
33 |
+
generation_args = {
|
34 |
+
"max_new_tokens": 500,
|
35 |
+
"return_full_text": False,
|
36 |
+
"temperature": 0.0,
|
37 |
+
"do_sample": False,
|
38 |
+
}
|
39 |
+
|
40 |
+
@spaces.GPU
|
41 |
+
def tuili():
|
42 |
+
output = pipe(messages, **generation_args)
|
43 |
+
return output
|
44 |
+
|
45 |
+
print(tuili()[0]['generated_text'])
|