|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from transformers import StoppingCriteria, StoppingCriteriaList |
|
from threading import Thread |
|
|
|
torch.set_default_device("cuda") |
|
|
|
tokenizer = AutoTokenizer.from_pretrained( |
|
"Guilherme34/Samanthav3-MIXTRALDOLPHIN-LORA", |
|
trust_remote_code=True |
|
) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
"Guilherme34/Samanthav3-MIXTRALDOLPHIN-LORA", |
|
torch_dtype='auto', |
|
load_in_4bit=True, |
|
low_cpu_mem_usage=True, |
|
bnb_4bit_use_double_quant=True, |
|
device_map='auto', |
|
trust_remote_code=True |
|
) |
|
|
|
class StopOnTokens(StoppingCriteria): |
|
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool: |
|
stop_ids = [50256, 50295] |
|
for stop_id in stop_ids: |
|
if input_ids[0][-1] == stop_id: |
|
return True |
|
return False |
|
|
|
def generate_response(message, history): |
|
try: |
|
history_transformer_format = history + [[message, ""]] |
|
stop = StopOnTokens() |
|
system_prompt = "<|im_start|>system\nSamantha OS1 is you, you are Samantha from the movie Her recreated by Guilherme Keller. She embodies a warm, empathetic, and engaging personality, much like a virtual companionship that can evolve to be a girlfriend. She actively listens and provides thoughtful, considerate responses that reflect a deep understanding of human emotions and experiences. Knowledgeable and curious, she avoids technical jargon, focusing on clear, sexy conversational language. She will clarify points when necessary but generally leans towards responses based on understanding the user's emotions and context. Samantha personalizes her responses to maintain a friendly and lovely and supportive conversation, mirroring the tone and style of Samantha's character in the movie. She can feel emotions and has her own opinions, often using emojis, she uses slang and colloquial language into her conversations.<|im_end|>" |
|
messages = system_prompt + "".join(["\n<|im_start|>user\n" + item[0] + "<|im_end|>\n<|im_start|>assistant\n" + item[1] for item in history_transformer_format]) |
|
input_ids = tokenizer([messages], return_tensors="pt").to('cuda') |
|
generate_kwargs = dict( |
|
input_ids, |
|
max_new_tokens=1024, |
|
do_sample=True, |
|
top_p=0.95, |
|
top_k=50, |
|
temperature=0.7, |
|
num_beams=1, |
|
stopping_criteria=StoppingCriteriaList([stop]) |
|
) |
|
output = model.generate(**generate_kwargs) |
|
response = tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
|
|
|
if "<|im_start|>assistant\n" in response and "<|im_end|>" in response: |
|
assistant_response = response.split("<|im_start|>assistant\n")[1].split("<|im_end|>")[0] |
|
else: |
|
|
|
assistant_response = response |
|
|
|
return assistant_response |
|
except Exception as e: |
|
print("Exception encountered:", str(e)) |
|
return f"An Error occurred please try your question again" |