sohojoe commited on
Commit
35d97c8
1 Parent(s): c58cbbc

prep to test on machine with gpu

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. chat_service.py +63 -0
  3. clip_transform.py +1 -1
  4. debug.py +22 -1
.gitignore CHANGED
@@ -1,3 +1,4 @@
1
 
2
  .env
3
  .DS_Store
 
 
1
 
2
  .env
3
  .DS_Store
4
+ __pycache__
chat_service.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+ # from huggingface_hub.inference_api import InferenceApi
6
+
7
+ class ChatService:
8
+ def __init__(self, api="huggingface", repo_id = "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5"):
9
+ self._api = api
10
+ self._device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
+
12
+ if self._api=="huggingface":
13
+ self._tokenizer = AutoTokenizer.from_pretrained(repo_id)
14
+ self._model = AutoModelForCausalLM.from_pretrained(repo_id,torch_dtype=torch.float16)
15
+ # self._model = AutoModelForCausalLM.from_pretrained(repo_id).half()
16
+ self._model = AutoModelForCausalLM.from_pretrained(repo_id)
17
+ self._model.eval().to(self._device)
18
+ else:
19
+ raise Exception(f"Unknown API: {self._api}")
20
+
21
+ self._system_prompt = "Below are a series of dialogues between various people and an AI assistant. The AI tries to be helpful, polite, honest, sophisticated, emotionally aware, and humble-but-knowledgeable. The assistant is happy to help with almost anything, and will do its best to understand exactly what is needed. It also tries to avoid giving false or misleading information, and it caveats when it isn't entirely sure about the right answer. That said, the assistant is practical and really does its best, and doesn't let caution get too much in the way of being useful.\n-----\n"
22
+ self._user_name = "<|prompter|>"
23
+ self._agent_name = "<|assistant|>"
24
+ self.reset()
25
+
26
+ def reset(self):
27
+ self._user_history = []
28
+ self._agent_history = []
29
+ self._full_history = self._user_history if self._user_history else ""
30
+
31
+
32
+ def _chat(self, prompt):
33
+ if self._api=="huggingface":
34
+ tokens = self._tokenizer.encode(prompt, return_tensors="pt", padding=True)
35
+ tokens = tokens.to(self._device)
36
+ outputs = self._model.generate(
37
+ tokens,
38
+ early_stopping=True,
39
+ max_new_tokens=200,
40
+ do_sample=True,
41
+ top_k=40,
42
+ temperature=1.0,
43
+ pad_token_id=self._tokenizer.eos_token_id,
44
+ )
45
+ agent_response = self._tokenizer.decode(outputs[0], truncate_before_pattern=[r"\n\n^#", "^'''", "\n\n\n"])
46
+
47
+ else:
48
+ raise Exception(f"API not implemented: {self._api}")
49
+ return agent_response
50
+
51
+ def chat(self, prompt):
52
+ if self._user_name:
53
+ self._full_history += f"{self._user_name}: {prompt}\n"
54
+ else:
55
+ self._full_history += f"{prompt}\n"
56
+ self._user_history.append(prompt)
57
+ agent_response = self._chat(self._full_history)
58
+ if self._agent_name:
59
+ self._full_history += f"{self._agent_name}: {agent_response}\n"
60
+ else:
61
+ self._full_history += f"{agent_response}\n"
62
+ self._agent_history.append(agent_response)
63
+ return agent_response
clip_transform.py CHANGED
@@ -26,7 +26,7 @@ class CLIPTransform:
26
  self._clip_model="ViT-L-14"
27
  self._pretrained='datacomp_xl_s13b_b90k'
28
 
29
- self.model, _, self.preprocess = open_clip.create_model_and_transforms(self._clip_model, pretrained=self._pretrained)
30
  self.tokenizer = open_clip.get_tokenizer(self._clip_model)
31
 
32
  print ("using device", self.device)
 
26
  self._clip_model="ViT-L-14"
27
  self._pretrained='datacomp_xl_s13b_b90k'
28
 
29
+ self.model, _, self.preprocess = open_clip.create_model_and_transforms(self._clip_model, pretrained=self._pretrained,device=self.device)
30
  self.tokenizer = open_clip.get_tokenizer(self._clip_model)
31
 
32
  print ("using device", self.device)
debug.py CHANGED
@@ -1,4 +1,25 @@
1
  from clip_transform import CLIPTransform
 
 
 
 
 
 
 
2
  clip_transform = CLIPTransform()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- print ("Initializing CLIP templates")
 
1
  from clip_transform import CLIPTransform
2
+ from chat_service import ChatService
3
+ from dotenv import load_dotenv
4
+
5
+
6
+ load_dotenv()
7
+
8
+ print ("Initializing CLIP templates")
9
  clip_transform = CLIPTransform()
10
+ print ("CLIP success")
11
+
12
+ print ("Initializing Chat")
13
+ chat_service = ChatService()
14
+ prompts = [
15
+ "hello, how are you today?",
16
+ "tell me about your showdow self?",
17
+ "hmm, interesting, tell me more about that.",
18
+ "wait, that is so interesting, what else?",
19
+ ]
20
+ for prompt in prompts:
21
+ print (f'prompt: "{prompt}"')
22
+ response = chat_service.chat(prompt)
23
+ print (f'response: "{response}"')
24
 
25
+ print ("Chat success")