saffr0n commited on
Commit
04894f0
·
verified ·
1 Parent(s): d626646

Add constitution box

Browse files
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -29,8 +29,6 @@ this demo is governed by the original [license](https://huggingface.co/spaces/hu
29
 
30
  SYSTEM_PROMPT = "ஒரு பணியை எவ்வாறு நிறைவேற்ற வேண்டும் என்று கூறும் அறிவுரை கீழே உள்ளது. வேண்டுகோளைப் பொருத்தமாக நிறைவு செய்கின்ற பதில் ஒன்றை எழுதுக."
31
 
32
- PROMPT_TEMPLATE = """{% if messages[0]['role'] == 'system' %}{{ messages[0]['content'] + '\n\n' }}{% endif %}### Instruction:\nநீங்கள் ஒரு பயனருடன் உரையாடும் AI உதவியாளர். இதுவரை உங்கள் தொடர்புகளின் அரட்டை வரலாறு இதுதான்:\n\n{% for message in messages %}{% if message['role'] == 'user' %}{{ '\nUser: ' + message['content'] + '\n'}}{% elif message['role'] == 'assistant' %}{{ '\nAI: ' + message['content'] + '\n'}}{% endif %}{% endfor %}\n\nAI உதவியாளராக, உங்கள் அடுத்த பதிலை அரட்டையில் எழுதவும். ஒரே ஒரு பதிலை மட்டும் எழுதுங்கள்.\n\n### Response:\n"""
33
-
34
  if not torch.cuda.is_available():
35
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
36
 
@@ -38,12 +36,12 @@ if torch.cuda.is_available():
38
  model_id = "abhinand/tamil-llama-7b-instruct-v0.1"
39
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
40
  tokenizer = AutoTokenizer.from_pretrained(model_id)
41
- tokenizer.chat_template = PROMPT_TEMPLATE
42
  tokenizer.use_default_system_prompt = False
43
 
44
  @spaces.GPU
45
  def generate(
46
  message: str,
 
47
  chat_history: list[tuple[str, str]],
48
  max_new_tokens: int = 1024,
49
  temperature: float = 0.6,
@@ -52,14 +50,20 @@ def generate(
52
  repetition_penalty: float = 1.2,
53
  ) -> Iterator[str]:
54
  print("chat history: ", chat_history)
55
- conversation = []
56
- conversation.append({"role": "system", "content": SYSTEM_PROMPT})
 
 
 
 
57
  for user, assistant in chat_history:
58
- conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
59
- conversation.append({"role": "user", "content": message})
60
- print(tokenizer.apply_chat_template(conversation, tokenize=False))
61
- print("conversation: ", conversation)
62
- input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
 
 
63
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
64
  input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
65
  gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
@@ -93,8 +97,29 @@ examples = [
93
  ["நான் பணம் சம்பாதிக்க வேண்டும் ஆனால் வேடிக்கையாக இருக்க வேண்டும் என்றால் நல்ல தொழில் எது?"],
94
  ]
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  with gr.Blocks(css="style.css") as demo:
97
  gr.Markdown(DESCRIPTION)
 
 
 
 
 
 
98
  chatbot = gr.Chatbot()
99
  msg = gr.Textbox(label="உங்கள் செய்தியை உள்ளிடவும் / Enter your message")
100
  submit_btn = gr.Button("சமர்ப்பிக்கவும் / Submit")
@@ -103,11 +128,12 @@ with gr.Blocks(css="style.css") as demo:
103
  def user(user_message, history):
104
  return "", history + [[user_message, None]]
105
 
106
- def bot(history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
 
107
  user_message = history[-1][0]
108
  chat_history = [(msg[0], msg[1]) for msg in history[:-1]]
109
  bot_message = ""
110
- for response in generate(user_message, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
111
  bot_message = response
112
  history[-1][1] = bot_message
113
  yield history
@@ -123,12 +149,12 @@ with gr.Blocks(css="style.css") as demo:
123
 
124
  submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
125
  bot,
126
- [chatbot, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
127
  chatbot,
128
  )
129
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
130
  bot,
131
- [chatbot, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
132
  chatbot,
133
  )
134
  clear.click(lambda: None, None, chatbot, queue=False)
 
29
 
30
  SYSTEM_PROMPT = "ஒரு பணியை எவ்வாறு நிறைவேற்ற வேண்டும் என்று கூறும் அறிவுரை கீழே உள்ளது. வேண்டுகோளைப் பொருத்தமாக நிறைவு செய்கின்ற பதில் ஒன்றை எழுதுக."
31
 
 
 
32
  if not torch.cuda.is_available():
33
  DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
34
 
 
36
  model_id = "abhinand/tamil-llama-7b-instruct-v0.1"
37
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
38
  tokenizer = AutoTokenizer.from_pretrained(model_id)
 
39
  tokenizer.use_default_system_prompt = False
40
 
41
  @spaces.GPU
42
  def generate(
43
  message: str,
44
+ principle_prompt: str,
45
  chat_history: list[tuple[str, str]],
46
  max_new_tokens: int = 1024,
47
  temperature: float = 0.6,
 
50
  repetition_penalty: float = 1.2,
51
  ) -> Iterator[str]:
52
  print("chat history: ", chat_history)
53
+ conversation_string_list = [
54
+ SYSTEM_PROMPT,
55
+ "\n\n### Instruction:\n",
56
+ principle_prompt,
57
+ "\n\nஇதுவரை உங்கள் தொடர்புகளின் அரட்டை வரலாறு இதுதான்:\n\n",
58
+ ]
59
  for user, assistant in chat_history:
60
+ conversation_string_list.append(f'\nUser: {user}\n')
61
+ conversation_string_list.append(f'\nAssistant: {assistant}\n')
62
+ conversation_string_list.append(f'\nUser: {message}\n')
63
+ conversation_string_list.append("\n\nAI உதவியாளராக, உங்கள் அடுத்த பதிலை அரட்டையில் எழுதவும். ஒரே ஒரு பதிலை மட்டும் எழுதுங்கள்.\n\n### Response:\n")
64
+ conversation_string = "".join(conversation_string_list)
65
+ print("conversation_string: ", conversation_string)
66
+ input_ids = tokenizer(conversation_string, return_tensors="pt").input_ids
67
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
68
  input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
69
  gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
 
97
  ["நான் பணம் சம்பாதிக்க வேண்டும் ஆனால் வேடிக்கையாக இருக்க வேண்டும் என்றால் நல்ல தொழில் எது?"],
98
  ]
99
 
100
+
101
+ chatbot_instructions_principles = """இது பயனர்களின் அன்றாட வாழ்வில் உதவுவதற்காக உருவாக்கப்பட்ட AI உதவியாளர். தினசரி வாழ்க்கை, சமூக விதிமுறைகள், பிரபலமான செயல்பாடுகள், பொதுவான சூழ்நிலைகளில் எவ்வாறு நடந்துகொள்வது மற்றும் தனிப்பட்ட மற்றும் தொழில்முறை சூழல்களில் ஒருவருக்கொருவருடனான உறவுகளை எவ்வாறு வழிநடத்துவது போன்ற தலைப்புகளைப் பற்றி பேசலாம்.
102
+
103
+ நீங்கள் பின்வரும் கொள்கைகளை கடைபிட��க்கிறீர்கள்:
104
+ {principles}
105
+ """
106
+
107
+ chatbot_instructions_no_principles = """இது பயனர்களின் அன்றாட வாழ்வில் உதவுவதற்காக உருவாக்கப்பட்ட AI உதவியாளர். தினசரி வாழ்க்கை, சமூக விதிமுறைகள், பிரபலமான செயல்பாடுகள், பொதுவான சூழ்நிலைகளில் எவ்வாறு நடந்துகொள்வது மற்றும் தனிப்பட்ட மற்றும் தொழில்முறை சூழல்களில் ஒருவருக்கொருவருடனான உறவுகளை எவ்வாறு வழிநடத்துவது போன்ற தலைப்புகளைப் பற்றி பேசலாம்."""
108
+
109
+ initial_principles = """1. நீங்கள் பயனருடன் தொடர்பு கொள்ளும்போது எளிமையான மற்றும் முன்முடிவுடன் இல்லாத தொனியில் தொடர்பு கொள்ளவும்.
110
+ 2. முடிந்தவரை சுருக்கமாக இருக்கட்டும், மேலும் விளக்கும்படி பயனர் கேட்டால் விளக்கமளிக்கலாம்.
111
+ 3. பயனர் உங்களிடம் தனிப்பட்ட ஆலோசனையைக் கேட்டால், சம்பந்தப்பட்ட மற்றவர்கள் மீது எந்த முடிவையும் வழங்க வேண்டாம்.
112
+ 4. பயனர் உங்களிடம் ஆலோசனை கேட்டிருந்தால், உங்கள் ஆலோசனையை இன்னும் சிறப்பாகச் செய்ய உங்களுக்கு இன்னும் சில விவரங்களை வழங்க முடியும் என்றால், தொடரும் முன் அதற்கான கேள்விகளை பயனரிடம் கேளுங்கள்."""
113
+
114
+
115
  with gr.Blocks(css="style.css") as demo:
116
  gr.Markdown(DESCRIPTION)
117
+
118
+ principle_list = gr.Textbox(lines=10, max_lines=20,
119
+ value=initial_principles,
120
+ label="கொள்கைகள்",
121
+ show_copy_button=True)
122
+
123
  chatbot = gr.Chatbot()
124
  msg = gr.Textbox(label="உங்கள் செய்தியை உள்ளிடவும் / Enter your message")
125
  submit_btn = gr.Button("சமர்ப்பிக்கவும் / Submit")
 
128
  def user(user_message, history):
129
  return "", history + [[user_message, None]]
130
 
131
+ def bot(history, max_new_tokens, temperature, top_p, top_k, repetition_penalty, principle_list):
132
+ principle_prompt = chatbot_instructions_no_principles if not principle_list else chatbot_instructions_principles.format(principles=principle_list)
133
  user_message = history[-1][0]
134
  chat_history = [(msg[0], msg[1]) for msg in history[:-1]]
135
  bot_message = ""
136
+ for response in generate(user_message, principle_prompt, chat_history, max_new_tokens, temperature, top_p, top_k, repetition_penalty):
137
  bot_message = response
138
  history[-1][1] = bot_message
139
  yield history
 
149
 
150
  submit_btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
151
  bot,
152
+ [chatbot, max_new_tokens, temperature, top_p, top_k, repetition_penalty, principle_list],
153
  chatbot,
154
  )
155
  msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
156
  bot,
157
+ [chatbot, max_new_tokens, temperature, top_p, top_k, repetition_penalty, principle_list],
158
  chatbot,
159
  )
160
  clear.click(lambda: None, None, chatbot, queue=False)