Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ app = Flask(__name__)
|
|
| 9 |
GLOBAL_WORKSPACE_ID = None
|
| 10 |
GLOBAL_BOT_ID = None
|
| 11 |
|
| 12 |
-
# Cookie value used in requests (should be updated with a valid cookie)
|
| 13 |
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1746194848836.1746538539919.88; 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; intercom-session-bjzkw2xf=TTdnZGNWUC9xNXMreE80NXhRZFNZS0pyUEdsbkJRc2JMcXdGZmcveVRPYkxZTmVnVnhqMUhJTWlDcEpVcWljeDZVYVVSblN4YnV5S0xBdWxDd2swQjZiaUZTeWl5M1psRmtoUWJwUU9FSFE9LS1Bay9zNldJTmVhUFdwMFNReFRmcXB3PT0=--68abc5394d7aab99748f3e451637cab5d9152a4c; _ga=GA1.2.1726154447.1735718383; _gid=GA1.2.1619749406.1746821934; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga_W6YT9YSNLH=GS2.2.s1746821948$o89$g0$t1746821948$j0$l0$h0; _ga_CYSS87Q508=GS2.2.s1746821949$o89$g0$t1746821949$j0$l0$h0; _hjSession_2931810=eyJpZCI6IjA1YTlkMzY3LWNiZWEtNGQ3OC04YzNiLTEzNTFjNjkxYzViZSIsImMiOjE3NDY4MjE5NTU2NjYsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _ga_PCC6TBWJY6=GS2.1.s1746821926$o120$g1$t1746821960$j0$l0$h0; _ga_HKHSWES9V9=GS2.1.s1746821929$o120$g1$t1746821960$j29$l0$h193244206; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=pIDxu4npODUGEpo7JHVQrKZ4GFaa3U+3BpgxaV5hcVw=; ory_kratos_session=MTc0NjgyMjAyOHxfUmdMaVlTQXVfSmxlT1lJSGpyU2FhbjVUTHg0R0ZsQWgtVm00M3pHcXZwVG9yNW1qRDJheUFGaFZvNmFEUVdBOThQR014RjJJbmhUMmhIV1I1ME5UVHZkTDNpMUMtQlRjZ1ZTbE55M19Pb2dHTF9vQlJoSGlBQnRRWUp0M1ZUdnVvcENLeVhOTllWNk1zMk11bFVPOWFrTzJMTTdxMmVteUozVVRDMWE5TVIxbDgzU3dUY2VQaDBRWDN4bDJUVm8yUkZQa19sb09GbzlFZHF2MDFQcVR6bVVWVVpDLXVoQ1lXMEh2LV9Sd2VNZXM1cjM4TGZPVTJqdW5xNTBETTBDYkppU0xNU2xicUk3Z2EyMnFkVmdyQT09fEYiv7pXcfXVnpIFi4JLGgDObQAchyJCoAwGDSkFkoX7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097"
|
| 14 |
|
| 15 |
# -------------------------------------------------------------------
|
|
@@ -252,9 +252,9 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 252 |
print(f"Received 403 error. Need to create new IDs.")
|
| 253 |
need_new_ids = True
|
| 254 |
break
|
| 255 |
-
|
| 256 |
elif response.status_code == 404:
|
| 257 |
-
print(f"Received
|
| 258 |
need_new_ids = True
|
| 259 |
break
|
| 260 |
|
|
@@ -332,6 +332,42 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
|
|
| 332 |
return "Unable to get a response from the assistant.", bot_id, workspace_id
|
| 333 |
|
| 334 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
# -------------------------------------------------------------------
|
| 336 |
# Flask Endpoint
|
| 337 |
# -------------------------------------------------------------------
|
|
@@ -345,7 +381,8 @@ def chat_endpoint():
|
|
| 345 |
{"role": "system", "content": "..."},
|
| 346 |
{"role": "user", "content": "..."},
|
| 347 |
...
|
| 348 |
-
]
|
|
|
|
| 349 |
}
|
| 350 |
Returns JSON with:
|
| 351 |
{
|
|
@@ -358,6 +395,7 @@ def chat_endpoint():
|
|
| 358 |
data = request.get_json(force=True)
|
| 359 |
user_input = data.get("user_input", "")
|
| 360 |
chat_history = data.get("chat_history", [])
|
|
|
|
| 361 |
|
| 362 |
# If we don't yet have a workspace or bot, create them
|
| 363 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
|
@@ -370,6 +408,18 @@ def chat_endpoint():
|
|
| 370 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 371 |
return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
|
| 372 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
# Call our function that interacts with Botpress API
|
| 374 |
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 375 |
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|
|
|
|
| 9 |
GLOBAL_WORKSPACE_ID = None
|
| 10 |
GLOBAL_BOT_ID = None
|
| 11 |
|
| 12 |
+
# Cookie value used in requests (empty for now - should be updated with a valid cookie)
|
| 13 |
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1746194848836.1746538539919.88; 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; intercom-session-bjzkw2xf=TTdnZGNWUC9xNXMreE80NXhRZFNZS0pyUEdsbkJRc2JMcXdGZmcveVRPYkxZTmVnVnhqMUhJTWlDcEpVcWljeDZVYVVSblN4YnV5S0xBdWxDd2swQjZiaUZTeWl5M1psRmtoUWJwUU9FSFE9LS1Bay9zNldJTmVhUFdwMFNReFRmcXB3PT0=--68abc5394d7aab99748f3e451637cab5d9152a4c; _ga=GA1.2.1726154447.1735718383; _gid=GA1.2.1619749406.1746821934; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga_W6YT9YSNLH=GS2.2.s1746821948$o89$g0$t1746821948$j0$l0$h0; _ga_CYSS87Q508=GS2.2.s1746821949$o89$g0$t1746821949$j0$l0$h0; _hjSession_2931810=eyJpZCI6IjA1YTlkMzY3LWNiZWEtNGQ3OC04YzNiLTEzNTFjNjkxYzViZSIsImMiOjE3NDY4MjE5NTU2NjYsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _ga_PCC6TBWJY6=GS2.1.s1746821926$o120$g1$t1746821960$j0$l0$h0; _ga_HKHSWES9V9=GS2.1.s1746821929$o120$g1$t1746821960$j29$l0$h193244206; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=pIDxu4npODUGEpo7JHVQrKZ4GFaa3U+3BpgxaV5hcVw=; ory_kratos_session=MTc0NjgyMjAyOHxfUmdMaVlTQXVfSmxlT1lJSGpyU2FhbjVUTHg0R0ZsQWgtVm00M3pHcXZwVG9yNW1qRDJheUFGaFZvNmFEUVdBOThQR014RjJJbmhUMmhIV1I1ME5UVHZkTDNpMUMtQlRjZ1ZTbE55M19Pb2dHTF9vQlJoSGlBQnRRWUp0M1ZUdnVvcENLeVhOTllWNk1zMk11bFVPOWFrTzJMTTdxMmVteUozVVRDMWE5TVIxbDgzU3dUY2VQaDBRWDN4bDJUVm8yUkZQa19sb09GbzlFZHF2MDFQcVR6bVVWVVpDLXVoQ1lXMEh2LV9Sd2VNZXM1cjM4TGZPVTJqdW5xNTBETTBDYkppU0xNU2xicUk3Z2EyMnFkVmdyQT09fEYiv7pXcfXVnpIFi4JLGgDObQAchyJCoAwGDSkFkoX7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097"
|
| 14 |
|
| 15 |
# -------------------------------------------------------------------
|
|
|
|
| 252 |
print(f"Received 403 error. Need to create new IDs.")
|
| 253 |
need_new_ids = True
|
| 254 |
break
|
| 255 |
+
|
| 256 |
elif response.status_code == 404:
|
| 257 |
+
print(f"Received 404 error. Need to create new IDs.")
|
| 258 |
need_new_ids = True
|
| 259 |
break
|
| 260 |
|
|
|
|
| 332 |
return "Unable to get a response from the assistant.", bot_id, workspace_id
|
| 333 |
|
| 334 |
|
| 335 |
+
def transcribe_audio(audio_url, bot_id):
|
| 336 |
+
"""Transcribe audio from URL using Botpress API"""
|
| 337 |
+
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
|
| 338 |
+
headers = {
|
| 339 |
+
"User-Agent": "Mozilla/5.0",
|
| 340 |
+
"x-bot-id": bot_id,
|
| 341 |
+
"Content-Type": "application/json",
|
| 342 |
+
"Cookie": AUTH_COOKIE
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
payload = {
|
| 346 |
+
"type": "openai:transcribeAudio",
|
| 347 |
+
"input": {
|
| 348 |
+
"fileUrl": audio_url,
|
| 349 |
+
"language": "",
|
| 350 |
+
"prompt": "get all text from this Audio",
|
| 351 |
+
"temperature": 0
|
| 352 |
+
}
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
try:
|
| 356 |
+
response = requests.post(botpress_url, json=payload, headers=headers, timeout=120)
|
| 357 |
+
if response.status_code == 200:
|
| 358 |
+
data = response.json()
|
| 359 |
+
# Extract text from all segments and join them
|
| 360 |
+
segments = data.get('output', {}).get('segments', [])
|
| 361 |
+
transcription = " ".join([segment.get('text', '').strip() for segment in segments])
|
| 362 |
+
return transcription
|
| 363 |
+
else:
|
| 364 |
+
print(f"Audio transcription failed: {response.status_code}, {response.text}")
|
| 365 |
+
return None
|
| 366 |
+
except Exception as e:
|
| 367 |
+
print(f"Error during audio transcription: {str(e)}")
|
| 368 |
+
return None
|
| 369 |
+
|
| 370 |
+
|
| 371 |
# -------------------------------------------------------------------
|
| 372 |
# Flask Endpoint
|
| 373 |
# -------------------------------------------------------------------
|
|
|
|
| 381 |
{"role": "system", "content": "..."},
|
| 382 |
{"role": "user", "content": "..."},
|
| 383 |
...
|
| 384 |
+
],
|
| 385 |
+
"audio_url": "string" (optional)
|
| 386 |
}
|
| 387 |
Returns JSON with:
|
| 388 |
{
|
|
|
|
| 395 |
data = request.get_json(force=True)
|
| 396 |
user_input = data.get("user_input", "")
|
| 397 |
chat_history = data.get("chat_history", [])
|
| 398 |
+
audio_url = data.get("audio_url", "")
|
| 399 |
|
| 400 |
# If we don't yet have a workspace or bot, create them
|
| 401 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
|
|
|
| 408 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 409 |
return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
|
| 410 |
|
| 411 |
+
# If audio URL is provided, transcribe it first
|
| 412 |
+
if audio_url:
|
| 413 |
+
print(f"Transcribing audio from URL: {audio_url}")
|
| 414 |
+
transcription = transcribe_audio(audio_url, GLOBAL_BOT_ID)
|
| 415 |
+
if transcription:
|
| 416 |
+
# Add transcription to user input
|
| 417 |
+
if user_input:
|
| 418 |
+
user_input = f"{user_input}\n\nTranscribed audio: {transcription}"
|
| 419 |
+
else:
|
| 420 |
+
user_input = transcription
|
| 421 |
+
print(f"Audio transcription: {transcription}")
|
| 422 |
+
|
| 423 |
# Call our function that interacts with Botpress API
|
| 424 |
print(f"Sending chat request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 425 |
assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
|