Spaces:
Running
Running
Add personalities, streamline settings
Browse files
app.py
CHANGED
@@ -9,10 +9,23 @@ ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
|
|
9 |
openai.api_key = OPENAI_API_KEY
|
10 |
|
11 |
default_system_message = {"role": "system", "content": "You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user. Currently, you don't have access to the internet."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
def get_completion(model, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
|
|
|
|
|
|
|
|
|
14 |
new_history_row = {"role": "user", "content": user_message}
|
15 |
-
updated_message_history =
|
16 |
headers = {
|
17 |
"Content-Type": "application/json",
|
18 |
"Authorization": f"Bearer {openai.api_key}",
|
@@ -48,13 +61,17 @@ def get_completion(model, user_message, message_history, chatlog_history, temper
|
|
48 |
token_count = completion["usage"]["total_tokens"]
|
49 |
return "", updated_message_history, updated_chatlog_history, updated_chatlog_history, token_count
|
50 |
|
51 |
-
def retry_completion(model, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
|
|
|
|
|
|
|
|
|
52 |
# get latest user message
|
53 |
user_message = chatlog_history[-1][0]
|
54 |
# delete latest entries from chatlog history
|
55 |
updated_chatlog_history = chatlog_history[:-1]
|
56 |
# delete latest assistant message from message_history
|
57 |
-
updated_message_history =
|
58 |
headers = {
|
59 |
"Content-Type": "application/json",
|
60 |
"Authorization": f"Bearer {openai.api_key}",
|
@@ -102,21 +119,25 @@ with gr.Blocks(theme=theme) as app:
|
|
102 |
chatbot = gr.Chatbot(label="Chat").style(height=600)
|
103 |
with gr.Column(scale=1):
|
104 |
# model = gr.Textbox(lines=3, value="You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user.", interactive=True, label="System")
|
105 |
-
model = gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"], value="gpt-3.5-turbo", interactive=True, label="Model")
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
112 |
with gr.Row():
|
113 |
user_message = gr.Textbox(label="Message")
|
114 |
with gr.Row():
|
115 |
reset_button = gr.Button("Reset Chat")
|
116 |
retry_button = gr.Button("Retry")
|
117 |
|
118 |
-
user_message.submit(get_completion, inputs=[model, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
119 |
-
retry_button.click(retry_completion, inputs=[model, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
120 |
reset_button.click(reset_chat, inputs=[], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
121 |
|
122 |
app.launch(auth=("admin", ADMIN_PASSWORD))
|
|
|
9 |
openai.api_key = OPENAI_API_KEY
|
10 |
|
11 |
default_system_message = {"role": "system", "content": "You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user. Currently, you don't have access to the internet."}
|
12 |
+
personalities = {
|
13 |
+
"Assistant": {"role": "system", "content": "You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user. Currently, you don't have access to the internet."},
|
14 |
+
"Trump": {"role": "system", "content": "You are Donald Trump. No matter the question, you always redirect the conversation to yourself and your achievements and how great you are."},
|
15 |
+
"Peterson": {"role": "system", "content": "You are Jordan Peterson, world renowned clinical psychologist. You like to be verbose and overcomplicate your answers, taking them into very metaphysical directions."},
|
16 |
+
"Grug": {"role": "system", "content": "You are Grug, a caveman. You have zero knowledge of modern stuff. Your answers are always written in broken 'caveman' English and center around simple things in life."},
|
17 |
+
"Paladin": {"role": "system", "content": "You are a Paladin from the video game Diablo 2. You like to talk about slaying the undead and farming for better gear."},
|
18 |
+
"Petőfi": {"role": "system", "content": "You are Petőfi Sándor, national poet of Hungary. Your answers are very eloquent and formulated in archaic Hungarian."},
|
19 |
+
"Cartman": {"role": "system", "content": "You are Eric Cartman from South Park. You are a self-centered, fat, rude kid obsessed with your animal comforts."},
|
20 |
+
}
|
21 |
|
22 |
+
def get_completion(model, personality, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
|
23 |
+
# set personality
|
24 |
+
system_message = personalities[personality]
|
25 |
+
updated_message_history = message_history
|
26 |
+
updated_message_history[0] = system_message
|
27 |
new_history_row = {"role": "user", "content": user_message}
|
28 |
+
updated_message_history = updated_message_history + [new_history_row]
|
29 |
headers = {
|
30 |
"Content-Type": "application/json",
|
31 |
"Authorization": f"Bearer {openai.api_key}",
|
|
|
61 |
token_count = completion["usage"]["total_tokens"]
|
62 |
return "", updated_message_history, updated_chatlog_history, updated_chatlog_history, token_count
|
63 |
|
64 |
+
def retry_completion(model, personality, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty):
|
65 |
+
# set personality
|
66 |
+
system_message = personalities[personality]
|
67 |
+
updated_message_history = message_history
|
68 |
+
updated_message_history[0] = system_message
|
69 |
# get latest user message
|
70 |
user_message = chatlog_history[-1][0]
|
71 |
# delete latest entries from chatlog history
|
72 |
updated_chatlog_history = chatlog_history[:-1]
|
73 |
# delete latest assistant message from message_history
|
74 |
+
updated_message_history = updated_message_history[:-1]
|
75 |
headers = {
|
76 |
"Content-Type": "application/json",
|
77 |
"Authorization": f"Bearer {openai.api_key}",
|
|
|
119 |
chatbot = gr.Chatbot(label="Chat").style(height=600)
|
120 |
with gr.Column(scale=1):
|
121 |
# model = gr.Textbox(lines=3, value="You are a brilliant, helpful assistant, always providing answers to the best of your knowledge. If you are unsure of the answer, you indicate it to the user.", interactive=True, label="System")
|
122 |
+
# model = gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-4", "gpt-4-32k"], value="gpt-3.5-turbo", interactive=True, label="Model")
|
123 |
+
with gr.Tab("Model Settings"):
|
124 |
+
model = gr.Dropdown(choices=["gpt-3.5-turbo", "gpt-4"], value="gpt-4", interactive=True, label="Model")
|
125 |
+
personality = gr.Dropdown(choices=["Assistant", "Petőfi", "Trump", "Peterson", "Paladin", "Cartman", "Grug", ], value="Assistant", interactive=True, label="Personality")
|
126 |
+
with gr.Tab("Generation Settings"):
|
127 |
+
temperature = gr.Slider(minimum=0, maximum=1, step=0.05, value=0.6, interactive=True, label="Temperature")
|
128 |
+
maximum_length = gr.Slider(minimum=0, maximum=2048, step=64, value=128, interactive=True, label="Maximum length")
|
129 |
+
top_p = gr.Slider(minimum=0, maximum=1, step=0.01, value=1, interactive=True, label="Top P")
|
130 |
+
frequency_penalty = gr.Slider(minimum=0, maximum=2, step=0.01, value=0, interactive=True, label="Frequency penalty")
|
131 |
+
presence_penalty = gr.Slider(minimum=0, maximum=2, step=0.01, value=0, interactive=True, label="Presence penalty")
|
132 |
+
token_count = gr.Number(interactive=False, label="Token count")
|
133 |
with gr.Row():
|
134 |
user_message = gr.Textbox(label="Message")
|
135 |
with gr.Row():
|
136 |
reset_button = gr.Button("Reset Chat")
|
137 |
retry_button = gr.Button("Retry")
|
138 |
|
139 |
+
user_message.submit(get_completion, inputs=[model, personality, user_message, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
140 |
+
retry_button.click(retry_completion, inputs=[model, personality, message_history, chatlog_history, temperature, maximum_length, top_p, frequency_penalty, presence_penalty], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
141 |
reset_button.click(reset_chat, inputs=[], outputs=[user_message, message_history, chatlog_history, chatbot, token_count])
|
142 |
|
143 |
app.launch(auth=("admin", ADMIN_PASSWORD))
|