leodeveloper2000
commited on
Commit
•
4464026
1
Parent(s):
f48e3c2
Upload 5 files
Browse files- app.py +17 -0
- dockerfile +30 -0
- requirements.txt +7 -0
- src/__pycache__/utils_youtube.cpython-311.pyc +0 -0
- src/utils_youtube.py +32 -0
app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Union
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from src.utils_youtube import get_youtube_transcript
|
4 |
+
|
5 |
+
## create a new FASTAPI app instance
|
6 |
+
app=FastAPI()
|
7 |
+
|
8 |
+
|
9 |
+
@app.get("/")
|
10 |
+
def home():
|
11 |
+
return {"message":"Hello World"}
|
12 |
+
|
13 |
+
# Define a function to handle the GET request at `/generate`
|
14 |
+
@app.get("/generate/")
|
15 |
+
def generate(youtubeurl,src_lang,target_lang):
|
16 |
+
transript=get_youtube_transcript(youtubeurl,src_lang,target_lang)
|
17 |
+
return {"output":f"{transript}"}
|
dockerfile
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## Use the official Python 3.9 image
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
## set the working directory to /code
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
## Copy the current directory contents in the container at /code
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
## Install the requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
12 |
+
|
13 |
+
# Set up a new user named "user"
|
14 |
+
RUN useradd user
|
15 |
+
# Switch to the "user" user
|
16 |
+
USER user
|
17 |
+
|
18 |
+
# Set home to the user's home directory
|
19 |
+
|
20 |
+
ENV HOME=/home/user \
|
21 |
+
PATH=/home/user/.local/bin:$PATH
|
22 |
+
|
23 |
+
# Set the working directory to the user's home directory
|
24 |
+
WORKDIR $HOME/app
|
25 |
+
|
26 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
27 |
+
COPY --chown=user . $HOME/app
|
28 |
+
|
29 |
+
## Start the FASTAPI App on port 7860
|
30 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.74.*
|
2 |
+
requests==2.27.*
|
3 |
+
uvicorn[standard]==0.17.*
|
4 |
+
langchain-community
|
5 |
+
typer
|
6 |
+
youtube_transcript_api
|
7 |
+
pytube
|
src/__pycache__/utils_youtube.cpython-311.pyc
ADDED
Binary file (1.45 kB). View file
|
|
src/utils_youtube.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.document_loaders import YoutubeLoader
|
2 |
+
from langchain_community.document_loaders.youtube import TranscriptFormat
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
|
6 |
+
|
7 |
+
def get_youtube_transcript(video_url,src_language, target_language) -> None:
|
8 |
+
"""get youtube transcript"""
|
9 |
+
try:
|
10 |
+
|
11 |
+
loader = YoutubeLoader.from_youtube_url(video_url,add_video_info=True,language=[src_language, "id"],translation=target_language, transcript_format=TranscriptFormat.TEXT)
|
12 |
+
|
13 |
+
#docs = loader.load_and_split(text_splitter)
|
14 |
+
transcript = loader.load()
|
15 |
+
|
16 |
+
#no transcript is found
|
17 |
+
if len(transcript) == 0:
|
18 |
+
return 'No transcript found', None, None
|
19 |
+
|
20 |
+
#metadata = transcript[0].metadata
|
21 |
+
|
22 |
+
#title = metadata.get('title')
|
23 |
+
#metadata['transcript'] = transcript[0].page_content
|
24 |
+
#metadata['youtubelink']=video_url
|
25 |
+
if not transcript:
|
26 |
+
raise ValueError(f"{src_language} transcript not available for this video.")
|
27 |
+
else:
|
28 |
+
return transcript[0].page_content
|
29 |
+
except Exception as e:
|
30 |
+
print(f"An error occurred: {e}")
|
31 |
+
return None
|
32 |
+
|