Spaces:
Sleeping
Sleeping
| 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 |