File size: 11,048 Bytes
f92da22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
"""
Medical Transcription Retriever from Langfuse
Retrieves medical transcriptions from Langfuse traces and saves them locally.
"""

import os
import json
import time
from datetime import datetime, timedelta
from dotenv import load_dotenv
from langfuse import Langfuse

# Load environment variables
load_dotenv()


class MedicalTranscriptionRetriever:
    """Retrieves medical transcriptions from Langfuse traces."""

    def __init__(self):
        """Initialize the retriever with Langfuse credentials."""
        self.public_key = os.getenv('LANGFUSE_PUBLIC_KEY')
        self.secret_key = os.getenv('LANGFUSE_SECRET_KEY')
        self.host = os.getenv('LANGFUSE_HOST', 'https://cloud.langfuse.com')

        if not self.public_key or not self.secret_key:
            raise ValueError("Missing Langfuse keys in .env file")

        self.client = Langfuse(
            public_key=self.public_key,
            secret_key=self.secret_key,
            host=self.host
        )

    def extract_transcription_from_input(self, input_data):
        """Extract transcription from document input data."""
        if isinstance(input_data, str):
            if "Voici le document:" in input_data:
                parts = input_data.split("Voici le document:")
                if len(parts) > 1:
                    return parts[1].strip()

        elif isinstance(input_data, dict):
            # Search in messages if it's a dict with messages
            if 'messages' in input_data:
                for message in input_data['messages']:
                    if isinstance(message, dict) and message.get('role') == 'user':
                        content = message.get('content', '')
                        if isinstance(content, str) and "Voici le document:" in content:
                            parts = content.split("Voici le document:")
                            if len(parts) > 1:
                                return parts[1].strip()

            # Search in other dict keys
            for key, value in input_data.items():
                if isinstance(value, str) and "Voici le document:" in value:
                    parts = value.split("Voici le document:")
                    if len(parts) > 1:
                        return parts[1].strip()

        elif isinstance(input_data, list):
            for message in input_data:
                if isinstance(message, dict):
                    content = message.get('content', '')
                    if isinstance(content, str) and "Voici le document:" in content:
                        parts = content.split("Voici le document:")
                        if len(parts) > 1:
                            return parts[1].strip()

        return None

    def get_traces_with_transcriptions(self, limit=50, days_back=7):
        """Retrieve traces containing medical transcriptions."""
        print(f"πŸ” Searching for transcriptions in the last {limit} traces...")

        try:
            # Retrieve traces
            traces = self.client.get_traces(limit=limit)
            print(f"βœ… {len(traces.data)} traces retrieved")

            transcriptions = []

            for i, trace in enumerate(traces.data):
                print(
                    f"πŸ“‹ Analyzing trace {i+1}/{len(traces.data)}: {trace.id}")

                try:
                    # Check if trace.input contains a transcription
                    if hasattr(trace, 'input') and trace.input is not None:
                        transcription = self.extract_transcription_from_input(
                            trace.input)

                        if transcription:
                            trans_info = {
                                'trace_id': trace.id,
                                'trace_name': trace.name,
                                'user_id': trace.user_id,
                                'trace_timestamp': trace.timestamp.isoformat() if trace.timestamp else None,
                                'transcription': transcription,
                                'extracted_at': datetime.now().isoformat()
                            }
                            transcriptions.append(trans_info)
                            print(f"  βœ… Transcription found and extracted!")
                        else:
                            print(f"  ❌ No transcription found in trace.input")
                    else:
                        print(f"  ⚠️ No input available for this trace")

                except Exception as e:
                    print(f"  ⚠️ Error analyzing trace {trace.id}: {e}")
                    continue

                # Delay between requests to avoid rate limiting
                if i < len(traces.data) - 1:  # Don't wait after the last trace
                    time.sleep(1)  # Wait 1 second between each trace

            print(f"\nπŸ“Š Summary: {len(transcriptions)} transcriptions found")
            return transcriptions

        except Exception as e:
            print(f"❌ Error retrieving traces: {e}")
            return []

    def save_transcriptions(self, transcriptions, filename=None):
        """Save transcriptions to a JSON file."""
        if not filename:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"medical_transcriptions_{timestamp}.json"

        try:
            # Concatenate all transcriptions into a single string
            transcription_texts = [trans['transcription']
                                   for trans in transcriptions]
            concatenated_transcription = "\n\n".join(transcription_texts)

            # Save as an object with transcription as a single string
            data_to_save = {
                "extracted_at": datetime.now().isoformat(),
                "total_transcriptions": len(transcriptions),
                "transcription": concatenated_transcription
            }

            with open(filename, 'w', encoding='utf-8') as f:
                json.dump(data_to_save, f, ensure_ascii=False, indent=2)

            print(f"πŸ’Ύ Transcriptions saved to {filename}")
            return filename

        except Exception as e:
            print(f"❌ Error during save: {e}")
            return None

    def save_transcriptions_by_user(self, transcriptions):
        """Save transcriptions by user in separate files."""
        if not transcriptions:
            print("πŸ“­ No transcriptions to save")
            return

        # Create transcriptions directory if it doesn't exist
        transcriptions_dir = "transcriptions"
        if not os.path.exists(transcriptions_dir):
            os.makedirs(transcriptions_dir)
            print(f"πŸ“ Directory '{transcriptions_dir}' created")

        # Group transcriptions by user_id
        user_transcriptions = {}
        for trans in transcriptions:
            user_id = trans.get('user_id', 'unknown')
            if user_id not in user_transcriptions:
                user_transcriptions[user_id] = []
            user_transcriptions[user_id].append(trans)

        # Save one file per user (only if user_id contains .rtf)
        saved_files = []
        for user_id, user_trans in user_transcriptions.items():
            # Check if user_id contains .rtf
            if '.rtf' not in user_id:
                print(f"⏭️ Skipped {user_id} (no .rtf)")
                continue

            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"transcriptions_{user_id}_{timestamp}.json"
            filepath = os.path.join(transcriptions_dir, filename)

            try:
                # Concatenate all transcriptions into a single string
                transcription_texts = [trans['transcription']
                                       for trans in user_trans]
                concatenated_transcription = "\n\n".join(transcription_texts)

                # Save as an object with transcription as a single string
                data_to_save = {
                    "user_id": user_id,
                    "extracted_at": datetime.now().isoformat(),
                    "total_transcriptions": len(user_trans),
                    "transcription": concatenated_transcription
                }

                with open(filepath, 'w', encoding='utf-8') as f:
                    json.dump(data_to_save, f, ensure_ascii=False, indent=2)

                saved_files.append(filepath)
                print(f"πŸ’Ύ Saved transcriptions for {user_id}: {filename}")

            except Exception as e:
                print(f"❌ Error saving transcriptions for {user_id}: {e}")

        print(f"\nπŸ“Š Summary: {len(saved_files)} files saved")
        return saved_files

    def display_transcriptions_summary(self, transcriptions):
        """Display a summary of retrieved transcriptions."""
        if not transcriptions:
            print("πŸ“­ No transcriptions to display")
            return

        print("\nπŸ“Š TRANSCRIPTIONS SUMMARY")
        print("=" * 50)
        print(f"Total transcriptions: {len(transcriptions)}")

        # Group by user
        user_counts = {}
        for trans in transcriptions:
            user_id = trans.get('user_id', 'unknown')
            user_counts[user_id] = user_counts.get(user_id, 0) + 1

        print(f"Unique users: {len(user_counts)}")
        for user_id, count in user_counts.items():
            print(f"  - {user_id}: {count} transcriptions")

    def run(self, limit=50, save_to_file=True, save_by_user=True):
        """Run the complete transcription retrieval process."""
        print("πŸš€ Starting medical transcription retrieval...")
        print("=" * 60)

        # Retrieve transcriptions
        transcriptions = self.get_traces_with_transcriptions(limit=limit)

        if not transcriptions:
            print("❌ No transcriptions found")
            return None

        # Display summary
        self.display_transcriptions_summary(transcriptions)

        # Save transcriptions
        saved_files = []
        if save_to_file:
            saved_file = self.save_transcriptions(transcriptions)
            if saved_file:
                saved_files.append(saved_file)

        if save_by_user:
            user_files = self.save_transcriptions_by_user(transcriptions)
            saved_files.extend(user_files)

        print(f"\nβœ… Retrieval completed! {len(saved_files)} files saved")
        return saved_files


def main():
    """Main function to run the transcription retriever."""
    print("πŸ₯ Medical Transcription Retriever")
    print("=" * 40)

    try:
        retriever = MedicalTranscriptionRetriever()
        saved_files = retriever.run(
            limit=50, save_to_file=True, save_by_user=True)

        if saved_files:
            print(f"\nπŸŽ‰ Success! Files saved: {len(saved_files)}")
        else:
            print("\n❌ No files were saved")

    except Exception as e:
        print(f"❌ Error: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()