Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,21 @@ 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 authorize(user, api_key, system_message):
|
| 15 |
test_data = {
|
|
@@ -25,6 +40,7 @@ def authorize(user, api_key, system_message):
|
|
| 25 |
|
| 26 |
if response.status_code == 200:
|
| 27 |
response_json = response.json()
|
|
|
|
| 28 |
|
| 29 |
if api_key not in sessions:
|
| 30 |
sessions[api_key] = {
|
|
@@ -33,6 +49,7 @@ def authorize(user, api_key, system_message):
|
|
| 33 |
"authorization": api_key,
|
| 34 |
"Content-Type": 'application/json'
|
| 35 |
},
|
|
|
|
| 36 |
"system_message": system_message
|
| 37 |
}
|
| 38 |
return True
|
|
@@ -85,6 +102,7 @@ def respond(message, api_key, max_tokens, top_p, temperature):
|
|
| 85 |
else:
|
| 86 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
| 87 |
|
|
|
|
| 88 |
def render_message(history):
|
| 89 |
messages_html = """
|
| 90 |
<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;">
|
|
@@ -171,7 +189,7 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow: auto; border: 1px solid #
|
|
| 171 |
last_message_state = gr.State("")
|
| 172 |
|
| 173 |
def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
|
| 174 |
-
loading_message = history + [(message, "Loading...", "You", "P-ALPLE",
|
| 175 |
yield render_message(loading_message), loading_message, ""
|
| 176 |
|
| 177 |
history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)
|
|
|
|
| 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 |
+
)
|
| 21 |
+
|
| 22 |
+
if response.status_code === 200:
|
| 23 |
+
response_json = response.json()
|
| 24 |
+
return response_json["avatar"]["link"]
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
|
| 29 |
def authorize(user, api_key, system_message):
|
| 30 |
test_data = {
|
|
|
|
| 40 |
|
| 41 |
if response.status_code == 200:
|
| 42 |
response_json = response.json()
|
| 43 |
+
avatar = render_avatars(user) or USER_PIC_PATH
|
| 44 |
|
| 45 |
if api_key not in sessions:
|
| 46 |
sessions[api_key] = {
|
|
|
|
| 49 |
"authorization": api_key,
|
| 50 |
"Content-Type": 'application/json'
|
| 51 |
},
|
| 52 |
+
"avatar": avatar,
|
| 53 |
"system_message": system_message
|
| 54 |
}
|
| 55 |
return True
|
|
|
|
| 102 |
else:
|
| 103 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
| 104 |
|
| 105 |
+
|
| 106 |
def render_message(history):
|
| 107 |
messages_html = """
|
| 108 |
<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;">
|
|
|
|
| 189 |
last_message_state = gr.State("")
|
| 190 |
|
| 191 |
def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
|
| 192 |
+
loading_message = history + [(message, "Loading...", "You", "P-ALPLE", sessions[api_key]["avatar"], ASSISTANT_PIC_PATH)]
|
| 193 |
yield render_message(loading_message), loading_message, ""
|
| 194 |
|
| 195 |
history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)
|