hysts HF staff commited on
Commit
64b35aa
1 Parent(s): 14f59ea
Files changed (2) hide show
  1. app.py +2 -4
  2. model.py +6 -0
app.py CHANGED
@@ -3,7 +3,7 @@ from typing import Iterator
3
  import gradio as gr
4
  import torch
5
 
6
- from model import get_prompt, run, tokenizer
7
 
8
  DEFAULT_SYSTEM_PROMPT = """\
9
  You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
@@ -86,9 +86,7 @@ def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
86
 
87
 
88
  def check_prompt_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
89
- prompt = get_prompt(message, chat_history, system_prompt)
90
- input_ids = tokenizer([prompt], return_tensors='np')['input_ids']
91
- input_token_length = input_ids.shape[-1]
92
  if input_token_length > MAX_INPUT_TOKEN_LENGTH:
93
  raise gr.Error(f'The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.')
94
 
 
3
  import gradio as gr
4
  import torch
5
 
6
+ from model import get_input_token_length, run
7
 
8
  DEFAULT_SYSTEM_PROMPT = """\
9
  You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
 
86
 
87
 
88
  def check_prompt_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
89
+ input_token_length = get_input_token_length(message, chat_history, system_prompt)
 
 
90
  if input_token_length > MAX_INPUT_TOKEN_LENGTH:
91
  raise gr.Error(f'The accumulated input is too long ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH}). Clear your chat history and try again.')
92
 
model.py CHANGED
@@ -26,6 +26,12 @@ def get_prompt(message: str, chat_history: list[tuple[str, str]],
26
  return ''.join(texts)
27
 
28
 
 
 
 
 
 
 
29
  def run(message: str,
30
  chat_history: list[tuple[str, str]],
31
  system_prompt: str,
 
26
  return ''.join(texts)
27
 
28
 
29
+ def get_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> int:
30
+ prompt = get_prompt(message, chat_history, system_prompt)
31
+ input_ids = tokenizer([prompt], return_tensors='np')['input_ids']
32
+ return input_ids.shape[-1]
33
+
34
+
35
  def run(message: str,
36
  chat_history: list[tuple[str, str]],
37
  system_prompt: str,