-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPhi3mini.py
164 lines (128 loc) · 5.38 KB
/
Phi3mini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import torch
import os
import io
from io import BytesIO
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
class Phi3mini_4k_ModelLoader_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
}
}
RETURN_TYPES = ("PHI3", "TK")
RETURN_NAMES = ("Phi3mini_4k", "tokenizer")
FUNCTION = "load_model"
CATEGORY = "🏖️Phi3mini"
def load_model(self,):
model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3-mini-4k-instruct",
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
return model, tokenizer
class Phi3mini_4k_Zho:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("PHI3",),
"tokenizer": ("TK",),
"prompt": ("STRING", {"default": "What is the meaning of life?", "multiline": True}),
"system_instruction": ("STRING", {"default": "You are creating a prompt for Stable Diffusion to generate an image. First step: understand the input and generate a text prompt for the input. Second step: only respond in English with the prompt itself in phrase, but embellish it as needed but keep it under 200 tokens.", "multiline": True}),
"temperature": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("text",)
FUNCTION = "generate_content"
CATEGORY = "🏖️Phi3mini"
def generate_content(self, model, tokenizer, prompt, system_instruction, temperature):
messages = [
{"role": "system", "content": system_instruction},
{"role": "user", "content": prompt},
]
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": temperature,
"do_sample": False,
}
output = pipe(messages, **generation_args)
textoutput = output[0]['generated_text']
return (textoutput,)
class Phi3mini_4k_Chat_Zho:
def __init__(self):
self.chat_history = []
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("PHI3",),
"tokenizer": ("TK",),
"prompt": ("STRING", {"default": "What is the meaning of life?", "multiline": True}),
"system_instruction": ("STRING", {"default": "You are creating a prompt for Stable Diffusion to generate an image. First step: understand the input and generate a text prompt for the input. Second step: only respond in English with the prompt itself in phrase, but embellish it as needed but keep it under 200 tokens.", "multiline": True}),
"temperature": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("text",)
FUNCTION = "generate_content"
CATEGORY = "🏖️Phi3mini"
def phi_3(self, user_question, system_role):
messages = [{"role": "system", "content": system_role},
{"role": "user", "content": user_question}]
pipe = pipeline(
"text-generation",
model=self.model, # Use self.model instead of model
tokenizer=self.tokenizer, # Use self.tokenizer instead of tokenizer
)
generation_args = {
"max_new_tokens": 500,
"return_full_text": False,
"temperature": self.temperature,
"do_sample": False,
}
output = pipe(messages, **generation_args)
text_output = output[0]['generated_text']
return text_output
def generate_content(self, model, tokenizer, prompt, system_instruction, temperature):
# Store model, tokenizer, and temperature as instance variables
self.model = model
self.tokenizer = tokenizer
self.temperature = temperature
# Generate response and update chat history
response = self.phi_3(prompt, system_instruction)
self.chat_history.append({"role": "user", "content": prompt})
self.chat_history.append({"role": "system", "content": response})
# Format and return chat history
formatted_history = self.format_chat_history()
return (formatted_history,)
def format_chat_history(self):
formatted_history = []
for message in self.chat_history:
formatted_message = f"{message['role']}: {message['content']}"
formatted_history.append(formatted_message)
formatted_history.append("-" * 40) # Add a separator line
return "\n".join(formatted_history)
NODE_CLASS_MAPPINGS = {
"Phi3mini_4k_ModelLoader_Zho": Phi3mini_4k_ModelLoader_Zho,
"Phi3mini_4k_Zho": Phi3mini_4k_Zho,
"Phi3mini_4k_Chat_Zho": Phi3mini_4k_Chat_Zho,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Phi3mini_4k_ModelLoader_Zho": "🏖️Phi3mini 4k ModelLoader",
"Phi3mini_4k_Zho": "🏖️Phi3mini 4k",
"Phi3mini_4k_Chat_Zho": "🏖️Phi3mini 4k Chat",
}