tanyuzhou commited on
Commit
6b9222f
โ€ข
1 Parent(s): 99cd0ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -5,7 +5,6 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import spaces
6
 
7
  tokenizer = AutoTokenizer.from_pretrained("Rorical/0-roleplay", trust_remote_code=True)
8
- tokenizer.chat_template = "{% for message in messages %}{{'<|im_start|>' + ((message['role'] + '\n') if message['role'] != '' else '') + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}"
9
  tokenizer.add_special_tokens({"bos_token": tokenizer.eos_token})
10
  tokenizer.bos_token_id = tokenizer.eos_token_id
11
 
@@ -14,19 +13,22 @@ tokenizer.bos_token_id = tokenizer.eos_token_id
14
  def respond(
15
  message,
16
  history: list[tuple[str, str]],
 
 
17
  system_message,
18
  max_tokens,
19
  temperature,
20
  top_p,
21
  ):
22
  model = AutoModelForCausalLM.from_pretrained("Rorical/0-roleplay", return_dict=True, trust_remote_code=True)
23
-
 
24
  # Construct the messages for the chat
25
  messages = [{"role": "", "content": system_message}]
26
  for user_message, bot_response in history:
27
- messages.append({"role": "่€ๅธˆ", "content": user_message}) # Assuming the user is "่€ๅธˆ"
28
- messages.append({"role": "ๆ˜Ÿ้‡Ž", "content": bot_response}) # Assuming the bot is "ๆ˜Ÿ้‡Ž"
29
- messages.append({"role": "่€ๅธˆ", "content": message}) # Append the latest user message
30
 
31
  # Tokenize and prepare inputs
32
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
@@ -75,7 +77,8 @@ prompt = """# ่ง’่‰ฒๆ‰ฎๆผ”
75
  demo = gr.ChatInterface(
76
  respond,
77
  additional_inputs=[
78
- gr.Textbox
 
79
  gr.Textbox(value=prompt, label="System message", lines=5),
80
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
81
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
5
  import spaces
6
 
7
  tokenizer = AutoTokenizer.from_pretrained("Rorical/0-roleplay", trust_remote_code=True)
 
8
  tokenizer.add_special_tokens({"bos_token": tokenizer.eos_token})
9
  tokenizer.bos_token_id = tokenizer.eos_token_id
10
 
 
13
  def respond(
14
  message,
15
  history: list[tuple[str, str]],
16
+ user_name,
17
+ bot_name,
18
  system_message,
19
  max_tokens,
20
  temperature,
21
  top_p,
22
  ):
23
  model = AutoModelForCausalLM.from_pretrained("Rorical/0-roleplay", return_dict=True, trust_remote_code=True)
24
+ tokenizer.chat_template = "{% for message in messages %}{{'<|im_start|>' + ((message['role'] + '\n') if message['role'] != '' else '') + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>" + bot_name + "\n' }}{% endif %}" # Be careful that this model used custom chat template.
25
+
26
  # Construct the messages for the chat
27
  messages = [{"role": "", "content": system_message}]
28
  for user_message, bot_response in history:
29
+ messages.append({"role": user_name, "content": user_message})
30
+ messages.append({"role": bot_name, "content": bot_response})
31
+ messages.append({"role": user_name, "content": message})
32
 
33
  # Tokenize and prepare inputs
34
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
 
77
  demo = gr.ChatInterface(
78
  respond,
79
  additional_inputs=[
80
+ gr.Textbox(value="่€ๅธˆ", label="User name", lines=1),
81
+ gr.Textbox(value="ๆ˜Ÿ้‡Ž", label="Bot name", lines=1),
82
  gr.Textbox(value=prompt, label="System message", lines=5),
83
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
84
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),