-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_transcript_sync.py
More file actions
executable file
Β·197 lines (151 loc) Β· 6.06 KB
/
Copy pathtest_transcript_sync.py
File metadata and controls
executable file
Β·197 lines (151 loc) Β· 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
"""
Test script to fetch a conversation from ElevenLabs and save to PostgreSQL
Usage: python test_transcript_sync.py CONVERSATION_ID RESEARCH_ID
"""
import sys
import os
import requests
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables FIRST before importing app modules
load_dotenv('/Users/david/GitHub/pad/vera-api/.env')
# Add parent directory to path for imports
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'vera-api'))
from app.db.base import SessionLocal
from app.models.database import Conversation, ResearchID
def fetch_elevenlabs_transcript(conversation_id: str):
"""Fetch conversation transcript from ElevenLabs API"""
api_key = os.getenv('ELEVENLABS_API_KEY')
if not api_key:
raise ValueError("ELEVENLABS_API_KEY not found in environment")
url = f"https://api.elevenlabs.io/v1/convai/conversations/{conversation_id}"
headers = {
'xi-api-key': api_key
}
print(f"π Fetching conversation: {conversation_id}")
print(f"π‘ URL: {url}")
response = requests.get(url, headers=headers)
if not response.ok:
print(f"β Error: {response.status_code} - {response.text}")
return None
data = response.json()
print(f"β
Successfully fetched conversation")
print(f"π Raw response keys: {data.keys()}")
return data
def save_to_database(conversation_data: dict, research_id: str):
"""Save conversation messages to PostgreSQL"""
db = SessionLocal()
try:
# Verify research ID exists
research_user = db.query(ResearchID).filter(
ResearchID.research_id == research_id
).first()
if not research_user:
print(f"β Research ID '{research_id}' not found in database")
print("Available research IDs:")
for rid in db.query(ResearchID).all():
print(f" - {rid.research_id}")
return False
print(f"\nβ
Found research user: {research_user.research_id} (ID: {research_user.id})")
# Check if conversation has transcript
if 'transcript' not in conversation_data:
print(f"β No 'transcript' field in conversation data")
print(f"Available fields: {conversation_data.keys()}")
return False
transcript = conversation_data['transcript']
if not isinstance(transcript, list):
print(f"β Transcript is not a list: {type(transcript)}")
return False
print(f"\nπ Processing {len(transcript)} messages...")
conversation_id = conversation_data.get('conversation_id', 'unknown')
saved_count = 0
skipped_count = 0
for idx, message in enumerate(transcript, 1):
print(f"\n--- Message {idx} ---")
print(f"Message data: {message}")
# Extract message details
role = message.get('role', 'unknown')
content = message.get('message') or message.get('text', '')
message_id = message.get('id', f'msg_{idx}')
timestamp = message.get('timestamp')
if not content:
print(f"β οΈ Skipping message {idx} - no content")
skipped_count += 1
continue
# Convert timestamp
if timestamp:
try:
msg_timestamp = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
except:
msg_timestamp = datetime.utcnow()
else:
msg_timestamp = datetime.utcnow()
# Check if message already exists
existing = db.query(Conversation).filter(
Conversation.elevenlabs_message_id == message_id,
Conversation.elevenlabs_conversation_id == conversation_id
).first()
if existing:
print(f"βοΈ Message {idx} already exists (ID: {message_id})")
skipped_count += 1
continue
# Create new conversation record
conv_record = Conversation(
research_id_fk=research_user.id,
conversation_id=conversation_id,
timestamp=msg_timestamp,
role=role,
content=content,
provider='elevenlabs',
elevenlabs_conversation_id=conversation_id,
elevenlabs_message_id=message_id
)
db.add(conv_record)
print(f"β
Saved message {idx}: {role} - {content[:50]}...")
saved_count += 1
# Commit all changes
db.commit()
print(f"\n{'='*60}")
print(f"β
Successfully saved {saved_count} messages")
print(f"βοΈ Skipped {skipped_count} messages (already existed or no content)")
print(f"π Total processed: {len(transcript)}")
print(f"{'='*60}")
return True
except Exception as e:
print(f"\nβ Error saving to database: {e}")
import traceback
traceback.print_exc()
db.rollback()
return False
finally:
db.close()
def main():
if len(sys.argv) < 3:
print("Usage: python test_transcript_sync.py CONVERSATION_ID RESEARCH_ID")
print("\nExample:")
print(" python test_transcript_sync.py conv_4501ka4xdsp2f6j80ba7nmzfk8dx goofy-test")
sys.exit(1)
conversation_id = sys.argv[1]
research_id = sys.argv[2]
print("="*60)
print("VERA Transcript Sync Test")
print("="*60)
print(f"Conversation ID: {conversation_id}")
print(f"Research ID: {research_id}")
print("="*60)
# Fetch from ElevenLabs
conversation_data = fetch_elevenlabs_transcript(conversation_id)
if not conversation_data:
print("\nβ Failed to fetch conversation from ElevenLabs")
sys.exit(1)
# Save to database
success = save_to_database(conversation_data, research_id)
if success:
print("\nβ
Test completed successfully!")
sys.exit(0)
else:
print("\nβ Test failed!")
sys.exit(1)
if __name__ == "__main__":
main()