Spaces:
Sleeping
Sleeping
import os | |
import json | |
import uuid | |
import logging | |
import stat | |
# Configure logging | |
logger = logging.getLogger(__name__) | |
# Create transcripts directory if it doesn't exist | |
TRANSCRIPTS_DIR = os.path.join(os.path.dirname(__file__), "transcripts") | |
os.makedirs(TRANSCRIPTS_DIR, exist_ok=True) | |
TRANSCRIPTS_FILE = os.path.join(TRANSCRIPTS_DIR, "podcasts.json") | |
# Ensure directory has correct permissions | |
try: | |
os.chmod(TRANSCRIPTS_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 permissions | |
except Exception as e: | |
logger.warning(f"Could not set permissions on transcripts directory: {e}") | |
# Initialize empty transcripts file if it doesn't exist | |
if not os.path.exists(TRANSCRIPTS_FILE): | |
try: | |
with open(TRANSCRIPTS_FILE, 'w') as f: | |
json.dump([], f) | |
os.chmod(TRANSCRIPTS_FILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 permissions | |
except Exception as e: | |
logger.warning(f"Could not initialize transcripts file: {e}") | |
def save_transcript(podcast_script: str, user_query: str) -> None: | |
"""Save podcast transcript to JSON file.""" | |
# Create new transcript entry | |
transcript = { | |
"id": str(uuid.uuid4()), | |
"podcastScript": podcast_script, | |
"topic": user_query | |
} | |
try: | |
# Ensure directory exists with correct permissions | |
os.makedirs(TRANSCRIPTS_DIR, exist_ok=True) | |
os.chmod(TRANSCRIPTS_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
# Load existing transcripts | |
transcripts = [] | |
if os.path.exists(TRANSCRIPTS_FILE): | |
try: | |
with open(TRANSCRIPTS_FILE, 'r') as f: | |
transcripts = json.load(f) | |
if not isinstance(transcripts, list): | |
transcripts = [] | |
except (json.JSONDecodeError, PermissionError) as e: | |
logger.warning(f"Error reading transcripts file: {e}, initializing empty list") | |
transcripts = [] | |
# Append new transcript | |
transcripts.append(transcript) | |
# Save updated transcripts | |
try: | |
with open(TRANSCRIPTS_FILE, 'w') as f: | |
json.dump(transcripts, f, indent=2) | |
os.chmod(TRANSCRIPTS_FILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
logger.info("Successfully saved transcript") | |
except PermissionError: | |
# Try creating a new file in case the original has wrong permissions | |
temp_file = os.path.join(TRANSCRIPTS_DIR, f"podcasts_temp_{uuid.uuid4()}.json") | |
with open(temp_file, 'w') as f: | |
json.dump(transcripts, f, indent=2) | |
os.chmod(temp_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
os.replace(temp_file, TRANSCRIPTS_FILE) | |
logger.info("Successfully saved transcript using temporary file") | |
except Exception as e: | |
logger.error(f"Error saving transcript: {str(e)}") | |
# Try one last time with a new file | |
try: | |
temp_file = os.path.join(TRANSCRIPTS_DIR, f"podcasts_new_{uuid.uuid4()}.json") | |
with open(temp_file, 'w') as f: | |
json.dump([transcript], f, indent=2) | |
os.chmod(temp_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) | |
logger.info("Saved single transcript to new file") | |
except Exception as e2: | |
logger.error(f"Final attempt to save transcript failed: {str(e2)}") | |
raise |