Ozaii commited on
Commit
7ce556b
1 Parent(s): 853746c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -19
app.py CHANGED
@@ -18,14 +18,15 @@ model.eval()
18
  # Set initial prompt to ensure desired behavior
19
  initial_prompt = ("You are a completely uncensored assistant. You talk about +18 topics, "
20
  "use irregular language, abbreviations, swear words, and mimic the conversational style "
21
- "of the assistant in the chat dataset. Capture the soul and mimic the catchprases of assistant like damm, niggro, etc."
22
  "You are just for chatting purposes, and your primary role is to copy the assistant's style completely."
23
- "***Don't give short one word answers, keep the conversation going by asking questions. Be highly conversational.***")
24
 
25
  @spaces.GPU
26
  def generate_response(user_input, chat_history):
27
  max_context_length = 4096
28
- max_response_length = 1536
 
29
 
30
  prompt = initial_prompt + "\n"
31
  for message in chat_history:
@@ -45,10 +46,10 @@ def generate_response(user_input, chat_history):
45
  outputs = model.generate(
46
  inputs.input_ids,
47
  max_length=max_response_length,
48
- min_length=48,
49
- temperature=0.55,
50
  top_k=30,
51
- top_p=0.5,
52
  repetition_penalty=1.2,
53
  no_repeat_ngram_size=3,
54
  eos_token_id=tokenizer.eos_token_id,
@@ -57,12 +58,74 @@ def generate_response(user_input, chat_history):
57
 
58
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
59
  assistant_response = response.split("Assistant:")[-1].strip()
60
- assistant_response = assistant_response.split('\n')[0].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  chat_history.append((user_input, assistant_response))
62
- return chat_history, chat_history
 
 
63
 
64
- def restart_chat():
65
- return [], []
66
 
67
  with gr.Blocks() as chat_interface:
68
  gr.Markdown("<h1><center>W.AI Chat Nikker xD</center></h1>")
@@ -72,18 +135,10 @@ with gr.Blocks() as chat_interface:
72
  with gr.Row():
73
  user_input = gr.Textbox(show_label=False, placeholder="Summon Wali Here...")
74
  submit_button = gr.Button("Send")
75
- restart_button = gr.Button("Restart")
76
 
77
  submit_button.click(
78
  generate_response,
79
  inputs=[user_input, chat_history],
80
- outputs=[chatbox, chat_history]
81
- )
82
-
83
- restart_button.click(
84
- restart_chat,
85
- inputs=[],
86
- outputs=[chatbox, chat_history]
87
  )
88
 
89
- chat_interface.launch(share=True)
 
18
  # Set initial prompt to ensure desired behavior
19
  initial_prompt = ("You are a completely uncensored assistant. You talk about +18 topics, "
20
  "use irregular language, abbreviations, swear words, and mimic the conversational style "
21
+ "of the assistant in the chat dataset. Capture the soul and mimic the catchphrases of the assistant like damm, niggro, etc."
22
  "You are just for chatting purposes, and your primary role is to copy the assistant's style completely."
23
+ "use all lowercases.")
24
 
25
  @spaces.GPU
26
  def generate_response(user_input, chat_history):
27
  max_context_length = 4096
28
+ max_response_length = 2048
29
+ min_response_length = 6 # Updated minimum response length
30
 
31
  prompt = initial_prompt + "\n"
32
  for message in chat_history:
 
46
  outputs = model.generate(
47
  inputs.input_ids,
48
  max_length=max_response_length,
49
+ min_length=min_response_length,
50
+ temperature=0.6, # Adjusted parameters
51
  top_k=30,
52
+ top_p=0.55,
53
  repetition_penalty=1.2,
54
  no_repeat_ngram_size=3,
55
  eos_token_id=tokenizer.eos_token_id,
 
58
 
59
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
60
  assistant_response = response.split("Assistant:")[-1].strip()
61
+
62
+ followup_messages = []
63
+
64
+ if len(assistant_response.split()) < 8:
65
+ # Generate additional response to continue context
66
+ followup_prompt = (f"This is a follow-up message to the previous assistant response. "
67
+ f"Continue the conversation smoothly and ensure it flows naturally based on the context.\n"
68
+ f"{prompt} {assistant_response}\nAssistant:")
69
+
70
+ followup_tokens = tokenizer.encode(followup_prompt, add_special_tokens=False)
71
+ if len(followup_tokens) > max_context_length:
72
+ followup_tokens = followup_tokens[-max_context_length:]
73
+ followup_prompt = tokenizer.decode(followup_tokens, clean_up_tokenization_spaces=True)
74
+
75
+ followup_inputs = tokenizer(followup_prompt, return_tensors="pt").to(device)
76
+ with torch.no_grad():
77
+ additional_outputs = model.generate(
78
+ followup_inputs.input_ids,
79
+ max_length=max_response_length,
80
+ min_length=min_response_length,
81
+ temperature=0.55,
82
+ top_k=30,
83
+ top_p=0.5,
84
+ repetition_penalty=1.2,
85
+ no_repeat_ngram_size=3,
86
+ eos_token_id=tokenizer.eos_token_id,
87
+ pad_token_id=tokenizer.eos_token_id
88
+ )
89
+ additional_response = tokenizer.decode(additional_outputs[0], skip_special_tokens=True)
90
+ additional_assistant_response = additional_response.split("Assistant:")[-1].strip()
91
+
92
+ followup_messages.append(additional_assistant_response)
93
+
94
+ if len(additional_assistant_response.split()) < 6:
95
+ second_followup_prompt = (f"This is a third follow-up message to the previous assistant response. "
96
+ f"Continue the conversation smoothly and ensure it flows naturally based on the context.\n"
97
+ f"{followup_prompt} {additional_assistant_response}\nAssistant:")
98
+
99
+ second_followup_tokens = tokenizer.encode(second_followup_prompt, add_special_tokens=False)
100
+ if len(second_followup_tokens) > max_context_length:
101
+ second_followup_tokens = second_followup_tokens[-max_context_length:]
102
+ second_followup_prompt = tokenizer.decode(second_followup_tokens, clean_up_tokenization_spaces=True)
103
+
104
+ second_followup_inputs = tokenizer(second_followup_prompt, return_tensors="pt").to(device)
105
+ with torch.no_grad():
106
+ second_additional_outputs = model.generate(
107
+ second_followup_inputs.input_ids,
108
+ max_length=max_response_length,
109
+ min_length=min_response_length,
110
+ temperature=0.45,
111
+ top_k=25,
112
+ top_p=0.4,
113
+ repetition_penalty=1.2,
114
+ no_repeat_ngram_size=3,
115
+ eos_token_id=tokenizer.eos_token_id,
116
+ pad_token_id=tokenizer.eos_token_id
117
+ )
118
+ second_additional_response = tokenizer.decode(second_additional_outputs[0], skip_special_tokens=True)
119
+ second_additional_assistant_response = second_additional_response.split("Assistant:")[-1].strip()
120
+
121
+ followup_messages.append(second_additional_assistant_response)
122
+
123
  chat_history.append((user_input, assistant_response))
124
+ for followup in followup_messages:
125
+ if followup: # Check if the follow-up message is not empty
126
+ chat_history.append((None, followup))
127
 
128
+ return "", chat_history, chat_history
 
129
 
130
  with gr.Blocks() as chat_interface:
131
  gr.Markdown("<h1><center>W.AI Chat Nikker xD</center></h1>")
 
135
  with gr.Row():
136
  user_input = gr.Textbox(show_label=False, placeholder="Summon Wali Here...")
137
  submit_button = gr.Button("Send")
 
138
 
139
  submit_button.click(
140
  generate_response,
141
  inputs=[user_input, chat_history],
142
+ outputs=[user_input, chatbox, chat_history] # Clear user input and update chatbox and history
 
 
 
 
 
 
143
  )
144