File size: 2,185 Bytes
bf2d622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import logging
from datetime import datetime

logger = logging.getLogger(__name__)

class OutputSaver:
    def __init__(self, output_dir="outputs"):
        self.output_dir = output_dir
        os.makedirs(self.output_dir, exist_ok=True)

    def save_transcription(self, transcription, session_id=None):
        if session_id is None:
            session_id = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        filename = f"{session_id}_transcript.txt"
        filepath = os.path.join(self.output_dir, filename)
        try:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(transcription)
            logger.info(f"Transcription saved to {filepath}")
            return filepath
        except Exception as e:
            logger.error(f"Failed to save transcription: {e}")
            return None

    def save_analysis(self, analysis, session_id=None):
        if session_id is None:
            session_id = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        filename = f"{session_id}_analysis.txt"
        filepath = os.path.join(self.output_dir, filename)
        try:
            with open(filepath, 'w', encoding='utf-8') as f:
                f.write(analysis)
            logger.info(f"Analysis saved to {filepath}")
            return filepath
        except Exception as e:
            logger.error(f"Failed to save analysis: {e}")
            return None

    def save_audio(self, audio_data, sample_rate, session_id=None):
        # For saving audio, we can use librosa or soundfile
        try:
            import soundfile as sf
        except ImportError:
            logger.warning("soundfile not installed, cannot save audio")
            return None

        if session_id is None:
            session_id = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        filename = f"{session_id}_audio.wav"
        filepath = os.path.join(self.output_dir, filename)
        try:
            sf.write(filepath, audio_data, sample_rate)
            logger.info(f"Audio saved to {filepath}")
            return filepath
        except Exception as e:
            logger.error(f"Failed to save audio: {e}")
            return None