Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,41 +12,32 @@ sessions = {}
|
|
| 12 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
| 13 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 14 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
|
|
|
| 15 |
def render_avatars(userid):
|
| 16 |
try:
|
| 17 |
response = requests.post(
|
| 18 |
'https://host.palple.polrambora.com/userexistence',
|
| 19 |
-
json={
|
| 20 |
-
'userid': userid
|
| 21 |
-
},
|
| 22 |
timeout=10
|
| 23 |
)
|
| 24 |
-
|
| 25 |
if response.status_code == 200:
|
| 26 |
response_json = response.json()
|
| 27 |
return response_json["avatar"]["link"]
|
| 28 |
-
except Exception
|
| 29 |
return None
|
| 30 |
except requests.exceptions.Timeout:
|
| 31 |
return None
|
| 32 |
|
| 33 |
-
|
| 34 |
def authorize(user, api_key, system_message):
|
| 35 |
-
test_data = {
|
| 36 |
-
"user": user,
|
| 37 |
-
"key": api_key
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
try:
|
| 41 |
response = requests.post(
|
| 42 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 43 |
json=test_data,
|
| 44 |
)
|
| 45 |
-
|
| 46 |
if response.status_code == 200:
|
| 47 |
response_json = response.json()
|
| 48 |
avatar = render_avatars(user) or USER_PIC_PATH
|
| 49 |
-
|
| 50 |
if api_key not in sessions:
|
| 51 |
sessions[api_key] = {
|
| 52 |
"history": [],
|
|
@@ -62,7 +53,7 @@ def authorize(user, api_key, system_message):
|
|
| 62 |
return 403
|
| 63 |
else:
|
| 64 |
return False
|
| 65 |
-
except Exception
|
| 66 |
return False
|
| 67 |
|
| 68 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
|
@@ -97,18 +88,19 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 97 |
"conversation_history": messages,
|
| 98 |
"input": message
|
| 99 |
}
|
| 100 |
-
|
| 101 |
-
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
| 102 |
-
|
| 103 |
if response.status_code == 200:
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
|
| 107 |
sessions[api_key]["history"] = history
|
| 108 |
-
|
| 109 |
else:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
|
| 113 |
def render_message(history):
|
| 114 |
messages_html = """
|
|
@@ -145,15 +137,14 @@ def escape_html(unsafe_text):
|
|
| 145 |
.replace("'", "'")
|
| 146 |
)
|
| 147 |
|
| 148 |
-
css="""
|
| 149 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
| 150 |
"""
|
|
|
|
| 151 |
with gr.Blocks(css=css) as demo:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
with gr.Column(visible=True) as auth_view:
|
| 155 |
gr.Markdown("## P-MSQ Authorization")
|
| 156 |
-
gr.Markdown("P-MSQ is in closed alpha test! The model,
|
| 157 |
api_user_input = gr.Textbox(placeholder="snowflake", label="UserID", type='email')
|
| 158 |
api_key_input = gr.Textbox(placeholder="Enter your API key", label="Token", type='password')
|
| 159 |
auth_button = gr.Button("Authorize")
|
|
@@ -174,16 +165,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 174 |
lines=2,
|
| 175 |
elem_id="input-text"
|
| 176 |
)
|
| 177 |
-
|
| 178 |
send_btn = gr.Button("Send")
|
| 179 |
regen_btn = gr.Button("Clear")
|
| 180 |
-
|
| 181 |
system_instructions_input = gr.Textbox(placeholder="Enter custom instructions (optional)",
|
| 182 |
label="Custom System Instructions",
|
| 183 |
lines=2)
|
| 184 |
save_instructions_btn = gr.Button("Save Instructions")
|
| 185 |
gr.Markdown("### Settings")
|
| 186 |
-
|
| 187 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
|
| 188 |
top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P")
|
| 189 |
temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature")
|
|
@@ -191,86 +179,21 @@ with gr.Blocks(css=css) as demo:
|
|
| 191 |
history_state = gr.State([])
|
| 192 |
last_message_state = gr.State("")
|
| 193 |
|
| 194 |
-
def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
|
| 195 |
-
loading_message = history + [(message, "Loading...", "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)]
|
| 196 |
-
yield render_message(loading_message), loading_message, ""
|
| 197 |
-
|
| 198 |
-
history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)
|
| 199 |
-
yield render_message(history), history, ""
|
| 200 |
-
|
| 201 |
-
def regenerate_response(history, last_message, max_tokens, top_p, temperature):
|
| 202 |
-
return "", []
|
| 203 |
-
|
| 204 |
-
def clear_history(api_key):
|
| 205 |
-
if api_key in sessions:
|
| 206 |
-
sessions[api_key]["history"] = []
|
| 207 |
-
return "", []
|
| 208 |
-
|
| 209 |
-
def load_conversation(api_key):
|
| 210 |
-
session = sessions.get(api_key, {})
|
| 211 |
-
history = session.get("history", [])
|
| 212 |
-
return render_message(history), history
|
| 213 |
-
|
| 214 |
msg_input.submit(
|
| 215 |
-
|
| 216 |
inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
|
| 217 |
outputs=[chatbot_output, history_state, msg_input],
|
| 218 |
)
|
| 219 |
-
|
| 220 |
send_btn.click(
|
| 221 |
-
|
| 222 |
inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
|
| 223 |
outputs=[chatbot_output, history_state, msg_input],
|
| 224 |
)
|
|
|
|
| 225 |
|
| 226 |
-
|
| 227 |
-
inputs=[api_key_input],
|
| 228 |
-
outputs=[chatbot_output, history_state])
|
| 229 |
-
|
| 230 |
-
with gr.Column(visible=False) as blacklist_view:
|
| 231 |
-
gr.Markdown("## P-MSQ Authorization")
|
| 232 |
-
gr.Markdown("Your linked ID appears to be blacklisted, and your API Key is pending on removal, if you believe this is a mistake, please try reaching us out.")
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
def authorize_and_proceed(user, api_key):
|
| 236 |
-
if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
|
| 237 |
-
gr.Info("Loading, please wait.")
|
| 238 |
-
messages_html, history = load_conversation(api_key)
|
| 239 |
-
|
| 240 |
-
return (
|
| 241 |
-
gr.update(visible=False),
|
| 242 |
-
gr.update(visible=True),
|
| 243 |
-
messages_html,
|
| 244 |
-
history
|
| 245 |
-
)
|
| 246 |
-
elif authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS) == 403:
|
| 247 |
-
return (
|
| 248 |
-
gr.update(visible=False),
|
| 249 |
-
gr.update(visible=False),
|
| 250 |
-
gr.update(visible=True),
|
| 251 |
-
)
|
| 252 |
-
else:
|
| 253 |
-
gr.Warning("Incorrect userid/token")
|
| 254 |
-
return (
|
| 255 |
-
gr.update(visible=True),
|
| 256 |
-
gr.update(visible=False),
|
| 257 |
-
auth_status.update(value="Invalid userid/token")
|
| 258 |
-
)
|
| 259 |
-
|
| 260 |
-
def save_custom_instructions(api_key, custom_instructions):
|
| 261 |
-
if api_key in sessions:
|
| 262 |
-
gr.Info("Instructions updated, we recommend to start the new conversation to make it more efficient.")
|
| 263 |
-
sessions[api_key]["system_message"] = custom_instructions
|
| 264 |
-
return "Instructions updated!", gr.update(value="")
|
| 265 |
-
else:
|
| 266 |
-
gr.Warning("Your session has been expired, please refresh the page and login again.")
|
| 267 |
-
return "Session not found.", gr.update(value="")
|
| 268 |
-
|
| 269 |
|
| 270 |
-
|
| 271 |
|
| 272 |
-
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
|
| 273 |
-
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 274 |
-
demo.launch(show_api=False)
|
| 275 |
if __name__ == "__main__":
|
| 276 |
-
demo.queue = False
|
|
|
|
| 12 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
| 13 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 14 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 15 |
+
|
| 16 |
def render_avatars(userid):
|
| 17 |
try:
|
| 18 |
response = requests.post(
|
| 19 |
'https://host.palple.polrambora.com/userexistence',
|
| 20 |
+
json={'userid': userid},
|
|
|
|
|
|
|
| 21 |
timeout=10
|
| 22 |
)
|
|
|
|
| 23 |
if response.status_code == 200:
|
| 24 |
response_json = response.json()
|
| 25 |
return response_json["avatar"]["link"]
|
| 26 |
+
except Exception:
|
| 27 |
return None
|
| 28 |
except requests.exceptions.Timeout:
|
| 29 |
return None
|
| 30 |
|
|
|
|
| 31 |
def authorize(user, api_key, system_message):
|
| 32 |
+
test_data = {"user": user, "key": api_key}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
response = requests.post(
|
| 35 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 36 |
json=test_data,
|
| 37 |
)
|
|
|
|
| 38 |
if response.status_code == 200:
|
| 39 |
response_json = response.json()
|
| 40 |
avatar = render_avatars(user) or USER_PIC_PATH
|
|
|
|
| 41 |
if api_key not in sessions:
|
| 42 |
sessions[api_key] = {
|
| 43 |
"history": [],
|
|
|
|
| 53 |
return 403
|
| 54 |
else:
|
| 55 |
return False
|
| 56 |
+
except Exception:
|
| 57 |
return False
|
| 58 |
|
| 59 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
|
|
|
| 88 |
"conversation_history": messages,
|
| 89 |
"input": message
|
| 90 |
}
|
| 91 |
+
|
| 92 |
+
response = requests.post(API_URL, headers=headers, data=json.dumps(data), stream=True)
|
|
|
|
| 93 |
if response.status_code == 200:
|
| 94 |
+
assistant_reply = ""
|
| 95 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 96 |
+
if chunk:
|
| 97 |
+
assistant_reply += chunk.decode()
|
| 98 |
+
yield assistant_reply
|
| 99 |
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
|
| 100 |
sessions[api_key]["history"] = history
|
| 101 |
+
yield history, assistant_reply
|
| 102 |
else:
|
| 103 |
+
yield history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
|
|
|
| 104 |
|
| 105 |
def render_message(history):
|
| 106 |
messages_html = """
|
|
|
|
| 137 |
.replace("'", "'")
|
| 138 |
)
|
| 139 |
|
| 140 |
+
css = """
|
| 141 |
.chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
|
| 142 |
"""
|
| 143 |
+
|
| 144 |
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
| 145 |
with gr.Column(visible=True) as auth_view:
|
| 146 |
gr.Markdown("## P-MSQ Authorization")
|
| 147 |
+
gr.Markdown("P-MSQ is in closed alpha test! The model, API, and more are subject to change.")
|
| 148 |
api_user_input = gr.Textbox(placeholder="snowflake", label="UserID", type='email')
|
| 149 |
api_key_input = gr.Textbox(placeholder="Enter your API key", label="Token", type='password')
|
| 150 |
auth_button = gr.Button("Authorize")
|
|
|
|
| 165 |
lines=2,
|
| 166 |
elem_id="input-text"
|
| 167 |
)
|
|
|
|
| 168 |
send_btn = gr.Button("Send")
|
| 169 |
regen_btn = gr.Button("Clear")
|
|
|
|
| 170 |
system_instructions_input = gr.Textbox(placeholder="Enter custom instructions (optional)",
|
| 171 |
label="Custom System Instructions",
|
| 172 |
lines=2)
|
| 173 |
save_instructions_btn = gr.Button("Save Instructions")
|
| 174 |
gr.Markdown("### Settings")
|
|
|
|
| 175 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
|
| 176 |
top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P")
|
| 177 |
temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature")
|
|
|
|
| 179 |
history_state = gr.State([])
|
| 180 |
last_message_state = gr.State("")
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
msg_input.submit(
|
| 183 |
+
respond,
|
| 184 |
inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
|
| 185 |
outputs=[chatbot_output, history_state, msg_input],
|
| 186 |
)
|
|
|
|
| 187 |
send_btn.click(
|
| 188 |
+
respond,
|
| 189 |
inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature],
|
| 190 |
outputs=[chatbot_output, history_state, msg_input],
|
| 191 |
)
|
| 192 |
+
regen_btn.click(lambda x: ("", []), inputs=[api_key_input], outputs=[chatbot_output, history_state])
|
| 193 |
|
| 194 |
+
auth_button.click(authorize, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
demo.launch(show_api=False)
|
| 197 |
|
|
|
|
|
|
|
|
|
|
| 198 |
if __name__ == "__main__":
|
| 199 |
+
demo.queue = False
|