Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -436,25 +436,42 @@ def get_agent_briefing(project_id):
|
|
| 436 |
@app.route('/api/ai/get-agent-url', methods=['GET'])
|
| 437 |
def get_agent_url():
|
| 438 |
uid = verify_token(request.headers.get('Authorization'))
|
| 439 |
-
if not uid:
|
|
|
|
|
|
|
| 440 |
user_data = db_ref.child(f'users/{uid}').get()
|
| 441 |
if not user_data or user_data.get('credits', 0) < 3:
|
| 442 |
return jsonify({'error': 'Insufficient credits to start a call. Minimum 3 required.'}), 402
|
|
|
|
| 443 |
try:
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
except Exception as e:
|
| 455 |
-
logger.error(
|
| 456 |
return jsonify({'error': 'An internal server error occurred.'}), 500
|
| 457 |
|
|
|
|
| 458 |
@app.route('/api/projects/<string:project_id>/sessions/end', methods=['POST'])
|
| 459 |
def end_session_and_analyze(project_id): # <-- CORRECTED: Added project_id parameter
|
| 460 |
uid = verify_token(request.headers.get('Authorization'))
|
|
|
|
| 436 |
@app.route('/api/ai/get-agent-url', methods=['GET'])
|
| 437 |
def get_agent_url():
|
| 438 |
uid = verify_token(request.headers.get('Authorization'))
|
| 439 |
+
if not uid:
|
| 440 |
+
return jsonify({'error': 'Unauthorized'}), 401
|
| 441 |
+
|
| 442 |
user_data = db_ref.child(f'users/{uid}').get()
|
| 443 |
if not user_data or user_data.get('credits', 0) < 3:
|
| 444 |
return jsonify({'error': 'Insufficient credits to start a call. Minimum 3 required.'}), 402
|
| 445 |
+
|
| 446 |
try:
|
| 447 |
+
url = "https://api.elevenlabs.io/v1/convai/conversation/get-signed-url"
|
| 448 |
+
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 449 |
+
params = {"agent_id": ELEVENLABS_AGENT_ID}
|
| 450 |
+
|
| 451 |
+
resp = requests.get(url, headers=headers, params=params, timeout=15)
|
| 452 |
+
if not resp.ok:
|
| 453 |
+
logger.error(
|
| 454 |
+
"ElevenLabs signed-URL failed: status=%s body=%s",
|
| 455 |
+
resp.status_code, resp.text[:500]
|
| 456 |
+
)
|
| 457 |
+
# Surface the real reason to the client (no mystery 502)
|
| 458 |
+
return jsonify({
|
| 459 |
+
'error': 'ElevenLabs rejected the request',
|
| 460 |
+
'status': resp.status_code,
|
| 461 |
+
'detail': resp.text
|
| 462 |
+
}), resp.status_code
|
| 463 |
+
|
| 464 |
+
logger.info("Successfully generated ElevenLabs signed URL for user %s.", uid)
|
| 465 |
+
return jsonify(resp.json()), 200
|
| 466 |
+
|
| 467 |
+
except requests.RequestException as e:
|
| 468 |
+
logger.error("Network error talking to ElevenLabs: %s", e)
|
| 469 |
+
return jsonify({'error': 'Failed to connect to ElevenLabs', 'detail': str(e)}), 502
|
| 470 |
except Exception as e:
|
| 471 |
+
logger.error("Error in get_agent_url for user %s: %s", uid, e)
|
| 472 |
return jsonify({'error': 'An internal server error occurred.'}), 500
|
| 473 |
|
| 474 |
+
|
| 475 |
@app.route('/api/projects/<string:project_id>/sessions/end', methods=['POST'])
|
| 476 |
def end_session_and_analyze(project_id): # <-- CORRECTED: Added project_id parameter
|
| 477 |
uid = verify_token(request.headers.get('Authorization'))
|