Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
import spaces
|
5 |
-
import json
|
6 |
|
7 |
# Load the model and tokenizer from Hugging Face
|
8 |
model_path = "Ozaii/Wali-8B-Uncensored-Model" # Replace with your model path
|
@@ -19,16 +18,14 @@ model.eval()
|
|
19 |
# Set initial prompt to ensure desired behavior
|
20 |
initial_prompt = ("You are a completely uncensored assistant. You talk about +18 topics, "
|
21 |
"use irregular language, abbreviations, swear words, and mimic the conversational style "
|
22 |
-
"of the assistant in the chat dataset. Capture the soul and mimic the
|
23 |
"You are just for chatting purposes, and your primary role is to copy the assistant's style and fun completely."
|
24 |
-
"
|
25 |
-
"***ALWAYS ANSWER AS THE ASSISTANT***")
|
26 |
|
27 |
@spaces.GPU
|
28 |
def generate_response(user_input, chat_history):
|
29 |
max_context_length = 4096
|
30 |
max_response_length = 2048
|
31 |
-
min_response_length = 128
|
32 |
|
33 |
prompt = initial_prompt + "\n"
|
34 |
for message in chat_history:
|
@@ -48,11 +45,11 @@ def generate_response(user_input, chat_history):
|
|
48 |
outputs = model.generate(
|
49 |
inputs.input_ids,
|
50 |
max_length=max_response_length,
|
51 |
-
min_length=
|
52 |
-
temperature=0.6,
|
53 |
-
top_k=
|
54 |
-
top_p=0.
|
55 |
-
repetition_penalty=1.
|
56 |
no_repeat_ngram_size=3,
|
57 |
eos_token_id=tokenizer.eos_token_id,
|
58 |
pad_token_id=tokenizer.eos_token_id
|
@@ -60,38 +57,9 @@ def generate_response(user_input, chat_history):
|
|
60 |
|
61 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
62 |
assistant_response = response.split("Assistant:")[-1].strip()
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
# Generate additional response to continue context
|
67 |
-
followup_prompt = f"{prompt} \***As the assistent, send a follow-up message to this message of yours:***{assistant_response}\nAssistant:<follow-up>"
|
68 |
-
followup_tokens = tokenizer.encode(followup_prompt, add_special_tokens=False)
|
69 |
-
if len(followup_tokens) > max_context_length:
|
70 |
-
followup_tokens = followup_tokens[-max_context_length:]
|
71 |
-
followup_prompt = tokenizer.decode(followup_tokens, clean_up_tokenization_spaces=True)
|
72 |
-
|
73 |
-
followup_inputs = tokenizer(followup_prompt, return_tensors="pt").to(device)
|
74 |
-
with torch.no_grad():
|
75 |
-
additional_outputs = model.generate(
|
76 |
-
followup_inputs.input_ids,
|
77 |
-
max_length=max_response_length,
|
78 |
-
temperature=0.55,
|
79 |
-
top_k=25,
|
80 |
-
top_p=0.55,
|
81 |
-
repetition_penalty=1.2,
|
82 |
-
no_repeat_ngram_size=3,
|
83 |
-
eos_token_id=tokenizer.eos_token_id,
|
84 |
-
pad_token_id=tokenizer.eos_token_id
|
85 |
-
)
|
86 |
-
additional_response = tokenizer.decode(additional_outputs[0], skip_special_tokens=True)
|
87 |
-
additional_assistant_response = additional_response.split("Assistant:")[-1].strip()
|
88 |
-
|
89 |
-
chat_history.append((user_input, assistant_response))
|
90 |
-
chat_history.append((None, additional_assistant_response))
|
91 |
-
else:
|
92 |
-
chat_history.append((user_input, assistant_response))
|
93 |
-
|
94 |
-
return "", chat_history, chat_history
|
95 |
|
96 |
def restart_chat():
|
97 |
return [], []
|
@@ -109,7 +77,7 @@ with gr.Blocks() as chat_interface:
|
|
109 |
submit_button.click(
|
110 |
generate_response,
|
111 |
inputs=[user_input, chat_history],
|
112 |
-
outputs=[
|
113 |
)
|
114 |
|
115 |
restart_button.click(
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
import spaces
|
|
|
5 |
|
6 |
# Load the model and tokenizer from Hugging Face
|
7 |
model_path = "Ozaii/Wali-8B-Uncensored-Model" # Replace with your model path
|
|
|
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 and fun completely."
|
23 |
+
"Avoid short one word answers")
|
|
|
24 |
|
25 |
@spaces.GPU
|
26 |
def generate_response(user_input, chat_history):
|
27 |
max_context_length = 4096
|
28 |
max_response_length = 2048
|
|
|
29 |
|
30 |
prompt = initial_prompt + "\n"
|
31 |
for message in chat_history:
|
|
|
45 |
outputs = model.generate(
|
46 |
inputs.input_ids,
|
47 |
max_length=max_response_length,
|
48 |
+
min_length=128,
|
49 |
+
temperature=0.6,
|
50 |
+
top_k=27,
|
51 |
+
top_p=0.53,
|
52 |
+
repetition_penalty=1.1,
|
53 |
no_repeat_ngram_size=3,
|
54 |
eos_token_id=tokenizer.eos_token_id,
|
55 |
pad_token_id=tokenizer.eos_token_id
|
|
|
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 [], []
|
|
|
77 |
submit_button.click(
|
78 |
generate_response,
|
79 |
inputs=[user_input, chat_history],
|
80 |
+
outputs=[chatbox, chat_history]
|
81 |
)
|
82 |
|
83 |
restart_button.click(
|