Guilherme34 commited on
Commit
d5c292e
1 Parent(s): 8fac83c

Create chat.py

Browse files
Files changed (1) hide show
  1. chat.py +53 -0
chat.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from transformers import StoppingCriteria, StoppingCriteriaList
4
+ from threading import Thread
5
+
6
+ torch.set_default_device("cuda")
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(
9
+ "Guilherme34/Samanthav3-MIXTRALDOLPHIN-LORA",
10
+ trust_remote_code=True
11
+ )
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ "Guilherme34/Samanthav3-MIXTRALDOLPHIN-LORA",
14
+ torch_dtype='auto',
15
+ load_in_4bit=True,
16
+ low_cpu_mem_usage=True,
17
+ bnb_4bit_use_double_quant=True,
18
+ device_map='auto',
19
+ trust_remote_code=True
20
+ )
21
+
22
+ class StopOnTokens(StoppingCriteria):
23
+ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
24
+ stop_ids = [50256, 50295]
25
+ for stop_id in stop_ids:
26
+ if input_ids[0][-1] == stop_id:
27
+ return True
28
+ return False
29
+
30
+ def generate_response(message, history):
31
+ try:
32
+ history_transformer_format = history + [[message, ""]]
33
+ stop = StopOnTokens()
34
+
35
+ 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|>"
36
+ 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])
37
+ input_ids = tokenizer([messages], return_tensors="pt").to('cuda')
38
+ generate_kwargs = dict(
39
+ input_ids,
40
+ max_new_tokens=1024,
41
+ do_sample=True,
42
+ top_p=0.95,
43
+ top_k=50,
44
+ temperature=0.7,
45
+ num_beams=1,
46
+ stopping_criteria=StoppingCriteriaList([stop])
47
+ )
48
+ output = model.generate(**generate_kwargs)
49
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
50
+ return response
51
+ except Exception as e:
52
+ print("Exception encountered:", str(e))
53
+ return f"An Error occurred please try your question again"