Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -32,7 +32,7 @@ def authorize(user, api_key):
|
|
32 |
|
33 |
if api_key not in sessions:
|
34 |
sessions[api_key] = {
|
35 |
-
"history": [],
|
36 |
"headers": {
|
37 |
"authorization": api_key,
|
38 |
"Content-Type": 'application/json'
|
@@ -94,7 +94,7 @@ def respond(message, api_key, system_message, max_tokens, top_p, temperature):
|
|
94 |
assistant_reply = response_json["msq"]["message"][0]
|
95 |
|
96 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
97 |
-
sessions[api_key]["history"] = history
|
98 |
|
99 |
return history, assistant_reply
|
100 |
else:
|
@@ -160,6 +160,16 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
160 |
def regenerate_response(history, last_message, system_message, max_tokens, top_p, temperature):
|
161 |
return "", []
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
msg_input.submit(user_interaction,
|
164 |
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
165 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
@@ -168,18 +178,19 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
168 |
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
169 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
170 |
|
171 |
-
regen_btn.click(
|
172 |
-
inputs=[
|
173 |
outputs=[chatbot_output, history_state])
|
174 |
|
175 |
def authorize_and_proceed(user, api_key):
|
176 |
if authorize(user, api_key):
|
177 |
-
|
|
|
178 |
else:
|
179 |
return gr.update(visible=True), gr.update(visible=False), auth_status.update(value="Invalid userid/token")
|
180 |
|
181 |
|
182 |
-
auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view])
|
183 |
|
184 |
if __name__ == "__main__":
|
185 |
demo.queue = False
|
|
|
32 |
|
33 |
if api_key not in sessions:
|
34 |
sessions[api_key] = {
|
35 |
+
"history": [],
|
36 |
"headers": {
|
37 |
"authorization": api_key,
|
38 |
"Content-Type": 'application/json'
|
|
|
94 |
assistant_reply = response_json["msq"]["message"][0]
|
95 |
|
96 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
97 |
+
sessions[api_key]["history"] = history
|
98 |
|
99 |
return history, assistant_reply
|
100 |
else:
|
|
|
160 |
def regenerate_response(history, last_message, system_message, max_tokens, top_p, temperature):
|
161 |
return "", []
|
162 |
|
163 |
+
def clear_history(api_key):
|
164 |
+
if api_key in sessions:
|
165 |
+
sessions[api_key]["history"] = []
|
166 |
+
return "", []
|
167 |
+
|
168 |
+
def load_conversation(api_key):
|
169 |
+
session = sessions.get(api_key, {})
|
170 |
+
history = session.get("history", [])
|
171 |
+
return render_message(history), history
|
172 |
+
|
173 |
msg_input.submit(user_interaction,
|
174 |
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
175 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
|
|
178 |
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
179 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
180 |
|
181 |
+
regen_btn.click(clear_history,
|
182 |
+
inputs=[api_key_input],
|
183 |
outputs=[chatbot_output, history_state])
|
184 |
|
185 |
def authorize_and_proceed(user, api_key):
|
186 |
if authorize(user, api_key):
|
187 |
+
messages_html, history = load_conversation(api_key)
|
188 |
+
return gr.update(visible=False), gr.update(visible=True), messages_html, history
|
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
|