artificialguybr commited on
Commit
9ab3033
1 Parent(s): ae4438b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -12,12 +12,6 @@ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
12
 
13
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
14
 
15
- def make_prediction(prompt, max_tokens=None, temperature=None, top_p=None, top_k=None, repetition_penalty=None):
16
- input_ids = tokenizer.encode(prompt, return_tensors="pt")
17
- out = model.generate(input_ids, max_length=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty)
18
- text = tokenizer.decode(out[0], skip_special_tokens=True)
19
- yield text
20
-
21
  def clear_chat(chat_history_state, chat_message):
22
  chat_history_state = []
23
  chat_message = ''
@@ -28,13 +22,21 @@ def user(message, history):
28
  history.append([message, ""])
29
  return "", history
30
 
31
- def regenerate(_chatbot, _task_history):
32
  if not _task_history:
33
  yield _chatbot
34
  return
35
- item = _task_history.pop(-1)
36
- _chatbot.pop(-1)
37
- yield from make_prediction(item[0]) # Assuming make_prediction is the function you want to use for regeneration
 
 
 
 
 
 
 
 
38
 
39
 
40
  def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
@@ -66,7 +68,7 @@ assistant
66
  )
67
 
68
  # Decodificar a saída
69
- decoded_output = tokenizer.decode(output[0])
70
  assistant_response = decoded_output.split('assistant')[-1].strip() # Pegar apenas a última resposta do assistente
71
 
72
  # Atualizar o histórico
@@ -134,7 +136,7 @@ with gr.Blocks() as demo:
134
  stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
135
 
136
  regen_btn.click(
137
- fn=regenerate, inputs=[chatbot, chat_history_state], outputs=[chatbot], queue=True
138
  )
139
 
140
  demo.queue(max_size=128, concurrency_count=2)
 
12
 
13
  BASE_SYSTEM_MESSAGE = "I carefully provide accurate, factual, thoughtful, nuanced answers and am brilliant at reasoning."
14
 
 
 
 
 
 
 
15
  def clear_chat(chat_history_state, chat_message):
16
  chat_history_state = []
17
  chat_message = ''
 
22
  history.append([message, ""])
23
  return "", history
24
 
25
+ def regenerate(_chatbot, _task_history, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty):
26
  if not _task_history:
27
  yield _chatbot
28
  return
29
+ # Remove the last bot message
30
+ last_message = _task_history[-1]
31
+ if last_message[1]: # Check if the bot actually sent a message last
32
+ last_message[1] = ""
33
+
34
+ # Regenerate the message using the chat function
35
+ new_history, _, _ = chat(_task_history, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty)
36
+
37
+ # Update the chatbot state
38
+ yield new_history
39
+
40
 
41
 
42
  def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
 
68
  )
69
 
70
  # Decodificar a saída
71
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
72
  assistant_response = decoded_output.split('assistant')[-1].strip() # Pegar apenas a última resposta do assistente
73
 
74
  # Atualizar o histórico
 
136
  stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
137
 
138
  regen_btn.click(
139
+ fn=regenerate, inputs=[chatbot, chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot], queue=True
140
  )
141
 
142
  demo.queue(max_size=128, concurrency_count=2)