Spaces:
Sleeping
Sleeping
Nagesh Muralidhar
commited on
Commit
·
f657ad3
1
Parent(s):
b66dccc
midterm-submission
Browse files- Dockerfile +5 -1
- server/utils.py +45 -12
Dockerfile
CHANGED
|
@@ -26,11 +26,15 @@ COPY server/ .
|
|
| 26 |
|
| 27 |
# Create necessary directories with proper permissions
|
| 28 |
RUN mkdir -p audio_storage transcripts logs && \
|
| 29 |
-
chmod 777 audio_storage transcripts logs
|
|
|
|
| 30 |
|
| 31 |
# Copy frontend build to a static directory
|
| 32 |
COPY --from=frontend-build /frontend/dist /app/static
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
# Expose port
|
| 35 |
EXPOSE 7860
|
| 36 |
|
|
|
|
| 26 |
|
| 27 |
# Create necessary directories with proper permissions
|
| 28 |
RUN mkdir -p audio_storage transcripts logs && \
|
| 29 |
+
chmod -R 777 audio_storage transcripts logs && \
|
| 30 |
+
chown -R nobody:nogroup audio_storage transcripts logs
|
| 31 |
|
| 32 |
# Copy frontend build to a static directory
|
| 33 |
COPY --from=frontend-build /frontend/dist /app/static
|
| 34 |
|
| 35 |
+
# Switch to non-root user
|
| 36 |
+
USER nobody
|
| 37 |
+
|
| 38 |
# Expose port
|
| 39 |
EXPOSE 7860
|
| 40 |
|
server/utils.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import json
|
| 3 |
import uuid
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
logger = logging.getLogger(__name__)
|
|
@@ -11,6 +12,21 @@ TRANSCRIPTS_DIR = os.path.join(os.path.dirname(__file__), "transcripts")
|
|
| 11 |
os.makedirs(TRANSCRIPTS_DIR, exist_ok=True)
|
| 12 |
TRANSCRIPTS_FILE = os.path.join(TRANSCRIPTS_DIR, "podcasts.json")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def save_transcript(podcast_script: str, user_query: str) -> None:
|
| 15 |
"""Save podcast transcript to JSON file."""
|
| 16 |
# Create new transcript entry
|
|
@@ -21,32 +37,49 @@ def save_transcript(podcast_script: str, user_query: str) -> None:
|
|
| 21 |
}
|
| 22 |
|
| 23 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Load existing transcripts
|
|
|
|
| 25 |
if os.path.exists(TRANSCRIPTS_FILE):
|
| 26 |
try:
|
| 27 |
with open(TRANSCRIPTS_FILE, 'r') as f:
|
| 28 |
transcripts = json.load(f)
|
| 29 |
if not isinstance(transcripts, list):
|
| 30 |
transcripts = []
|
| 31 |
-
except json.JSONDecodeError:
|
| 32 |
-
logger.warning("Error reading transcripts file, initializing empty list")
|
| 33 |
transcripts = []
|
| 34 |
-
else:
|
| 35 |
-
transcripts = []
|
| 36 |
|
| 37 |
# Append new transcript
|
| 38 |
transcripts.append(transcript)
|
| 39 |
|
| 40 |
# Save updated transcripts
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
logger.info("Successfully saved transcript")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
logger.error(f"Error saving transcript: {str(e)}")
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import json
|
| 3 |
import uuid
|
| 4 |
import logging
|
| 5 |
+
import stat
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logger = logging.getLogger(__name__)
|
|
|
|
| 12 |
os.makedirs(TRANSCRIPTS_DIR, exist_ok=True)
|
| 13 |
TRANSCRIPTS_FILE = os.path.join(TRANSCRIPTS_DIR, "podcasts.json")
|
| 14 |
|
| 15 |
+
# Ensure directory has correct permissions
|
| 16 |
+
try:
|
| 17 |
+
os.chmod(TRANSCRIPTS_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 permissions
|
| 18 |
+
except Exception as e:
|
| 19 |
+
logger.warning(f"Could not set permissions on transcripts directory: {e}")
|
| 20 |
+
|
| 21 |
+
# Initialize empty transcripts file if it doesn't exist
|
| 22 |
+
if not os.path.exists(TRANSCRIPTS_FILE):
|
| 23 |
+
try:
|
| 24 |
+
with open(TRANSCRIPTS_FILE, 'w') as f:
|
| 25 |
+
json.dump([], f)
|
| 26 |
+
os.chmod(TRANSCRIPTS_FILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777 permissions
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logger.warning(f"Could not initialize transcripts file: {e}")
|
| 29 |
+
|
| 30 |
def save_transcript(podcast_script: str, user_query: str) -> None:
|
| 31 |
"""Save podcast transcript to JSON file."""
|
| 32 |
# Create new transcript entry
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# Ensure directory exists with correct permissions
|
| 41 |
+
os.makedirs(TRANSCRIPTS_DIR, exist_ok=True)
|
| 42 |
+
os.chmod(TRANSCRIPTS_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 43 |
+
|
| 44 |
# Load existing transcripts
|
| 45 |
+
transcripts = []
|
| 46 |
if os.path.exists(TRANSCRIPTS_FILE):
|
| 47 |
try:
|
| 48 |
with open(TRANSCRIPTS_FILE, 'r') as f:
|
| 49 |
transcripts = json.load(f)
|
| 50 |
if not isinstance(transcripts, list):
|
| 51 |
transcripts = []
|
| 52 |
+
except (json.JSONDecodeError, PermissionError) as e:
|
| 53 |
+
logger.warning(f"Error reading transcripts file: {e}, initializing empty list")
|
| 54 |
transcripts = []
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Append new transcript
|
| 57 |
transcripts.append(transcript)
|
| 58 |
|
| 59 |
# Save updated transcripts
|
| 60 |
+
try:
|
| 61 |
+
with open(TRANSCRIPTS_FILE, 'w') as f:
|
| 62 |
+
json.dump(transcripts, f, indent=2)
|
| 63 |
+
os.chmod(TRANSCRIPTS_FILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 64 |
logger.info("Successfully saved transcript")
|
| 65 |
+
except PermissionError:
|
| 66 |
+
# Try creating a new file in case the original has wrong permissions
|
| 67 |
+
temp_file = os.path.join(TRANSCRIPTS_DIR, f"podcasts_temp_{uuid.uuid4()}.json")
|
| 68 |
+
with open(temp_file, 'w') as f:
|
| 69 |
+
json.dump(transcripts, f, indent=2)
|
| 70 |
+
os.chmod(temp_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 71 |
+
os.replace(temp_file, TRANSCRIPTS_FILE)
|
| 72 |
+
logger.info("Successfully saved transcript using temporary file")
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
logger.error(f"Error saving transcript: {str(e)}")
|
| 76 |
+
# Try one last time with a new file
|
| 77 |
+
try:
|
| 78 |
+
temp_file = os.path.join(TRANSCRIPTS_DIR, f"podcasts_new_{uuid.uuid4()}.json")
|
| 79 |
+
with open(temp_file, 'w') as f:
|
| 80 |
+
json.dump([transcript], f, indent=2)
|
| 81 |
+
os.chmod(temp_file, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 82 |
+
logger.info("Saved single transcript to new file")
|
| 83 |
+
except Exception as e2:
|
| 84 |
+
logger.error(f"Final attempt to save transcript failed: {str(e2)}")
|
| 85 |
+
raise
|