Nagesh Muralidhar commited on
Commit
6c71315
·
1 Parent(s): f657ad3

midterm-submission

Browse files
Files changed (2) hide show
  1. Dockerfile +50 -41
  2. server/utils.py +52 -85
Dockerfile CHANGED
@@ -1,42 +1,51 @@
1
- # Build frontend
2
- FROM node:18-alpine as frontend-build
3
-
4
- WORKDIR /frontend
5
- COPY podcraft/package*.json ./
6
- RUN npm install
7
- COPY podcraft/ .
8
- RUN npm run build
9
-
10
- # Build backend
11
- FROM python:3.11-slim
12
-
13
- WORKDIR /app
14
-
15
- # Install system dependencies
16
- RUN apt-get update && apt-get install -y \
17
- build-essential \
18
- && rm -rf /var/lib/apt/lists/*
19
-
20
- # Copy and install Python dependencies
21
- COPY server/requirements.txt .
22
- RUN pip install --no-cache-dir -r requirements.txt
23
-
24
- # Copy backend code
25
- COPY server/ .
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
-
41
- # Start FastAPI application
 
 
 
 
 
 
 
 
 
42
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # Build frontend
2
+ FROM node:18-alpine as frontend-build
3
+
4
+ WORKDIR /frontend
5
+ COPY podcraft/package*.json ./
6
+ RUN npm install
7
+ COPY podcraft/ .
8
+ RUN npm run build
9
+
10
+ # Build backend
11
+ FROM python:3.11-slim
12
+
13
+ WORKDIR /app
14
+
15
+ # Install system dependencies
16
+ RUN apt-get update && apt-get install -y \
17
+ build-essential \
18
+ && rm -rf /var/lib/apt/lists/*
19
+
20
+ # Create a non-root user to run the application
21
+ RUN useradd -m -u 1000 appuser
22
+
23
+ # Copy and install Python dependencies
24
+ COPY server/requirements.txt .
25
+ RUN pip install --no-cache-dir -r requirements.txt
26
+
27
+ # Copy backend code
28
+ COPY server/ .
29
+
30
+ # Create necessary directories
31
+ RUN mkdir -p audio_storage transcripts logs
32
+
33
+ # Set ownership and permissions for application directories
34
+ RUN chown -R appuser:appuser /app \
35
+ && chmod -R 755 /app \
36
+ && chmod -R 777 /app/audio_storage \
37
+ && chmod -R 777 /app/transcripts \
38
+ && chmod -R 777 /app/logs
39
+
40
+ # Copy frontend build to a static directory
41
+ COPY --from=frontend-build /frontend/dist /app/static
42
+ RUN chown -R appuser:appuser /app/static
43
+
44
+ # Switch to non-root user
45
+ USER appuser
46
+
47
+ # Expose port
48
+ EXPOSE 7860
49
+
50
+ # Start FastAPI application
51
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
server/utils.py CHANGED
@@ -1,85 +1,52 @@
1
- import os
2
- import json
3
- import uuid
4
- import logging
5
- import stat
6
-
7
- # Configure logging
8
- logger = logging.getLogger(__name__)
9
-
10
- # Create transcripts directory if it doesn't exist
11
- TRANSCRIPTS_DIR = os.path.join(os.path.dirname(__file__), "transcripts")
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
33
- transcript = {
34
- "id": str(uuid.uuid4()),
35
- "podcastScript": podcast_script,
36
- "topic": user_query
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
 
1
+ import os
2
+ import json
3
+ import uuid
4
+ import logging
5
+
6
+ # Configure logging
7
+ logger = logging.getLogger(__name__)
8
+
9
+ # Create transcripts directory if it doesn't exist
10
+ 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
17
+ transcript = {
18
+ "id": str(uuid.uuid4()),
19
+ "podcastScript": podcast_script,
20
+ "topic": user_query
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
+ with open(TRANSCRIPTS_FILE, 'w') as f:
42
+ json.dump(transcripts, f, indent=2)
43
+ logger.info("Successfully saved transcript")
44
+
45
+ except Exception as e:
46
+ logger.error(f"Error saving transcript: {str(e)}")
47
+ # Create directory if it doesn't exist
48
+ os.makedirs(os.path.dirname(TRANSCRIPTS_FILE), exist_ok=True)
49
+ # Try to save just this transcript
50
+ with open(TRANSCRIPTS_FILE, 'w') as f:
51
+ json.dump([transcript], f, indent=2)
52
+ logger.info("Saved single transcript after error")