Spaces:
Build error
Build error
Add sessions, remove session when closed, refreshed tab
Browse files
app.py
CHANGED
|
@@ -7,13 +7,15 @@ API_URL = "https://host.palple.polrambora.com/pmsq"
|
|
| 7 |
API_TOKEN = os.getenv("POLLY")
|
| 8 |
URL_HEADER = os.getenv("KEY")
|
| 9 |
headers = {
|
| 10 |
-
"authorization":"",
|
| 11 |
"Content-Type": 'application/json',
|
| 12 |
}
|
| 13 |
|
| 14 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 15 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 16 |
|
|
|
|
|
|
|
| 17 |
def authorize(user, api_key):
|
| 18 |
test_data = {
|
| 19 |
"user": user,
|
|
@@ -25,20 +27,19 @@ def authorize(user, api_key):
|
|
| 25 |
|
| 26 |
print("Preparing to send the request...")
|
| 27 |
try:
|
| 28 |
-
print(f"Request Data: {test_data}")
|
| 29 |
-
print(f"Request Headers: {test_headers}")
|
| 30 |
-
print(f'{json.dumps(test_data)}')
|
| 31 |
response = requests.post(
|
| 32 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 33 |
json=test_data,
|
| 34 |
)
|
| 35 |
|
| 36 |
-
print("Request sent!")
|
| 37 |
-
|
| 38 |
if response.status_code == 200:
|
| 39 |
response_json = response.json()
|
| 40 |
headers['authorization'] = api_key
|
| 41 |
print(f"Response: {response_json}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return True
|
| 43 |
else:
|
| 44 |
print(f"Failed request - Status code: {response.status_code}, Response: {response.text}")
|
|
@@ -54,7 +55,9 @@ def authorize(user, api_key):
|
|
| 54 |
return False
|
| 55 |
|
| 56 |
|
| 57 |
-
def respond(message,
|
|
|
|
|
|
|
| 58 |
messages = []
|
| 59 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 60 |
if user_message:
|
|
@@ -82,28 +85,19 @@ def respond(message, history, system_message, max_tokens, top_p, temperature):
|
|
| 82 |
"conversation_history": messages,
|
| 83 |
"input": message
|
| 84 |
}
|
| 85 |
-
|
| 86 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
| 87 |
|
| 88 |
if response.status_code == 200:
|
| 89 |
response_json = response.json()
|
| 90 |
assistant_reply = response_json["msq"]["message"][0]
|
|
|
|
| 91 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
|
|
|
| 92 |
return history, assistant_reply
|
| 93 |
else:
|
| 94 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
| 95 |
|
| 96 |
-
def load_conversation_from_file(json_file):
|
| 97 |
-
with open(json_file, 'r') as file:
|
| 98 |
-
data = json.load(file)
|
| 99 |
-
history = []
|
| 100 |
-
for msg in data['conversation']:
|
| 101 |
-
user_message = msg.get('user_message', "")
|
| 102 |
-
assistant_message = msg.get('assistant_message', "")
|
| 103 |
-
history.append((user_message, assistant_message, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
| 104 |
-
return history
|
| 105 |
-
|
| 106 |
-
# Message rendering
|
| 107 |
def render_message(history):
|
| 108 |
messages_html = ""
|
| 109 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
|
@@ -157,28 +151,19 @@ with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid
|
|
| 157 |
history_state = gr.State([])
|
| 158 |
last_message_state = gr.State("")
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def load_conversation(file_path):
|
| 164 |
-
history = load_conversation_from_file(file_path)
|
| 165 |
-
return render_message(history), history
|
| 166 |
-
|
| 167 |
-
load_btn.click(load_conversation, inputs=json_file_input, outputs=[chatbot_output, history_state])
|
| 168 |
-
|
| 169 |
-
def user_interaction(message, history, system_message, max_tokens, top_p, temperature):
|
| 170 |
-
history, assistant_reply = respond(message, history, system_message, max_tokens, top_p, temperature)
|
| 171 |
return render_message(history), history, "", message
|
| 172 |
|
| 173 |
def regenerate_response(history, last_message, system_message, max_tokens, top_p, temperature):
|
| 174 |
return "", []
|
| 175 |
|
| 176 |
msg_input.submit(user_interaction,
|
| 177 |
-
inputs=[msg_input,
|
| 178 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
| 179 |
|
| 180 |
send_btn.click(user_interaction,
|
| 181 |
-
inputs=[msg_input,
|
| 182 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
| 183 |
|
| 184 |
regen_btn.click(regenerate_response,
|
|
|
|
| 7 |
API_TOKEN = os.getenv("POLLY")
|
| 8 |
URL_HEADER = os.getenv("KEY")
|
| 9 |
headers = {
|
| 10 |
+
"authorization": "",
|
| 11 |
"Content-Type": 'application/json',
|
| 12 |
}
|
| 13 |
|
| 14 |
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"
|
| 15 |
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"
|
| 16 |
|
| 17 |
+
sessions = {}
|
| 18 |
+
|
| 19 |
def authorize(user, api_key):
|
| 20 |
test_data = {
|
| 21 |
"user": user,
|
|
|
|
| 27 |
|
| 28 |
print("Preparing to send the request...")
|
| 29 |
try:
|
|
|
|
|
|
|
|
|
|
| 30 |
response = requests.post(
|
| 31 |
"https://host.palple.polrambora.com/check_key_impv",
|
| 32 |
json=test_data,
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
| 35 |
if response.status_code == 200:
|
| 36 |
response_json = response.json()
|
| 37 |
headers['authorization'] = api_key
|
| 38 |
print(f"Response: {response_json}")
|
| 39 |
+
|
| 40 |
+
if api_key not in sessions:
|
| 41 |
+
sessions[api_key] = []
|
| 42 |
+
|
| 43 |
return True
|
| 44 |
else:
|
| 45 |
print(f"Failed request - Status code: {response.status_code}, Response: {response.text}")
|
|
|
|
| 55 |
return False
|
| 56 |
|
| 57 |
|
| 58 |
+
def respond(message, api_key, system_message, max_tokens, top_p, temperature):
|
| 59 |
+
history = sessions.get(api_key, [])
|
| 60 |
+
|
| 61 |
messages = []
|
| 62 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
| 63 |
if user_message:
|
|
|
|
| 85 |
"conversation_history": messages,
|
| 86 |
"input": message
|
| 87 |
}
|
| 88 |
+
|
| 89 |
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
|
| 90 |
|
| 91 |
if response.status_code == 200:
|
| 92 |
response_json = response.json()
|
| 93 |
assistant_reply = response_json["msq"]["message"][0]
|
| 94 |
+
|
| 95 |
history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
|
| 96 |
+
sessions[api_key] = history
|
| 97 |
return history, assistant_reply
|
| 98 |
else:
|
| 99 |
return history, "Error: " + response.json().get("error", "Unknown error occurred.")
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def render_message(history):
|
| 102 |
messages_html = ""
|
| 103 |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
|
|
|
|
| 151 |
history_state = gr.State([])
|
| 152 |
last_message_state = gr.State("")
|
| 153 |
|
| 154 |
+
def user_interaction(message, api_key, system_message, max_tokens, top_p, temperature):
|
| 155 |
+
history, assistant_reply = respond(message, api_key, system_message, max_tokens, top_p, temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
return render_message(history), history, "", message
|
| 157 |
|
| 158 |
def regenerate_response(history, last_message, system_message, max_tokens, top_p, temperature):
|
| 159 |
return "", []
|
| 160 |
|
| 161 |
msg_input.submit(user_interaction,
|
| 162 |
+
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
| 163 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
| 164 |
|
| 165 |
send_btn.click(user_interaction,
|
| 166 |
+
inputs=[msg_input, api_key_input, system_message, max_tokens, top_p, temperature],
|
| 167 |
outputs=[chatbot_output, history_state, msg_input, last_message_state])
|
| 168 |
|
| 169 |
regen_btn.click(regenerate_response,
|