Spaces:
Sleeping
Sleeping
Nagesh Muralidhar
commited on
Commit
·
6c71315
1
Parent(s):
f657ad3
midterm-submission
Browse files- Dockerfile +50 -41
- 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 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
os.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
try:
|
| 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 |
-
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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|