Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +13,7 @@ GLOBAL_WORKSPACE_ID = "wkspace_01JMHB3QHE7GRWYTGAQV7KGWYN"
|
|
| 13 |
GLOBAL_BOT_ID = "6d6e5198-a4aa-4eb6-9617-67cdde390664"
|
| 14 |
|
| 15 |
# Cookie value used in requests (should be updated with a valid cookie)
|
| 16 |
-
AUTH_COOKIE = "pscd=try.botpress.com;
|
| 17 |
-
|
| 18 |
|
| 19 |
# -------------------------------------------------------------------
|
| 20 |
# Helper functions for random bot/workspace names
|
|
@@ -22,7 +21,6 @@ AUTH_COOKIE = "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSess
|
|
| 22 |
def generate_random_name(length=5):
|
| 23 |
return ''.join(random.choices(string.ascii_letters, k=length))
|
| 24 |
|
| 25 |
-
|
| 26 |
# -------------------------------------------------------------------
|
| 27 |
# Functions to create/delete workspaces and bots
|
| 28 |
# -------------------------------------------------------------------
|
|
@@ -33,7 +31,6 @@ def create_workspace():
|
|
| 33 |
"Cookie": AUTH_COOKIE
|
| 34 |
}
|
| 35 |
payload = {"name": generate_random_name()}
|
| 36 |
-
|
| 37 |
try:
|
| 38 |
response = requests.post(ws_url, headers=headers, json=payload)
|
| 39 |
if response.status_code == 200:
|
|
@@ -56,7 +53,6 @@ def create_bot(workspace_id):
|
|
| 56 |
"Content-Type": "application/json"
|
| 57 |
}
|
| 58 |
payload = {"name": generate_random_name()}
|
| 59 |
-
|
| 60 |
try:
|
| 61 |
response = requests.post(bot_url, headers=headers, json=payload)
|
| 62 |
if response.status_code == 200:
|
|
@@ -64,11 +60,9 @@ def create_bot(workspace_id):
|
|
| 64 |
bot_id = response_json.get("bot", {}).get("id")
|
| 65 |
if not bot_id:
|
| 66 |
print("Bot ID not found in the response.")
|
| 67 |
-
|
| 68 |
# Install integration for the new bot
|
| 69 |
if bot_id:
|
| 70 |
install_bot_integration(bot_id, workspace_id)
|
| 71 |
-
|
| 72 |
return bot_id
|
| 73 |
else:
|
| 74 |
print(f"Bot creation failed with: {response.status_code}, {response.text}")
|
|
@@ -81,7 +75,6 @@ def create_bot(workspace_id):
|
|
| 81 |
def install_bot_integration(bot_id, workspace_id):
|
| 82 |
"""Install required integration for the bot to function properly"""
|
| 83 |
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
|
| 84 |
-
|
| 85 |
headers = {
|
| 86 |
"User-Agent": "Mozilla/5.0",
|
| 87 |
"Cookie": AUTH_COOKIE,
|
|
@@ -89,7 +82,6 @@ def install_bot_integration(bot_id, workspace_id):
|
|
| 89 |
"x-bot-id": bot_id,
|
| 90 |
"x-workspace-id": workspace_id
|
| 91 |
}
|
| 92 |
-
|
| 93 |
# Integration payload
|
| 94 |
payload = {
|
| 95 |
"integrations": {
|
|
@@ -98,7 +90,6 @@ def install_bot_integration(bot_id, workspace_id):
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
-
|
| 102 |
try:
|
| 103 |
response = requests.put(url, headers=headers, json=payload)
|
| 104 |
if response.status_code == 200:
|
|
@@ -119,7 +110,6 @@ def delete_bot(bot_id, workspace_id):
|
|
| 119 |
"x-workspace-id": workspace_id,
|
| 120 |
"Cookie": AUTH_COOKIE
|
| 121 |
}
|
| 122 |
-
|
| 123 |
try:
|
| 124 |
return requests.delete(url, headers=headers)
|
| 125 |
except Exception as e:
|
|
@@ -133,7 +123,6 @@ def delete_workspace(workspace_id):
|
|
| 133 |
"User-Agent": "Mozilla/5.0",
|
| 134 |
"Cookie": AUTH_COOKIE
|
| 135 |
}
|
| 136 |
-
|
| 137 |
try:
|
| 138 |
return requests.delete(url, headers=headers)
|
| 139 |
except Exception as e:
|
|
@@ -160,7 +149,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 160 |
# Process chat history into the format expected by the new API
|
| 161 |
messages = []
|
| 162 |
system_prompt = ""
|
| 163 |
-
|
| 164 |
for msg in chat_history:
|
| 165 |
if msg["role"] == "system":
|
| 166 |
system_prompt = msg["content"]
|
|
@@ -209,7 +197,33 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 209 |
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
|
| 210 |
return assistant_content, bot_id, workspace_id
|
| 211 |
|
| 212 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
else:
|
| 214 |
raise Exception("Invalid or expired bot ID.")
|
| 215 |
|
|
@@ -228,7 +242,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 228 |
if new_bot:
|
| 229 |
# Update headers with the new bot ID
|
| 230 |
headers["x-bot-id"] = new_bot
|
| 231 |
-
|
| 232 |
# Retry with new IDs
|
| 233 |
try:
|
| 234 |
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
|
@@ -240,7 +253,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 240 |
return f"Unable to get a response from the assistant.", new_bot, new_workspace
|
| 241 |
except Exception as retry_error:
|
| 242 |
return f"Unable to get a response from the assistant.", new_bot, new_workspace
|
| 243 |
-
|
| 244 |
# If we get here, all regeneration attempts failed
|
| 245 |
return "Unable to get a response from the assistant.", None, None
|
| 246 |
|
|
@@ -283,7 +295,6 @@ def chat_endpoint():
|
|
| 283 |
GLOBAL_WORKSPACE_ID = create_workspace()
|
| 284 |
if GLOBAL_WORKSPACE_ID:
|
| 285 |
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
|
| 286 |
-
|
| 287 |
# If creation failed
|
| 288 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 289 |
return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
|
|
|
|
| 13 |
GLOBAL_BOT_ID = "6d6e5198-a4aa-4eb6-9617-67cdde390664"
|
| 14 |
|
| 15 |
# Cookie value used in requests (should be updated with a valid cookie)
|
| 16 |
+
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24device_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpso%22%3A%20%7B%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%7D%2C%22__mpus%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpu%22%3A%20%7B%7D%2C%22__mpr%22%3A%20%5B%5D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24user_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%7D; _gid=GA1.2.1287628494.1744029890; _hjSession_2931810=eyJpZCI6IjYxMzliNWFhLTZkZDMtNDQxNy1hODliLTJkMjI3ZjhjMmZkZCIsImMiOjE3NDQwMjk4OTk5NzksInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MX0=; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1743857941488.1744029903713.64; __hssrc=1; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bh2jkV1V4U780wKDgoj+CN2zFBwUjPdw4PdorGkX/jA=; ory_kratos_session=MTc0NDAyOTk1OHwxWng4S0ZiNVVLblNRWldzUU9WWVVTRlVwU053aFBTOGZOa3ZBenJBQzdRejIydkc0YTJXZFVsVllvU1diNk9lQlQ1NTJtT2dJbWhDeWRDeU5TVmViNjhNclBPRDBZeFdrY3k3emh5dmlscE8wNmFLcENLLWhhbGJvblMwbUNPaUF3Y29mQ0ZKQld0ajkxeVA3YzhNWDJfaFhwRnp2LUF4VGVxSV92Yk5zTHBQT2x3X2hPZGpPUlpDOHBMTWQyeGRqeUJVTjhPRldTS09XeVA0UUswSDFyRWZLOUNFTkZwdllMT2NMUkN1dlRHcVcxUU9nbEFMaV96RU1Ea1pVbWc5SjAzdU82RGpSMk1LdHY0VlYxZHE0UT09fI-_ev3SlBoRdguqdHUF842Ku0_AZ9rX7GxVN7rV9NHs; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; _ga=GA1.1.1726154447.1735718383; _ga_W6YT9YSNLH=GS1.2.1744029893.64.1.1744030016.0.0.0; _ga_CYSS87Q508=GS1.2.1744029892.64.1.1744030016.0.0.0; __hssc=59821234.3.1744029903713; intercom-session-bjzkw2xf=RDY2ZU9VdlNaTHFvNFlwdnNmdXU1azdvVllUREF1UmZtdTNzMlVCZVFUYVpBTVFNSzFyclkwME9ySnZOd21pYTNDbE1DSVk3dk9QVVJrTzJYTHdVMEVMTXhaOTRWZ3AxSVZuYktVTnpYUFE9LS1nczlsRzhzNHdiZ2FmT3B2ZWdQNjVnPT0=--676bd54698c0b37278daa9c30607c884dedaf325; _ga_PCC6TBWJY6=GS1.1.1744029889.84.1.1744030023.0.0.0; _ga_HKHSWES9V9=GS1.1.1744029889.84.1.1744030023.57.0.2075971943"
|
|
|
|
| 17 |
|
| 18 |
# -------------------------------------------------------------------
|
| 19 |
# Helper functions for random bot/workspace names
|
|
|
|
| 21 |
def generate_random_name(length=5):
|
| 22 |
return ''.join(random.choices(string.ascii_letters, k=length))
|
| 23 |
|
|
|
|
| 24 |
# -------------------------------------------------------------------
|
| 25 |
# Functions to create/delete workspaces and bots
|
| 26 |
# -------------------------------------------------------------------
|
|
|
|
| 31 |
"Cookie": AUTH_COOKIE
|
| 32 |
}
|
| 33 |
payload = {"name": generate_random_name()}
|
|
|
|
| 34 |
try:
|
| 35 |
response = requests.post(ws_url, headers=headers, json=payload)
|
| 36 |
if response.status_code == 200:
|
|
|
|
| 53 |
"Content-Type": "application/json"
|
| 54 |
}
|
| 55 |
payload = {"name": generate_random_name()}
|
|
|
|
| 56 |
try:
|
| 57 |
response = requests.post(bot_url, headers=headers, json=payload)
|
| 58 |
if response.status_code == 200:
|
|
|
|
| 60 |
bot_id = response_json.get("bot", {}).get("id")
|
| 61 |
if not bot_id:
|
| 62 |
print("Bot ID not found in the response.")
|
|
|
|
| 63 |
# Install integration for the new bot
|
| 64 |
if bot_id:
|
| 65 |
install_bot_integration(bot_id, workspace_id)
|
|
|
|
| 66 |
return bot_id
|
| 67 |
else:
|
| 68 |
print(f"Bot creation failed with: {response.status_code}, {response.text}")
|
|
|
|
| 75 |
def install_bot_integration(bot_id, workspace_id):
|
| 76 |
"""Install required integration for the bot to function properly"""
|
| 77 |
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
|
|
|
|
| 78 |
headers = {
|
| 79 |
"User-Agent": "Mozilla/5.0",
|
| 80 |
"Cookie": AUTH_COOKIE,
|
|
|
|
| 82 |
"x-bot-id": bot_id,
|
| 83 |
"x-workspace-id": workspace_id
|
| 84 |
}
|
|
|
|
| 85 |
# Integration payload
|
| 86 |
payload = {
|
| 87 |
"integrations": {
|
|
|
|
| 90 |
}
|
| 91 |
}
|
| 92 |
}
|
|
|
|
| 93 |
try:
|
| 94 |
response = requests.put(url, headers=headers, json=payload)
|
| 95 |
if response.status_code == 200:
|
|
|
|
| 110 |
"x-workspace-id": workspace_id,
|
| 111 |
"Cookie": AUTH_COOKIE
|
| 112 |
}
|
|
|
|
| 113 |
try:
|
| 114 |
return requests.delete(url, headers=headers)
|
| 115 |
except Exception as e:
|
|
|
|
| 123 |
"User-Agent": "Mozilla/5.0",
|
| 124 |
"Cookie": AUTH_COOKIE
|
| 125 |
}
|
|
|
|
| 126 |
try:
|
| 127 |
return requests.delete(url, headers=headers)
|
| 128 |
except Exception as e:
|
|
|
|
| 149 |
# Process chat history into the format expected by the new API
|
| 150 |
messages = []
|
| 151 |
system_prompt = ""
|
|
|
|
| 152 |
for msg in chat_history:
|
| 153 |
if msg["role"] == "system":
|
| 154 |
system_prompt = msg["content"]
|
|
|
|
| 197 |
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
|
| 198 |
return assistant_content, bot_id, workspace_id
|
| 199 |
|
| 200 |
+
# Handle 403 error specifically
|
| 201 |
+
elif response.status_code == 403:
|
| 202 |
+
# Step 1: Delete the current bot and workspace
|
| 203 |
+
if bot_id and workspace_id:
|
| 204 |
+
delete_bot(bot_id, workspace_id)
|
| 205 |
+
delete_workspace(workspace_id)
|
| 206 |
+
|
| 207 |
+
# Step 2: Create a new workspace and bot
|
| 208 |
+
new_workspace_id = create_workspace()
|
| 209 |
+
if new_workspace_id:
|
| 210 |
+
new_bot_id = create_bot(new_workspace_id)
|
| 211 |
+
|
| 212 |
+
# Step 3: Install integration for the new bot
|
| 213 |
+
if new_bot_id:
|
| 214 |
+
install_bot_integration(new_bot_id, new_workspace_id)
|
| 215 |
+
|
| 216 |
+
# Step 4: Retry with the new IDs
|
| 217 |
+
headers["x-bot-id"] = new_bot_id
|
| 218 |
+
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 219 |
+
if retry_response.status_code == 200:
|
| 220 |
+
data = retry_response.json()
|
| 221 |
+
assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
|
| 222 |
+
return assistant_content, new_bot_id, new_workspace_id
|
| 223 |
+
else:
|
| 224 |
+
return f"Unable to get a response from the assistant.", new_bot_id, new_workspace_id
|
| 225 |
+
|
| 226 |
+
# Any other error status code
|
| 227 |
else:
|
| 228 |
raise Exception("Invalid or expired bot ID.")
|
| 229 |
|
|
|
|
| 242 |
if new_bot:
|
| 243 |
# Update headers with the new bot ID
|
| 244 |
headers["x-bot-id"] = new_bot
|
|
|
|
| 245 |
# Retry with new IDs
|
| 246 |
try:
|
| 247 |
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
|
|
|
| 253 |
return f"Unable to get a response from the assistant.", new_bot, new_workspace
|
| 254 |
except Exception as retry_error:
|
| 255 |
return f"Unable to get a response from the assistant.", new_bot, new_workspace
|
|
|
|
| 256 |
# If we get here, all regeneration attempts failed
|
| 257 |
return "Unable to get a response from the assistant.", None, None
|
| 258 |
|
|
|
|
| 295 |
GLOBAL_WORKSPACE_ID = create_workspace()
|
| 296 |
if GLOBAL_WORKSPACE_ID:
|
| 297 |
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
|
|
|
|
| 298 |
# If creation failed
|
| 299 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 300 |
return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
|