Tonic commited on
Commit
0d5c130
1 Parent(s): 3a6504f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -7,15 +7,10 @@ import gradio as gr
7
  import sentencepiece
8
 
9
  title = "Welcome to Tonic's 🐋🐳Orca-2-13B (in 8bit)!"
10
- description = "You can use [🐋🐳microsoft/Orca-2-13b](https://huggingface.co/microsoft/Orca-2-13b) via API using Gradio by scrolling down and clicking Use 'Via API' or privately by [cloning this space on huggingface](https://huggingface.co/spaces/Tonic1/TonicsOrca2?duplicate=true) . [Join my active builders' server on discord](https://discord.gg/VqTxc76K3u). Big thanks to the HuggingFace Organisation for the Community Grant."
11
 
12
- # os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:50'
13
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
14
  model_name = "microsoft/Orca-2-13b"
15
- # offload_folder = './model_weights'
16
-
17
- # if not os.path.exists(offload_folder):
18
- # os.makedirs(offload_folder)
19
 
20
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
21
  model = transformers.AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
@@ -25,9 +20,26 @@ class OrcaChatBot:
25
  self.model = model
26
  self.tokenizer = tokenizer
27
  self.system_message = system_message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def predict(self, user_message, temperature=0.4, max_new_tokens=70, top_p=0.99, repetition_penalty=1.9):
30
- prompt = f"<|im_start|>system\n{self.system_message}<|im_end|>\n<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant"
 
31
  inputs = self.tokenizer(prompt, return_tensors='pt', add_special_tokens=False)
32
  input_ids = inputs["input_ids"].to(self.model.device)
33
 
@@ -38,13 +50,13 @@ class OrcaChatBot:
38
  top_p=top_p,
39
  repetition_penalty=repetition_penalty,
40
  pad_token_id=self.tokenizer.eos_token_id,
41
- do_sample=True
42
- )
43
 
44
  response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
45
-
46
  return response
47
-
48
  Orca_bot = OrcaChatBot(model, tokenizer)
49
 
50
  def gradio_predict(user_message, system_message, max_new_tokens, temperature, top_p, repetition_penalty):
@@ -58,7 +70,7 @@ iface = gr.Interface(
58
  inputs=[
59
  gr.Textbox(label="Your Message", type="text", lines=3),
60
  gr.Textbox(label="Introduce a Character Here or Set a Scene (system prompt)", type="text", lines=2),
61
- gr.Slider(label="Max new tokens", value=125, minimum=25, maximum=256, step=1),
62
  gr.Slider(label="Temperature", value=0.1, minimum=0.05, maximum=1.0, step=0.05),
63
  gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.01, maximum=0.99, step=0.05),
64
  gr.Slider(label="Repetition penalty", value=1.9, minimum=1.0, maximum=2.0, step=0.05)
 
7
  import sentencepiece
8
 
9
  title = "Welcome to Tonic's 🐋🐳Orca-2-13B (in 8bit)!"
10
+ description = "You can use [🐋🐳microsoft/Orca-2-13b](https://huggingface.co/microsoft/Orca-2-13b) via API using Gradio by scrolling down and clicking Use 'Via API' or privately by [cloning this space on huggingface](https://huggingface.co/spaces/Tonic1/TonicsOrca2?duplicate=true) . [Join my active builders' server on discord](https://discord.gg/VqTxc76K3u). Let's build together! Big thanks to the HuggingFace Organisation for the Community Grant."
11
 
 
12
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
13
  model_name = "microsoft/Orca-2-13b"
 
 
 
 
14
 
15
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
16
  model = transformers.AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", load_in_8bit=True)
 
20
  self.model = model
21
  self.tokenizer = tokenizer
22
  self.system_message = system_message
23
+ self.conversation_history = []
24
+
25
+ def update_conversation_history(self, user_message, assistant_message):
26
+ self.conversation_history.append(("user", user_message))
27
+ self.conversation_history.append(("assistant", assistant_message))
28
+
29
+
30
+ def format_prompt(self):
31
+ prompt = f"<|im_start|>assistant\n{self.system_message}<|im_end|>\n"
32
+ for role, message in self.conversation_history:
33
+ if message.strip():
34
+ prompt += f"<|im_start|>{role}\n{message}<|im_end|>\n"
35
+ # if role == "assistant":
36
+ # prompt += f"<|im_end|>\n"
37
+ prompt += "<|im_start|> assistant\n"
38
+ return prompt
39
 
40
  def predict(self, user_message, temperature=0.4, max_new_tokens=70, top_p=0.99, repetition_penalty=1.9):
41
+ self.update_conversation_history(user_message, "")
42
+ prompt = self.format_prompt()
43
  inputs = self.tokenizer(prompt, return_tensors='pt', add_special_tokens=False)
44
  input_ids = inputs["input_ids"].to(self.model.device)
45
 
 
50
  top_p=top_p,
51
  repetition_penalty=repetition_penalty,
52
  pad_token_id=self.tokenizer.eos_token_id,
53
+ do_sample=True
54
+ )
55
 
56
  response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True)
57
+ self.update_conversation_history("", response)
58
  return response
59
+
60
  Orca_bot = OrcaChatBot(model, tokenizer)
61
 
62
  def gradio_predict(user_message, system_message, max_new_tokens, temperature, top_p, repetition_penalty):
 
70
  inputs=[
71
  gr.Textbox(label="Your Message", type="text", lines=3),
72
  gr.Textbox(label="Introduce a Character Here or Set a Scene (system prompt)", type="text", lines=2),
73
+ gr.Slider(label="Max new tokens", value=420, minimum=25, maximum=2056, step=1),
74
  gr.Slider(label="Temperature", value=0.1, minimum=0.05, maximum=1.0, step=0.05),
75
  gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.01, maximum=0.99, step=0.05),
76
  gr.Slider(label="Repetition penalty", value=1.9, minimum=1.0, maximum=2.0, step=0.05)