Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,15 +4,38 @@ import json
|
|
4 |
import os
|
5 |
|
6 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
|
|
|
|
7 |
API_TOKEN = os.getenv("POLLY")
|
8 |
|
|
|
|
|
|
|
9 |
headers = {
|
10 |
"Authorization": f"{API_TOKEN}",
|
11 |
"Content-Type": "application/json",
|
12 |
}
|
13 |
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def respond(message, history, system_message, max_tokens, top_p, temperature):
|
18 |
messages = []
|
@@ -49,6 +72,7 @@ def respond(message, history, system_message, max_tokens, top_p, temperature):
|
|
49 |
response_json = response.json()
|
50 |
assistant_reply = response_json["msq"]["message"][0]
|
51 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
|
|
52 |
return history, assistant_reply
|
53 |
else:
|
54 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
@@ -71,27 +95,41 @@ def render_message(history):
|
|
71 |
return messages_html
|
72 |
|
73 |
with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background-color: #f9f9f9;}") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
gr.Markdown("## P-MSQ Chat Interface")
|
75 |
|
76 |
-
chatbot_output = gr.HTML(elem_id="chatbox")
|
77 |
|
78 |
msg_input = gr.Textbox(
|
79 |
show_label=False,
|
80 |
placeholder="Type your message and press Enter...",
|
81 |
lines=2,
|
82 |
-
elem_id="input-text"
|
|
|
83 |
)
|
84 |
|
85 |
-
send_btn = gr.Button("Send")
|
86 |
-
regen_btn = gr.Button("Clear")
|
87 |
|
88 |
-
|
|
|
|
|
|
|
89 |
|
90 |
-
gr.Markdown("### Settings")
|
91 |
|
92 |
-
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
|
93 |
-
top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P")
|
94 |
-
temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature")
|
95 |
|
96 |
history_state = gr.State([])
|
97 |
last_message_state = gr.State("")
|
@@ -115,7 +153,11 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
115 |
inputs=[history_state, last_message_state, system_message, max_tokens, top_p, temperature],
|
116 |
outputs=[chatbot_output, history_state])
|
117 |
|
118 |
-
|
|
|
|
|
|
|
|
|
119 |
send_btn
|
120 |
regen_btn
|
121 |
|
|
|
4 |
import os
|
5 |
|
6 |
API_URL = "https://host.palple.polrambora.com/pmsq"
|
7 |
+
AUTH_FILE = "auth_key.json"
|
8 |
+
CONVO_FILE = "conversation_history.json"
|
9 |
API_TOKEN = os.getenv("POLLY")
|
10 |
|
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 |
headers = {
|
15 |
"Authorization": f"{API_TOKEN}",
|
16 |
"Content-Type": "application/json",
|
17 |
}
|
18 |
|
19 |
+
def check_auth(key):
|
20 |
+
if not os.path.exists(AUTH_FILE):
|
21 |
+
return False
|
22 |
+
with open(AUTH_FILE, 'r') as f:
|
23 |
+
saved_key = f.read().strip()
|
24 |
+
return key == saved_key
|
25 |
+
|
26 |
+
def save_auth(key):
|
27 |
+
with open(AUTH_FILE, 'w') as f:
|
28 |
+
f.write(key)
|
29 |
+
|
30 |
+
def load_conversation():
|
31 |
+
if os.path.exists(CONVO_FILE):
|
32 |
+
with open(CONVO_FILE, 'r') as f:
|
33 |
+
return json.load(f)
|
34 |
+
return []
|
35 |
+
|
36 |
+
def save_conversation(history):
|
37 |
+
with open(CONVO_FILE, 'w') as f:
|
38 |
+
json.dump(history, f)
|
39 |
|
40 |
def respond(message, history, system_message, max_tokens, top_p, temperature):
|
41 |
messages = []
|
|
|
72 |
response_json = response.json()
|
73 |
assistant_reply = response_json["msq"]["message"][0]
|
74 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
75 |
+
save_conversation(history)
|
76 |
return history, assistant_reply
|
77 |
else:
|
78 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
|
|
95 |
return messages_html
|
96 |
|
97 |
with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background-color: #f9f9f9;}") as demo:
|
98 |
+
def auth_and_load(auth_key):
|
99 |
+
if check_auth(auth_key):
|
100 |
+
history = load_conversation()
|
101 |
+
return render_message(history), gr.update(visible=True), gr.update(visible=False)
|
102 |
+
return "", gr.update(visible=False), gr.update(visible=True)
|
103 |
+
|
104 |
+
def authorize_and_proceed(auth_key):
|
105 |
+
save_auth(auth_key)
|
106 |
+
return auth_and_load(auth_key)
|
107 |
+
|
108 |
gr.Markdown("## P-MSQ Chat Interface")
|
109 |
|
110 |
+
chatbot_output = gr.HTML(elem_id="chatbox", visible=False)
|
111 |
|
112 |
msg_input = gr.Textbox(
|
113 |
show_label=False,
|
114 |
placeholder="Type your message and press Enter...",
|
115 |
lines=2,
|
116 |
+
elem_id="input-text",
|
117 |
+
visible=False
|
118 |
)
|
119 |
|
120 |
+
send_btn = gr.Button("Send", visible=False)
|
121 |
+
regen_btn = gr.Button("Clear", visible=False)
|
122 |
|
123 |
+
auth_key_input = gr.Textbox(label="Enter your auth key:", type="password", visible=True)
|
124 |
+
auth_btn = gr.Button("Authorize", visible=True)
|
125 |
+
|
126 |
+
system_message = gr.Textbox(value="You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations.", label="System message", visible=False)
|
127 |
|
128 |
+
gr.Markdown("### Settings", visible=False)
|
129 |
|
130 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens", visible=False)
|
131 |
+
top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P", visible=False)
|
132 |
+
temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature", visible=False)
|
133 |
|
134 |
history_state = gr.State([])
|
135 |
last_message_state = gr.State("")
|
|
|
153 |
inputs=[history_state, last_message_state, system_message, max_tokens, top_p, temperature],
|
154 |
outputs=[chatbot_output, history_state])
|
155 |
|
156 |
+
auth_btn.click(auth_and_load, inputs=auth_key_input, outputs=[chatbot_output, chatbot_output, auth_btn])
|
157 |
+
|
158 |
+
auth_key_input.submit(authorize_and_proceed, inputs=auth_key_input, outputs=[chatbot_output, chatbot_output, auth_btn])
|
159 |
+
|
160 |
+
with gr.Row(visible=False):
|
161 |
send_btn
|
162 |
regen_btn
|
163 |
|