Tonic commited on
Commit
58cfbdd
1 Parent(s): e77ff91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -9,6 +9,7 @@ from tokenization_yi import YiTokenizer
9
  os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:120'
10
  model_id = "larryvrh/Yi-6B-200K-Llamafied"
11
  tokenizer_path = "./"
 
12
 
13
  DESCRIPTION = """
14
  # 👋🏻Welcome to 🙋🏻‍♂️Tonic's🧑🏻‍🚀YI-200K🚀"
@@ -19,13 +20,18 @@ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder
19
 
20
  tokenizer = AutoModelForCausalLM.from_pretrained(tokenizer_path)
21
  tokenizer = YiTokenizer.from_pretrained(tokenizer_path)
22
- model = AutoModelForCausalLM.from_pretrained(model_id=model_id, device_map="auto", torch_dtype="auto", trust_remote_code=True)
23
 
24
- def predict(message, max_new_tokens=4056, temperature=3.5, top_p=0.9, top_k=800, do_sample=False):
 
 
25
 
26
- prompt = message.strip()
27
- input_ids = tokenizer.encode(prompt, return_tensors='pt')
 
 
28
  input_ids = input_ids.to(model.device)
 
29
  response_ids = model.generate(
30
  input_ids,
31
  max_length=max_new_tokens + input_ids.shape[1],
@@ -35,28 +41,33 @@ def predict(message, max_new_tokens=4056, temperature=3.5, top_p=0.9, top_k=800,
35
  pad_token_id=tokenizer.eos_token_id,
36
  do_sample=do_sample
37
  )
 
38
  response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
39
- return [("bot", response)]
40
-
41
 
42
  with gr.Blocks(theme='ParityError/Anime') as demo:
43
  gr.Markdown(DESCRIPTION)
44
  with gr.Group():
45
- textbox = gr.Textbox(placeholder='Enter your message here', label='Your Message', lines=2)
 
 
 
46
  submit_button = gr.Button('Submit', variant='primary')
47
- chatbot = gr.Chatbot(label='TonicYi-6B-200K')
48
 
49
  with gr.Accordion(label='Advanced options', open=False):
50
- max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=55000, step=1, value=8000)
51
  temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.2)
52
  top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
53
  top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=900)
54
- do_sample_checkbox = gr.Checkbox(label='Disable for faster inference', value=False )
55
 
56
  submit_button.click(
57
  fn=predict,
58
- inputs=[textbox, max_new_tokens, temperature, top_p, top_k, do_sample_checkbox],
59
  outputs=chatbot
60
  )
61
 
 
 
 
62
  demo.launch()
 
9
  os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:120'
10
  model_id = "larryvrh/Yi-6B-200K-Llamafied"
11
  tokenizer_path = "./"
12
+ eos_token_id = 7
13
 
14
  DESCRIPTION = """
15
  # 👋🏻Welcome to 🙋🏻‍♂️Tonic's🧑🏻‍🚀YI-200K🚀"
 
20
 
21
  tokenizer = AutoModelForCausalLM.from_pretrained(tokenizer_path)
22
  tokenizer = YiTokenizer.from_pretrained(tokenizer_path)
23
+ model = AutoModelForCausalLM.from_pretrained(model_id=tokenizer_path, device_map="auto", torch_dtype="auto", trust_remote_code=True)
24
 
25
+ def format_prompt(user_message, system_message="I am YiTonic, an AI language model created by Tonic-AI. I am a cautious assistant. I carefully follow instructions. I am helpful and harmless and I follow ethical guidelines and promote positive behavior."):
26
+ prompt = f"<|im_start|>assistant\n{self.system_message}<|im_end|>\n<|im_start|>\nuser\n{user_message}<|im_end|>\nassistant\n"
27
+ return prompt
28
 
29
+ def predict(message, system_message, max_new_tokens=4056, temperature=3.5, top_p=0.9, top_k=800, do_sample=False):
30
+ formatted_prompt = format_prompt(message, system_message)
31
+
32
+ input_ids = tokenizer.encode(formatted_prompt, return_tensors='pt')
33
  input_ids = input_ids.to(model.device)
34
+
35
  response_ids = model.generate(
36
  input_ids,
37
  max_length=max_new_tokens + input_ids.shape[1],
 
41
  pad_token_id=tokenizer.eos_token_id,
42
  do_sample=do_sample
43
  )
44
+
45
  response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
46
+ return [("bot", response)]
 
47
 
48
  with gr.Blocks(theme='ParityError/Anime') as demo:
49
  gr.Markdown(DESCRIPTION)
50
  with gr.Group():
51
+ textbox = gr.Textbox(placeholder='Your Message Here', label='Your Message', lines=2)
52
+ system_prompt = gr.Textbox(placeholder='Provide a System Prompt In The First Person', label='System Prompt', lines=2, value="You are YiTonic, an AI language model created by Tonic-AI. You are a cautious assistant. You carefully follow instructions. You are helpful and harmless and you follow ethical guidelines and promote positive behavior.")
53
+
54
+ with gr.Group():
55
  submit_button = gr.Button('Submit', variant='primary')
 
56
 
57
  with gr.Accordion(label='Advanced options', open=False):
58
+ max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=55000, step=1, value=4056)
59
  temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.2)
60
  top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
61
  top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=900)
62
+ do_sample_checkbox = gr.Checkbox(label='Disable for faster inference', value=False)
63
 
64
  submit_button.click(
65
  fn=predict,
66
+ inputs=[textbox, system_prompt, max_new_tokens, temperature, top_p, top_k, do_sample_checkbox],
67
  outputs=chatbot
68
  )
69
 
70
+ with gr.Group():
71
+ chatbot = gr.Chatbot(label='TonicYi-6B-200K')
72
+
73
  demo.launch()