Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ sessions = {}
|
|
| 10 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 11 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 12 |
|
| 13 |
-
def authorize(user, api_key):
|
| 14 |
test_data = {
|
| 15 |
"user": user,
|
| 16 |
"key": api_key
|
|
@@ -36,7 +36,8 @@ def authorize(user, api_key):
|
|
| 36 |
"headers": {
|
| 37 |
"authorization": api_key,
|
| 38 |
"Content-Type": 'application/json'
|
| 39 |
-
}
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
return True
|
|
@@ -54,11 +55,11 @@ def authorize(user, api_key):
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
|
| 57 |
-
def respond(message, api_key,
|
| 58 |
session = sessions.get(api_key, {})
|
| 59 |
history = session.get("history", [])
|
| 60 |
headers = session.get("headers", {})
|
| 61 |
-
|
| 62 |
messages = []
|
| 63 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 64 |
if user_message:
|
|
@@ -142,8 +143,10 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
| 142 |
send_btn = gr.Button("Send")
|
| 143 |
regen_btn = gr.Button("Clear")
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
gr.Markdown("### Settings")
|
| 148 |
|
| 149 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
|
|
@@ -189,8 +192,17 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
| 189 |
else:
|
| 190 |
return gr.update(visible=True), gr.update(visible=False), auth_status.update(value="Invalid userid/token")
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
|
|
|
| 193 |
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
|
|
|
|
|
|
|
| 194 |
|
| 195 |
if __name__ == "__main__":
|
| 196 |
demo.queue = False
|
|
|
|
| 10 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 11 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 12 |
|
| 13 |
+
def authorize(user, api_key, system_message):
|
| 14 |
test_data = {
|
| 15 |
"user": user,
|
| 16 |
"key": api_key
|
|
|
|
| 36 |
"headers": {
|
| 37 |
"authorization": api_key,
|
| 38 |
"Content-Type": 'application/json'
|
| 39 |
+
},
|
| 40 |
+
"system_message": system_message
|
| 41 |
}
|
| 42 |
|
| 43 |
return True
|
|
|
|
| 55 |
return False
|
| 56 |
|
| 57 |
|
| 58 |
+
def respond(message, api_key, max_tokens, top_p, temperature):
|
| 59 |
session = sessions.get(api_key, {})
|
| 60 |
history = session.get("history", [])
|
| 61 |
headers = session.get("headers", {})
|
| 62 |
+
system_message = session.get("system_message", "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations.")
|
| 63 |
messages = []
|
| 64 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 65 |
if user_message:
|
|
|
|
| 143 |
send_btn = gr.Button("Send")
|
| 144 |
regen_btn = gr.Button("Clear")
|
| 145 |
|
| 146 |
+
system_instructions_input = gr.Textbox(placeholder="Enter custom instructions (optional)",
|
| 147 |
+
label="Custom System Instructions",
|
| 148 |
+
lines=2)
|
| 149 |
+
save_instructions_btn = gr.Button("Save Instructions")
|
| 150 |
gr.Markdown("### Settings")
|
| 151 |
|
| 152 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
|
|
|
|
| 192 |
else:
|
| 193 |
return gr.update(visible=True), gr.update(visible=False), auth_status.update(value="Invalid userid/token")
|
| 194 |
|
| 195 |
+
def save_custom_instructions(api_key, custom_instructions):
|
| 196 |
+
if api_key in sessions:
|
| 197 |
+
sessions[api_key]["system_message"] = custom_instructions
|
| 198 |
+
return "Instructions updated!"
|
| 199 |
+
else:
|
| 200 |
+
return "Session not found."
|
| 201 |
|
| 202 |
+
|
| 203 |
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
|
| 204 |
+
save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
|
| 205 |
+
|
| 206 |
|
| 207 |
if __name__ == "__main__":
|
| 208 |
demo.queue = False
|