Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,13 @@ import string
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 8 |
# Global variables to store workspace and bot IDs
|
| 9 |
GLOBAL_WORKSPACE_ID = None
|
| 10 |
GLOBAL_BOT_ID = None
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
AUTH_COOKIE = "
|
| 14 |
-
|
| 15 |
|
| 16 |
# -------------------------------------------------------------------
|
| 17 |
# Helper functions for random bot/workspace names
|
|
@@ -177,73 +177,83 @@ def delete_workspace(workspace_id):
|
|
| 177 |
|
| 178 |
|
| 179 |
# -------------------------------------------------------------------
|
| 180 |
-
#
|
| 181 |
# -------------------------------------------------------------------
|
| 182 |
-
def transcribe_audio(file_url,
|
| 183 |
"""
|
| 184 |
-
Sends
|
| 185 |
-
|
| 186 |
"""
|
| 187 |
# Prepare the headers
|
| 188 |
headers = {
|
| 189 |
"User-Agent": "Mozilla/5.0",
|
| 190 |
-
"Content-Type": "application/json",
|
| 191 |
"x-bot-id": bot_id,
|
|
|
|
|
|
|
| 192 |
"Cookie": AUTH_COOKIE
|
| 193 |
}
|
| 194 |
|
| 195 |
-
#
|
| 196 |
-
if bot_id:
|
| 197 |
-
headers["x-bot-id"] = bot_id
|
| 198 |
-
|
| 199 |
-
# Prepare the payload for audio transcription
|
| 200 |
payload = {
|
| 201 |
"type": "openai:transcribeAudio",
|
| 202 |
"input": {
|
| 203 |
"fileUrl": file_url,
|
| 204 |
-
"language": language,
|
| 205 |
"prompt": prompt,
|
| 206 |
-
"temperature":
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
|
| 211 |
max_retries = 3
|
| 212 |
-
timeout =
|
| 213 |
|
| 214 |
-
# Flag to track if we need to create new IDs
|
| 215 |
-
|
| 216 |
|
| 217 |
# Attempt to send the request
|
| 218 |
for attempt in range(max_retries):
|
| 219 |
try:
|
| 220 |
-
print(f"Attempt {attempt+1}: Sending transcription request to Botpress API")
|
| 221 |
-
if bot_id:
|
| 222 |
-
print(f"Using bot_id={bot_id}, workspace_id={workspace_id}")
|
| 223 |
-
|
| 224 |
response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 225 |
|
| 226 |
# If successful (200)
|
| 227 |
if response.status_code == 200:
|
| 228 |
data = response.json()
|
| 229 |
|
| 230 |
-
# Extract
|
|
|
|
| 231 |
segments = data.get('output', {}).get('segments', [])
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
|
|
|
|
| 234 |
print(f"Successfully received transcription from Botpress API")
|
| 235 |
-
return
|
| 236 |
-
|
| 237 |
-
#
|
| 238 |
-
elif response.status_code
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
# Handle network errors or timeouts (just retry)
|
| 244 |
-
elif response.status_code in [
|
| 245 |
print(f"Received error {response.status_code}. Retrying...")
|
| 246 |
-
time.sleep(3)
|
| 247 |
continue
|
| 248 |
|
| 249 |
# Any other error status code
|
|
@@ -253,70 +263,91 @@ def transcribe_audio(file_url, language="", prompt="get all text from this Audio
|
|
| 253 |
time.sleep(2)
|
| 254 |
continue
|
| 255 |
else:
|
| 256 |
-
return f"Unable to transcribe audio (Error {response.status_code}).",
|
| 257 |
|
| 258 |
except requests.exceptions.Timeout:
|
| 259 |
-
print(f"
|
| 260 |
if attempt < max_retries - 1:
|
| 261 |
-
time.sleep(
|
| 262 |
continue
|
| 263 |
else:
|
| 264 |
-
return "The transcription is taking too long. Please try with a shorter audio file.",
|
| 265 |
|
| 266 |
except Exception as e:
|
| 267 |
-
print(f"Error during
|
| 268 |
if attempt < max_retries - 1:
|
| 269 |
-
time.sleep(
|
| 270 |
continue
|
| 271 |
else:
|
| 272 |
-
return f"Unable to transcribe audio: {str(e)}",
|
| 273 |
|
| 274 |
-
# If we need
|
| 275 |
-
if
|
| 276 |
-
print("Creating new workspace and bot
|
| 277 |
|
| 278 |
-
# First,
|
| 279 |
if bot_id and workspace_id:
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
# Create new
|
| 284 |
new_workspace_id = create_workspace()
|
| 285 |
if not new_workspace_id:
|
| 286 |
-
return "Failed to create a new workspace
|
| 287 |
|
|
|
|
| 288 |
new_bot_id = create_bot(new_workspace_id)
|
| 289 |
if not new_bot_id:
|
| 290 |
-
return "Failed to create a new bot
|
| 291 |
|
| 292 |
-
# Update headers with new bot ID
|
| 293 |
headers["x-bot-id"] = new_bot_id
|
|
|
|
| 294 |
|
| 295 |
# Try one more time with the new IDs
|
| 296 |
try:
|
| 297 |
-
print(f"Retrying
|
| 298 |
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 299 |
|
| 300 |
if retry_response.status_code == 200:
|
| 301 |
data = retry_response.json()
|
|
|
|
|
|
|
|
|
|
| 302 |
segments = data.get('output', {}).get('segments', [])
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
print(f"Successfully received transcription with new IDs")
|
| 305 |
-
return
|
| 306 |
else:
|
| 307 |
-
print(f"
|
| 308 |
-
return f"Unable to transcribe audio with new credentials.",
|
| 309 |
|
| 310 |
except Exception as e:
|
| 311 |
-
print(f"Error with new IDs
|
| 312 |
-
return f"Unable to transcribe audio with new credentials: {str(e)}",
|
| 313 |
|
| 314 |
# Should not reach here due to the handling in the loop
|
| 315 |
-
return "Unable to transcribe audio.",
|
| 316 |
|
| 317 |
|
| 318 |
# -------------------------------------------------------------------
|
| 319 |
-
# Flask Endpoint
|
| 320 |
# -------------------------------------------------------------------
|
| 321 |
@app.route("/transcribe", methods=["POST"])
|
| 322 |
def transcribe_endpoint():
|
|
@@ -324,15 +355,11 @@ def transcribe_endpoint():
|
|
| 324 |
Expects JSON with:
|
| 325 |
{
|
| 326 |
"file_url": "string",
|
| 327 |
-
"
|
| 328 |
-
"prompt": "string" (optional),
|
| 329 |
-
"temperature": float (optional),
|
| 330 |
-
"include_full_response": boolean (optional)
|
| 331 |
}
|
| 332 |
Returns JSON with:
|
| 333 |
{
|
| 334 |
-
"transcription": "string"
|
| 335 |
-
"full_response": {} (optional)
|
| 336 |
}
|
| 337 |
"""
|
| 338 |
global GLOBAL_WORKSPACE_ID, GLOBAL_BOT_ID
|
|
@@ -340,51 +367,38 @@ def transcribe_endpoint():
|
|
| 340 |
# Parse JSON from request
|
| 341 |
data = request.get_json(force=True)
|
| 342 |
file_url = data.get("file_url", "")
|
| 343 |
-
|
| 344 |
-
prompt = data.get("prompt", "get all text from this Audio")
|
| 345 |
-
temperature = data.get("temperature", 0)
|
| 346 |
-
include_full_response = data.get("include_full_response", False)
|
| 347 |
|
| 348 |
-
# Validate input
|
| 349 |
if not file_url:
|
| 350 |
return jsonify({"error": "Missing file_url parameter"}), 400
|
| 351 |
|
| 352 |
# If we don't yet have a workspace or bot, create them
|
| 353 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 354 |
-
print("No existing IDs found
|
| 355 |
GLOBAL_WORKSPACE_ID = create_workspace()
|
| 356 |
if GLOBAL_WORKSPACE_ID:
|
| 357 |
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
|
| 358 |
|
| 359 |
# If creation failed
|
| 360 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 361 |
-
return jsonify({"
|
| 362 |
|
| 363 |
-
# Call our function that interacts with Botpress API
|
| 364 |
print(f"Sending transcription request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 365 |
-
transcription,
|
| 366 |
file_url,
|
| 367 |
-
language,
|
| 368 |
prompt,
|
| 369 |
-
temperature,
|
| 370 |
GLOBAL_BOT_ID,
|
| 371 |
GLOBAL_WORKSPACE_ID
|
| 372 |
)
|
| 373 |
|
| 374 |
# Update global IDs if they changed
|
| 375 |
if updated_bot_id != GLOBAL_BOT_ID or updated_workspace_id != GLOBAL_WORKSPACE_ID:
|
| 376 |
-
print(f"Updating global IDs
|
| 377 |
GLOBAL_BOT_ID = updated_bot_id
|
| 378 |
GLOBAL_WORKSPACE_ID = updated_workspace_id
|
| 379 |
|
| 380 |
-
|
| 381 |
-
response = {"transcription": transcription}
|
| 382 |
-
|
| 383 |
-
# Include full response data if requested
|
| 384 |
-
if include_full_response and full_data:
|
| 385 |
-
response["full_response"] = full_data
|
| 386 |
-
|
| 387 |
-
return jsonify(response)
|
| 388 |
|
| 389 |
|
| 390 |
# -------------------------------------------------------------------
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
# Global variables to store workspace and bot IDs
|
| 10 |
GLOBAL_WORKSPACE_ID = None
|
| 11 |
GLOBAL_BOT_ID = None
|
| 12 |
|
| 13 |
+
# Cookie value used in requests (should be updated with a valid cookie)
|
| 14 |
+
AUTH_COOKIE = ""
|
|
|
|
| 15 |
|
| 16 |
# -------------------------------------------------------------------
|
| 17 |
# Helper functions for random bot/workspace names
|
|
|
|
| 177 |
|
| 178 |
|
| 179 |
# -------------------------------------------------------------------
|
| 180 |
+
# Main function that calls the Botpress API endpoint for audio transcription
|
| 181 |
# -------------------------------------------------------------------
|
| 182 |
+
def transcribe_audio(file_url, prompt, bot_id, workspace_id):
|
| 183 |
"""
|
| 184 |
+
Sends the audio file URL to the Botpress API endpoint for transcription,
|
| 185 |
+
returns the transcription text and (possibly updated) bot/workspace IDs.
|
| 186 |
"""
|
| 187 |
# Prepare the headers
|
| 188 |
headers = {
|
| 189 |
"User-Agent": "Mozilla/5.0",
|
|
|
|
| 190 |
"x-bot-id": bot_id,
|
| 191 |
+
"x-workspace-id": workspace_id,
|
| 192 |
+
"Content-Type": "application/json",
|
| 193 |
"Cookie": AUTH_COOKIE
|
| 194 |
}
|
| 195 |
|
| 196 |
+
# Prepare the payload for the API
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
payload = {
|
| 198 |
"type": "openai:transcribeAudio",
|
| 199 |
"input": {
|
| 200 |
"fileUrl": file_url,
|
|
|
|
| 201 |
"prompt": prompt,
|
| 202 |
+
"temperature": 0
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
|
| 207 |
max_retries = 3
|
| 208 |
+
timeout = 120 # Increased timeout for long audio files
|
| 209 |
|
| 210 |
+
# Flag to track if we need to create new IDs due to quota exceeded
|
| 211 |
+
quota_exceeded = False
|
| 212 |
|
| 213 |
# Attempt to send the request
|
| 214 |
for attempt in range(max_retries):
|
| 215 |
try:
|
| 216 |
+
print(f"Attempt {attempt+1}: Sending transcription request to Botpress API with bot_id={bot_id}, workspace_id={workspace_id}")
|
|
|
|
|
|
|
|
|
|
| 217 |
response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 218 |
|
| 219 |
# If successful (200)
|
| 220 |
if response.status_code == 200:
|
| 221 |
data = response.json()
|
| 222 |
|
| 223 |
+
# Extract all text segments from the response
|
| 224 |
+
transcription_text = ""
|
| 225 |
segments = data.get('output', {}).get('segments', [])
|
| 226 |
+
for segment in segments:
|
| 227 |
+
segment_text = segment.get('text', '')
|
| 228 |
+
if segment_text:
|
| 229 |
+
transcription_text += segment_text + " "
|
| 230 |
|
| 231 |
+
transcription_text = transcription_text.strip()
|
| 232 |
print(f"Successfully received transcription from Botpress API")
|
| 233 |
+
return transcription_text, bot_id, workspace_id
|
| 234 |
+
|
| 235 |
+
# Check for quota exceeded error specifically
|
| 236 |
+
elif response.status_code == 403:
|
| 237 |
+
error_data = response.json()
|
| 238 |
+
error_message = error_data.get('message', '')
|
| 239 |
+
|
| 240 |
+
# Check if this is the specific quota exceeded error
|
| 241 |
+
if "has reached its usage limit for ai spend" in error_message:
|
| 242 |
+
print(f"Quota exceeded error detected: {error_message}")
|
| 243 |
+
quota_exceeded = True
|
| 244 |
+
break
|
| 245 |
+
else:
|
| 246 |
+
print(f"Received 403 error but not quota exceeded: {error_message}")
|
| 247 |
+
if attempt < max_retries - 1:
|
| 248 |
+
time.sleep(2)
|
| 249 |
+
continue
|
| 250 |
+
else:
|
| 251 |
+
return f"Unable to transcribe the audio (Error 403).", bot_id, workspace_id
|
| 252 |
|
| 253 |
# Handle network errors or timeouts (just retry)
|
| 254 |
+
elif response.status_code in [404, 408, 502, 503, 504]:
|
| 255 |
print(f"Received error {response.status_code}. Retrying...")
|
| 256 |
+
time.sleep(3) # Wait before retrying
|
| 257 |
continue
|
| 258 |
|
| 259 |
# Any other error status code
|
|
|
|
| 263 |
time.sleep(2)
|
| 264 |
continue
|
| 265 |
else:
|
| 266 |
+
return f"Unable to transcribe the audio (Error {response.status_code}).", bot_id, workspace_id
|
| 267 |
|
| 268 |
except requests.exceptions.Timeout:
|
| 269 |
+
print(f"Request timed out. Retrying...")
|
| 270 |
if attempt < max_retries - 1:
|
| 271 |
+
time.sleep(2)
|
| 272 |
continue
|
| 273 |
else:
|
| 274 |
+
return "The transcription is taking too long. Please try again with a shorter audio file.", bot_id, workspace_id
|
| 275 |
|
| 276 |
except Exception as e:
|
| 277 |
+
print(f"Error during request: {str(e)}")
|
| 278 |
if attempt < max_retries - 1:
|
| 279 |
+
time.sleep(2)
|
| 280 |
continue
|
| 281 |
else:
|
| 282 |
+
return f"Unable to transcribe the audio: {str(e)}", bot_id, workspace_id
|
| 283 |
|
| 284 |
+
# If quota exceeded, we need to create new resources
|
| 285 |
+
if quota_exceeded:
|
| 286 |
+
print("Quota exceeded. Creating new workspace and bot...")
|
| 287 |
|
| 288 |
+
# First delete the bot, then the workspace (in that order)
|
| 289 |
if bot_id and workspace_id:
|
| 290 |
+
print(f"Deleting bot {bot_id} first...")
|
| 291 |
+
delete_success = delete_bot(bot_id, workspace_id)
|
| 292 |
+
if delete_success:
|
| 293 |
+
print(f"Successfully deleted bot {bot_id}")
|
| 294 |
+
else:
|
| 295 |
+
print(f"Failed to delete bot {bot_id}")
|
| 296 |
+
|
| 297 |
+
print(f"Now deleting workspace {workspace_id}...")
|
| 298 |
+
ws_delete_success = delete_workspace(workspace_id)
|
| 299 |
+
if ws_delete_success:
|
| 300 |
+
print(f"Successfully deleted workspace {workspace_id}")
|
| 301 |
+
else:
|
| 302 |
+
print(f"Failed to delete workspace {workspace_id}")
|
| 303 |
|
| 304 |
+
# Create new workspace
|
| 305 |
new_workspace_id = create_workspace()
|
| 306 |
if not new_workspace_id:
|
| 307 |
+
return "Failed to create a new workspace after quota exceeded. Please try again later.", bot_id, workspace_id
|
| 308 |
|
| 309 |
+
# Create new bot in the new workspace
|
| 310 |
new_bot_id = create_bot(new_workspace_id)
|
| 311 |
if not new_bot_id:
|
| 312 |
+
return "Failed to create a new bot after quota exceeded. Please try again later.", new_workspace_id, workspace_id
|
| 313 |
|
| 314 |
+
# Update headers with new bot ID and workspace ID
|
| 315 |
headers["x-bot-id"] = new_bot_id
|
| 316 |
+
headers["x-workspace-id"] = new_workspace_id
|
| 317 |
|
| 318 |
# Try one more time with the new IDs
|
| 319 |
try:
|
| 320 |
+
print(f"Retrying with new bot_id={new_bot_id}, workspace_id={new_workspace_id}")
|
| 321 |
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
|
| 322 |
|
| 323 |
if retry_response.status_code == 200:
|
| 324 |
data = retry_response.json()
|
| 325 |
+
|
| 326 |
+
# Extract all text segments from the response
|
| 327 |
+
transcription_text = ""
|
| 328 |
segments = data.get('output', {}).get('segments', [])
|
| 329 |
+
for segment in segments:
|
| 330 |
+
segment_text = segment.get('text', '')
|
| 331 |
+
if segment_text:
|
| 332 |
+
transcription_text += segment_text + " "
|
| 333 |
+
|
| 334 |
+
transcription_text = transcription_text.strip()
|
| 335 |
print(f"Successfully received transcription with new IDs")
|
| 336 |
+
return transcription_text, new_bot_id, new_workspace_id
|
| 337 |
else:
|
| 338 |
+
print(f"Failed with new IDs: {retry_response.status_code}, {retry_response.text}")
|
| 339 |
+
return f"Unable to transcribe the audio with new credentials.", new_bot_id, new_workspace_id
|
| 340 |
|
| 341 |
except Exception as e:
|
| 342 |
+
print(f"Error with new IDs: {str(e)}")
|
| 343 |
+
return f"Unable to transcribe the audio with new credentials: {str(e)}", new_bot_id, new_workspace_id
|
| 344 |
|
| 345 |
# Should not reach here due to the handling in the loop
|
| 346 |
+
return "Unable to transcribe the audio.", bot_id, workspace_id
|
| 347 |
|
| 348 |
|
| 349 |
# -------------------------------------------------------------------
|
| 350 |
+
# Flask Endpoint
|
| 351 |
# -------------------------------------------------------------------
|
| 352 |
@app.route("/transcribe", methods=["POST"])
|
| 353 |
def transcribe_endpoint():
|
|
|
|
| 355 |
Expects JSON with:
|
| 356 |
{
|
| 357 |
"file_url": "string",
|
| 358 |
+
"prompt": "string"
|
|
|
|
|
|
|
|
|
|
| 359 |
}
|
| 360 |
Returns JSON with:
|
| 361 |
{
|
| 362 |
+
"transcription": "string"
|
|
|
|
| 363 |
}
|
| 364 |
"""
|
| 365 |
global GLOBAL_WORKSPACE_ID, GLOBAL_BOT_ID
|
|
|
|
| 367 |
# Parse JSON from request
|
| 368 |
data = request.get_json(force=True)
|
| 369 |
file_url = data.get("file_url", "")
|
| 370 |
+
prompt = data.get("prompt", "get all text with his lang and exatract (DON'T translate) .")
|
|
|
|
|
|
|
|
|
|
| 371 |
|
|
|
|
| 372 |
if not file_url:
|
| 373 |
return jsonify({"error": "Missing file_url parameter"}), 400
|
| 374 |
|
| 375 |
# If we don't yet have a workspace or bot, create them
|
| 376 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 377 |
+
print("No existing IDs found. Creating new workspace and bot...")
|
| 378 |
GLOBAL_WORKSPACE_ID = create_workspace()
|
| 379 |
if GLOBAL_WORKSPACE_ID:
|
| 380 |
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
|
| 381 |
|
| 382 |
# If creation failed
|
| 383 |
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
|
| 384 |
+
return jsonify({"error": "I'm currently unavailable. Please try again later."}), 500
|
| 385 |
|
| 386 |
+
# Call our function that interacts with Botpress API
|
| 387 |
print(f"Sending transcription request with existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
|
| 388 |
+
transcription, updated_bot_id, updated_workspace_id = transcribe_audio(
|
| 389 |
file_url,
|
|
|
|
| 390 |
prompt,
|
|
|
|
| 391 |
GLOBAL_BOT_ID,
|
| 392 |
GLOBAL_WORKSPACE_ID
|
| 393 |
)
|
| 394 |
|
| 395 |
# Update global IDs if they changed
|
| 396 |
if updated_bot_id != GLOBAL_BOT_ID or updated_workspace_id != GLOBAL_WORKSPACE_ID:
|
| 397 |
+
print(f"Updating global IDs: bot_id={updated_bot_id}, workspace_id={updated_workspace_id}")
|
| 398 |
GLOBAL_BOT_ID = updated_bot_id
|
| 399 |
GLOBAL_WORKSPACE_ID = updated_workspace_id
|
| 400 |
|
| 401 |
+
return jsonify({"transcription": transcription})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
|
| 404 |
# -------------------------------------------------------------------
|