Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1026,52 +1026,6 @@ def delete_project(project_id):
|
|
| 1026 |
#-------------------------
|
| 1027 |
import math
|
| 1028 |
|
| 1029 |
-
|
| 1030 |
-
|
| 1031 |
-
# Fixed server code
|
| 1032 |
-
|
| 1033 |
-
AGENT_ID = os.getenv("AGENT_ID", "agent_01jy2d4krmfkn9r7v7wdyqtjct")
|
| 1034 |
-
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
| 1035 |
-
|
| 1036 |
-
@app.route('/api/projects/<project_id>/initiate-call', methods=['POST'])
|
| 1037 |
-
def initiate_call(project_id):
|
| 1038 |
-
"""
|
| 1039 |
-
This is the definitive, correct version. It uses the official 'get-signed-url'
|
| 1040 |
-
endpoint, which is the only one guaranteed to work for authenticated agents.
|
| 1041 |
-
"""
|
| 1042 |
-
logger.info(f"[INITIATE] Received request for project: {project_id}")
|
| 1043 |
-
|
| 1044 |
-
uid = verify_token(request.headers.get('Authorization'))
|
| 1045 |
-
if not uid:
|
| 1046 |
-
return jsonify({'error': 'Unauthorized'}), 401
|
| 1047 |
-
|
| 1048 |
-
if not ELEVENLABS_API_KEY:
|
| 1049 |
-
logger.error("[INITIATE] ELEVENLABS_API_KEY is not set on the server.")
|
| 1050 |
-
return jsonify({'error': 'Server configuration error.'}), 500
|
| 1051 |
-
|
| 1052 |
-
# This is the correct URL as per the official ElevenLabs documentation and our debug results.
|
| 1053 |
-
url = f"https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?agent_id={AGENT_ID}"
|
| 1054 |
-
headers = {"xi-api-key": ELEVENLABS_API_KEY}
|
| 1055 |
-
|
| 1056 |
-
try:
|
| 1057 |
-
response = requests.get(url, headers=headers, timeout=15)
|
| 1058 |
-
response.raise_for_status() # This will raise an error for 4xx/5xx responses
|
| 1059 |
-
|
| 1060 |
-
data = response.json()
|
| 1061 |
-
signed_url = data.get("signed_url")
|
| 1062 |
-
|
| 1063 |
-
if not signed_url:
|
| 1064 |
-
logger.error("[INITIATE] ElevenLabs response missing 'signed_url'.")
|
| 1065 |
-
return jsonify({'error': 'Failed to retrieve session URL from provider.'}), 502
|
| 1066 |
-
|
| 1067 |
-
logger.info("[INITIATE] Successfully retrieved signed URL.")
|
| 1068 |
-
# The React SDK expects a JSON object with the key "signed_url".
|
| 1069 |
-
return jsonify({"signed_url": signed_url}), 200
|
| 1070 |
-
|
| 1071 |
-
except requests.exceptions.RequestException as e:
|
| 1072 |
-
logger.error(f"[INITIATE] Error calling ElevenLabs API: {e}")
|
| 1073 |
-
return jsonify({'error': 'Could not connect to AI service provider.'}), 504
|
| 1074 |
-
|
| 1075 |
@app.route('/api/debug/test-agent', methods=['GET'])
|
| 1076 |
def test_agent():
|
| 1077 |
"""
|
|
|
|
| 1026 |
#-------------------------
|
| 1027 |
import math
|
| 1028 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1029 |
@app.route('/api/debug/test-agent', methods=['GET'])
|
| 1030 |
def test_agent():
|
| 1031 |
"""
|