Skip to content

Commit d61c8f2

Browse files
committed
UPDATE
1 parent a06c120 commit d61c8f2

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

backend/.dockerignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Ignore data and checkpoints directories
2+
backend/data/
3+
backend/checkpoints/
4+
data
5+
checkpoints
6+
7+
# Ignore Git and version control
8+
.git
9+
.gitignore
10+
11+
# Ignore other unnecessary files
12+
*.pyc
13+
*.pyo
14+
*.pyd
15+
__pycache__
16+
*.swp
17+
.DS_Store
18+
19+
# Ignore specific files or other directories as needed
20+
# e.g., local environment files, IDE-specific files,

backend/Dockerfile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ WORKDIR /code
2020
# Copy the project files to the container
2121
COPY . /code/
2222

23-
# Install aria2 and unzip
24-
RUN apt-get update && apt-get install -y aria2 unzip
23+
# Install necessary system packages
24+
RUN apt-get update && apt-get install -y aria2 unzip && \
25+
apt-get clean && \
26+
rm -rf /var/lib/apt/lists/*
27+
2528

2629
# Download and unzip the checkpoints
2730
RUN aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://myshell-public-repo-hosting.s3.amazonaws.com/checkpoints_1226.zip -d /code -o checkpoints_1226.zip && \
2831
unzip /code/checkpoints_1226.zip -d /code/checkpoints && \
2932
rm /code/checkpoints_1226.zip
3033

31-
# If requirements.txt has other necessary packages, uncomment the next line
32-
RUN pip install -r requirements.txt
33-
# Install additional Python dependencies
34-
RUN pip install --upgrade pip && \
34+
COPY requirements.txt /code/
35+
36+
# Install Python dependencies
37+
RUN pip install -r requirements.txt && \
3538
pip install pytube moviepy fastapi uvicorn loguru youtube-dl youtube-transcript-api librosa googletrans==4.0.0-rc1
3639

3740

backend/app/api/v1/endpoints.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,37 @@
88
from fastapi.responses import FileResponse, StreamingResponse
99
import os
1010
from starlette.status import HTTP_404_NOT_FOUND
11-
11+
import re
12+
from typing import Dict
1213

1314
router = APIRouter()
1415

16+
# Global dictionary to track the status of tasks
17+
task_status: Dict[str, str] = {}
18+
1519

1620
# Retrieve the DATA_DIR from environment variable or use default
1721
DATA_DIR = os.getenv("DATA_DIR", "/home/sam/github/youtube-auto-dub/backend/data")
1822

23+
def extract_video_id(url: str) -> str:
24+
"""
25+
Extracts the video ID from a YouTube URL.
26+
"""
27+
pattern = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
28+
match = re.search(pattern, str(url))
29+
return match.group(1) if match else None
30+
1931

2032
@router.post("/download/")
21-
async def download_video(
22-
request: VideoDownloadRequest, background_tasks: BackgroundTasks
23-
):
33+
async def download_video(request: VideoDownloadRequest, background_tasks: BackgroundTasks):
34+
# Correctly access youtube_url from the request object
35+
video_id = extract_video_id(request.url)
36+
if not video_id:
37+
logger.error("Invalid YouTube URL provided.")
38+
raise HTTPException(status_code=400, detail="Invalid YouTube URL")
39+
40+
if not video_id:
41+
raise HTTPException(status_code=400, detail="Invalid YouTube URL")
2442
try:
2543
url = str(request.url)
2644
logger.info(f"Received download request for URL: {url}")
@@ -44,19 +62,26 @@ async def download_video(
4462
output_path = os.path.join(
4563
DATA_DIR, "final_videos", os.path.basename(video_path)
4664
)
65+
task_status[video_id] = "Processing"
4766

4867
background_tasks.add_task(
4968
process_audio_and_assemble_video,
5069
video_path,
5170
audio_path,
5271
output_path,
5372
translated_text,
73+
video_id
5474
)
55-
return {"message": "Video processing started.", "caption_path": caption_path}
75+
76+
return {"video_id": video_id, "status": "Processing started"}
5677
except Exception as e:
5778
logger.error(f"Error in download_video endpoint: {e}")
5879
raise HTTPException(status_code=500, detail=str(e))
5980

81+
@router.get("/status/{video_id}")
82+
def get_task_status(video_id: str):
83+
status = task_status.get(video_id, "Not Found")
84+
return {"video_id": video_id, "status": status}
6085

6186
@router.get("/download-video/{video_id}")
6287
async def download_video(video_id: str):
@@ -67,7 +92,7 @@ async def download_video(video_id: str):
6792

6893

6994
def process_audio_and_assemble_video(
70-
video_path: str, audio_path: str, output_path: str, text_to_speak: str
95+
video_path: str, audio_path: str, output_path: str, text_to_speak: str, video_id: str
7196
):
7297
try:
7398
style = "default"
@@ -94,5 +119,8 @@ def process_audio_and_assemble_video(
94119
video_path, processed_audio_path, output_path
95120
)
96121

122+
task_status[video_id] = "Completed"
123+
97124
except Exception as e:
125+
task_status[video_id] = "Failed"
98126
logger.error(f"Error in process_audio_and_assemble_video: {e}")

0 commit comments

Comments
 (0)