Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -955,6 +955,7 @@ import math
|
|
| 955 |
|
| 956 |
|
| 957 |
|
|
|
|
| 958 |
AGENT_ID = os.getenv("AGENT_ID", "agent_01jy1vv1hqe2b9x0yyp6ayrdxj")
|
| 959 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 960 |
|
|
@@ -974,6 +975,9 @@ def initiate_call(project_id):
|
|
| 974 |
logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
|
| 975 |
return jsonify({'error': 'Server configuration error.'}), 500
|
| 976 |
|
|
|
|
|
|
|
|
|
|
| 977 |
url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
|
| 978 |
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 979 |
|
|
@@ -995,6 +999,53 @@ def initiate_call(project_id):
|
|
| 995 |
logger.error(f"[INITIATE] Error calling ElevenLabs API: {e}")
|
| 996 |
return jsonify({'error': 'Could not connect to AI service provider.'}), 504
|
| 997 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 998 |
|
| 999 |
@app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
|
| 1000 |
def log_call_usage(project_id):
|
|
|
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
+
# Fixed server code
|
| 959 |
AGENT_ID = os.getenv("AGENT_ID", "agent_01jy1vv1hqe2b9x0yyp6ayrdxj")
|
| 960 |
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 961 |
|
|
|
|
| 975 |
logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
|
| 976 |
return jsonify({'error': 'Server configuration error.'}), 500
|
| 977 |
|
| 978 |
+
# ✅ CRITICAL FIX: Since your agent is PUBLIC, you have two options:
|
| 979 |
+
|
| 980 |
+
# OPTION 1: Use signed URL (recommended for production)
|
| 981 |
url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
|
| 982 |
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 983 |
|
|
|
|
| 999 |
logger.error(f"[INITIATE] Error calling ElevenLabs API: {e}")
|
| 1000 |
return jsonify({'error': 'Could not connect to AI service provider.'}), 504
|
| 1001 |
|
| 1002 |
+
# Alternative for public agents (simpler but less secure):
|
| 1003 |
+
@app.route('/api/projects/<project_id>/initiate-call-simple', methods=['POST'])
|
| 1004 |
+
def initiate_call_simple(project_id):
|
| 1005 |
+
"""
|
| 1006 |
+
For public agents, you can also return the agent ID directly.
|
| 1007 |
+
Less secure but simpler for testing.
|
| 1008 |
+
"""
|
| 1009 |
+
logger.info(f"[INITIATE-SIMPLE] Received request for project: {project_id}")
|
| 1010 |
+
|
| 1011 |
+
uid = verify_token(request.headers.get('Authorization'))
|
| 1012 |
+
if not uid:
|
| 1013 |
+
return jsonify({'error': 'Unauthorized'}), 401
|
| 1014 |
+
|
| 1015 |
+
# For public agents, you can return the agent ID directly
|
| 1016 |
+
return jsonify({"agent_id": AGENT_ID}), 200
|
| 1017 |
+
|
| 1018 |
+
# Debug endpoint to test your setup
|
| 1019 |
+
@app.route('/api/debug/test-agent', methods=['GET'])
|
| 1020 |
+
def test_agent():
|
| 1021 |
+
"""Debug endpoint to test if agent exists and is accessible"""
|
| 1022 |
+
if not ELEVENLABS_API_KEY:
|
| 1023 |
+
return jsonify({'error': 'API key not set'}), 500
|
| 1024 |
+
|
| 1025 |
+
# Test 1: Check if agent exists
|
| 1026 |
+
agent_url = f"https://api.elevenlabs.io/v1/convai/agents/{AGENT_ID}"
|
| 1027 |
+
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 1028 |
+
|
| 1029 |
+
try:
|
| 1030 |
+
agent_response = requests.get(agent_url, headers=headers, timeout=10)
|
| 1031 |
+
|
| 1032 |
+
# Test 2: Try to get signed URL
|
| 1033 |
+
signed_url_endpoint = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
|
| 1034 |
+
signed_response = requests.get(signed_url_endpoint, headers=headers, timeout=10)
|
| 1035 |
+
|
| 1036 |
+
return jsonify({
|
| 1037 |
+
'agent_id': AGENT_ID,
|
| 1038 |
+
'agent_exists': {
|
| 1039 |
+
'status_code': agent_response.status_code,
|
| 1040 |
+
'response': agent_response.json() if agent_response.status_code == 200 else agent_response.text
|
| 1041 |
+
},
|
| 1042 |
+
'signed_url_test': {
|
| 1043 |
+
'status_code': signed_response.status_code,
|
| 1044 |
+
'response': signed_response.json() if signed_response.status_code == 200 else signed_response.text
|
| 1045 |
+
}
|
| 1046 |
+
})
|
| 1047 |
+
except Exception as e:
|
| 1048 |
+
return jsonify({'error': str(e), 'agent_id': AGENT_ID})
|
| 1049 |
|
| 1050 |
@app.route('/api/projects/<project_id>/log-call-usage', methods=['POST'])
|
| 1051 |
def log_call_usage(project_id):
|