Spaces:
Sleeping
Sleeping
File size: 2,456 Bytes
6c71315 f6b7a05 6c71315 f6b7a05 6c71315 f6b7a05 6c71315 f6b7a05 6c71315 |
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 |
import os
import json
import uuid
import logging
# 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")
def save_transcript(podcast_script: str, user_query: str) -> None:
"""Save podcast transcript to JSON file."""
# Process the topic to match filename format
topic = user_query.lower().strip().replace(" ", "_")
topic = topic.replace("?", "").replace("!", "").replace(".", "") # Remove punctuation
# Create new transcript entry
transcript = {
"id": str(uuid.uuid4()),
"podcastScript": podcast_script,
"topic": topic.replace("_", " ") # Store topic with spaces for matching
}
try:
# Load existing 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:
logger.warning("Error reading transcripts file, initializing empty list")
transcripts = []
else:
transcripts = []
# Check if transcript for this topic already exists
for i, existing in enumerate(transcripts):
if existing.get("topic") == transcript["topic"]:
# Update existing transcript
transcripts[i] = transcript
break
else:
# Append new transcript if no existing one was found
transcripts.append(transcript)
# Save updated transcripts
with open(TRANSCRIPTS_FILE, 'w') as f:
json.dump(transcripts, f, indent=2)
logger.info(f"Successfully saved transcript for topic: {transcript['topic']}")
except Exception as e:
logger.error(f"Error saving transcript: {str(e)}")
# Create directory if it doesn't exist
os.makedirs(os.path.dirname(TRANSCRIPTS_FILE), exist_ok=True)
# Try to save just this transcript
with open(TRANSCRIPTS_FILE, 'w') as f:
json.dump([transcript], f, indent=2)
logger.info("Saved single transcript after error") |