Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,41 +10,57 @@ sessions = {}
|
|
10 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
11 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
12 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
13 |
-
|
14 |
def render_avatars(userid):
|
15 |
try:
|
16 |
response = requests.post(
|
17 |
'https://host.palple.polrambora.com/userexistence',
|
18 |
-
json={
|
|
|
|
|
19 |
timeout=10
|
20 |
)
|
|
|
21 |
if response.status_code == 200:
|
22 |
response_json = response.json()
|
23 |
return response_json["avatar"]["link"]
|
24 |
-
except Exception:
|
25 |
return None
|
|
|
|
|
|
|
26 |
|
27 |
def authorize(user, api_key, system_message):
|
28 |
-
test_data = {
|
|
|
|
|
|
|
|
|
29 |
try:
|
30 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
31 |
if response.status_code == 200:
|
|
|
32 |
avatar = render_avatars(user) or USER_PIC_PATH
|
|
|
33 |
if api_key not in sessions:
|
34 |
sessions[api_key] = {
|
35 |
-
"history": [],
|
36 |
-
"headers": {
|
37 |
"authorization": api_key,
|
38 |
"Content-Type": 'application/json'
|
39 |
},
|
40 |
"avatar": avatar,
|
41 |
-
"system_message": system_message
|
42 |
}
|
43 |
return True
|
44 |
elif response.status_code == 403:
|
45 |
return 403
|
46 |
-
|
47 |
-
|
|
|
48 |
return False
|
49 |
|
50 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
@@ -52,12 +68,23 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
52 |
history = session.get("history", [])
|
53 |
headers = session.get("headers", {})
|
54 |
system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)
|
55 |
-
messages = [
|
56 |
-
|
57 |
-
if user_message
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
data = {
|
62 |
"preferences": {
|
63 |
"max_char": max_tokens,
|
@@ -68,46 +95,79 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
68 |
"conversation_history": messages,
|
69 |
"input": message
|
70 |
}
|
|
|
71 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
|
|
72 |
if response.status_code == 200:
|
73 |
response_json = response.json()
|
74 |
assistant_reply = response_json["msq"]["message"][0]
|
75 |
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
|
76 |
-
sessions[api_key]["history"] = history
|
77 |
-
return history, assistant_reply
|
78 |
-
|
|
|
|
|
79 |
|
80 |
def render_message(history):
|
81 |
messages_html = """
|
82 |
<div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
|
83 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
|
|
84 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
85 |
if user_message:
|
86 |
user_message_html = markdown.markdown(user_message)
|
87 |
messages_html += f"""
|
88 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
89 |
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
90 |
-
<span style='color: white;'>{user_message_html}</span>
|
91 |
</div><br>"""
|
|
|
92 |
if assistant_message:
|
93 |
assistant_message_html = markdown.markdown(assistant_message)
|
94 |
messages_html += f"""
|
95 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
96 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
97 |
-
<span style='color: white;'>{assistant_message_html}</span>
|
98 |
</div><br>"""
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
return messages_html
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
105 |
"""
|
106 |
|
107 |
-
with gr.Blocks(css=
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
gr.Markdown("## P-MSQ Chat Interface")
|
110 |
-
|
111 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
112 |
|
113 |
msg_input = gr.Text(
|
@@ -215,4 +275,4 @@ with gr.Blocks(css=math_css) as demo:
|
|
215 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
216 |
demo.launch(show_api=False)
|
217 |
if __name__ == "__main__":
|
218 |
-
demo.queue = False
|
|
|
10 |
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
|
11 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
12 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
|
|
13 |
def render_avatars(userid):
|
14 |
try:
|
15 |
response = requests.post(
|
16 |
'https://host.palple.polrambora.com/userexistence',
|
17 |
+
json={
|
18 |
+
'userid': userid
|
19 |
+
},
|
20 |
timeout=10
|
21 |
)
|
22 |
+
|
23 |
if response.status_code == 200:
|
24 |
response_json = response.json()
|
25 |
return response_json["avatar"]["link"]
|
26 |
+
except Exception as e:
|
27 |
return None
|
28 |
+
except requests.exceptions.Timeout:
|
29 |
+
return None
|
30 |
+
|
31 |
|
32 |
def authorize(user, api_key, system_message):
|
33 |
+
test_data = {
|
34 |
+
"user": user,
|
35 |
+
"key": api_key
|
36 |
+
}
|
37 |
+
|
38 |
try:
|
39 |
+
response = requests.post(
|
40 |
+
"https://host.palple.polrambora.com/check_key_impv",
|
41 |
+
json=test_data,
|
42 |
+
)
|
43 |
+
|
44 |
if response.status_code == 200:
|
45 |
+
response_json = response.json()
|
46 |
avatar = render_avatars(user) or USER_PIC_PATH
|
47 |
+
|
48 |
if api_key not in sessions:
|
49 |
sessions[api_key] = {
|
50 |
+
"history": [],
|
51 |
+
"headers": {
|
52 |
"authorization": api_key,
|
53 |
"Content-Type": 'application/json'
|
54 |
},
|
55 |
"avatar": avatar,
|
56 |
+
"system_message": system_message
|
57 |
}
|
58 |
return True
|
59 |
elif response.status_code == 403:
|
60 |
return 403
|
61 |
+
else:
|
62 |
+
return False
|
63 |
+
except Exception as e:
|
64 |
return False
|
65 |
|
66 |
def respond(message, api_key, max_tokens, top_p, temperature):
|
|
|
68 |
history = session.get("history", [])
|
69 |
headers = session.get("headers", {})
|
70 |
system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)
|
71 |
+
messages = []
|
72 |
+
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
73 |
+
if user_message:
|
74 |
+
messages.append({
|
75 |
+
"role": "user",
|
76 |
+
"content": user_message,
|
77 |
+
"profile": user_profile,
|
78 |
+
"picture": user_pic
|
79 |
+
})
|
80 |
+
if assistant_message:
|
81 |
+
messages.append({
|
82 |
+
"role": "assistant",
|
83 |
+
"content": assistant_message,
|
84 |
+
"profile": assistant_profile,
|
85 |
+
"picture": assistant_pic
|
86 |
+
})
|
87 |
+
|
88 |
data = {
|
89 |
"preferences": {
|
90 |
"max_char": max_tokens,
|
|
|
95 |
"conversation_history": messages,
|
96 |
"input": message
|
97 |
}
|
98 |
+
|
99 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
100 |
+
|
101 |
if response.status_code == 200:
|
102 |
response_json = response.json()
|
103 |
assistant_reply = response_json["msq"]["message"][0]
|
104 |
history.append((message, assistant_reply, "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH))
|
105 |
+
sessions[api_key]["history"] = history
|
106 |
+
return history, assistant_reply
|
107 |
+
else:
|
108 |
+
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
109 |
+
|
110 |
|
111 |
def render_message(history):
|
112 |
messages_html = """
|
113 |
<div id="chatbox-container" class="chatbox" style="height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;">
|
114 |
<div id="messages" style="display: block; margin-bottom: 10px;">"""
|
115 |
+
|
116 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
117 |
if user_message:
|
118 |
user_message_html = markdown.markdown(user_message)
|
119 |
messages_html += f"""
|
120 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
121 |
<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
122 |
+
<span id="message-content" style='color: white;'>{user_message_html}</span>
|
123 |
</div><br>"""
|
124 |
+
|
125 |
if assistant_message:
|
126 |
assistant_message_html = markdown.markdown(assistant_message)
|
127 |
messages_html += f"""
|
128 |
<div style='display: flex; align-items: center; margin-bottom: 10px;'>
|
129 |
<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>
|
130 |
+
<span id="message-content" style='color: white;'>{assistant_message_html}</span>
|
131 |
</div><br>"""
|
132 |
+
|
133 |
+
messages_html += """
|
134 |
+
</div></div>
|
135 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.3/purify.min.js"></script>
|
136 |
+
<script>
|
137 |
+
function escapeHtml(unsafe) {
|
138 |
+
return unsafe
|
139 |
+
.replace(/&/g, "&")
|
140 |
+
.replace(/</g, "<")
|
141 |
+
.replace(/>/g, ">")
|
142 |
+
.replace(/"/g, """)
|
143 |
+
.replace(/'/g, "'");
|
144 |
+
}
|
145 |
+
let message = document.getElementById('message-content').innerHTML
|
146 |
+
document.getElementById('message-content').innerHTML = escapeHtml(message);
|
147 |
+
</script>
|
148 |
+
"""
|
149 |
return messages_html
|
150 |
|
151 |
+
|
152 |
+
js = """
|
153 |
+
function HTMLClean() {
|
154 |
+
alert("This page is in-dev stage, expect lots of restarts, the interface is open-sourced (not the backend that is responsible for API Keys)")
|
155 |
+
}
|
156 |
"""
|
157 |
|
158 |
+
with gr.Blocks(css=".chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}", js=js) as demo:
|
159 |
+
|
160 |
+
with gr.Column(visible=True) as auth_view:
|
161 |
+
gr.Markdown("## P-MSQ Authorization")
|
162 |
+
gr.Markdown("P-MSQ is in closed alpha test! The model, api and more are subject to change.")
|
163 |
+
api_user_input = gr.Textbox(placeholder="snowflake", label="UserID", type='email')
|
164 |
+
api_key_input = gr.Textbox(placeholder="Enter your API key", label="Token", type='password')
|
165 |
+
auth_button = gr.Button("Authorize")
|
166 |
+
auth_status = gr.Textbox(label="Authorization Status", interactive=False)
|
167 |
+
|
168 |
+
with gr.Column(visible=False) as chat_view:
|
169 |
gr.Markdown("## P-MSQ Chat Interface")
|
170 |
+
|
171 |
chatbot_output = gr.HTML(elem_id="chatbox-container")
|
172 |
|
173 |
msg_input = gr.Text(
|
|
|
275 |
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
276 |
demo.launch(show_api=False)
|
277 |
if __name__ == "__main__":
|
278 |
+
demo.queue = False
|