Skip to content

Commit

Permalink
yt-dlp for youtube (#66)
Browse files Browse the repository at this point in the history
* pytube replaced with yt-dlp

* yt-dlp docs link added

* yt-dlp added
  • Loading branch information
vasiliadi authored Aug 29, 2024
1 parent bf839cc commit e4085f2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ You need to replace the path to the env_file in `compose.yaml`

| | Links |
| ---|--- |
| Libraries | [streamlit](https://docs.streamlit.io)<br> [replicate](https://replicate.com/docs/get-started/python)<br>[google-generativeai](https://ai.google.dev/gemini-api/docs/get-started/python)<br>[pytube](https://pytube.io/en/latest/) |
| Libraries | [streamlit](https://docs.streamlit.io)<br> [replicate](https://replicate.com/docs/get-started/python)<br>[google-generativeai](https://ai.google.dev/gemini-api/docs/get-started/python)<br>~~[pytube](https://pytube.io/en/latest/)~~<br>[yt-dlp](https://github.com/yt-dlp/yt-dlp) |
| Docker | [Docker Best Practices](https://testdriven.io/blog/docker-best-practices/)<br><br>[Docker](https://docs.docker.com/language/python/)<br>[Dockerfile reference](https://docs.docker.com/reference/dockerfile/)<br>[Dockerfile Linter](https://hadolint.github.io/hadolint/)<br><br>[.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files)<br><br>[Docker Compose](https://docs.docker.com/compose/)<br>[Syntax for environment files in Docker Compose](https://docs.docker.com/compose/environment-variables/env-file/)<br>[Ways to set environment variables with Compose](https://docs.docker.com/compose/environment-variables/set-environment-variables/)<br>[Compose file version 3 reference](https://docs.docker.com/compose/compose-file/compose-file-v3/)|
| GitHub Actions | [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)<br>[Publishing images to Docker Hub and GitHub Packages](https://docs.github.com/en/actions/publishing-packages/publishing-docker-images#publishing-images-to-docker-hub-and-github-packages) |
| Dev Containers | [An open specification for enriching containers with development specific content and settings](https://containers.dev/)<br>[Developing inside a Container](https://code.visualstudio.com/docs/devcontainers/containers) |
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
streamlit==1.37.1
google-generativeai==0.7.2
pytube==15.0.0
requests==2.32.3
replicate==0.32.0
yt-dlp==2024.8.6
30 changes: 23 additions & 7 deletions streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import google.generativeai as genai
import replicate
import requests
from pytube import YouTube
from yt_dlp import YoutubeDL

# Google Gemini config
gemini_api_key = os.environ["GEMINI_API_KEY"]
Expand Down Expand Up @@ -54,9 +54,18 @@ def download(mode=st.session_state.mode):
with open(audio_file_name, "wb") as f:
f.write(uploaded_file.getbuffer())
case "YouTube link":
YouTube(yt_url).streams.filter(only_audio=True).order_by(
"abr"
).asc().first().download(filename=audio_file_name)
ydl_opts = {
"format": "worstaudio",
"outtmpl": "audio",
"postprocessors": [
{ # Extract audio using ffmpeg
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
}
],
}
with YoutubeDL(ydl_opts) as ydl:
ydl.download(yt_url)
case "Audio file link":
downloaded_file = requests.get(audio_link)
with open(audio_file_name, "wb") as f:
Expand Down Expand Up @@ -101,15 +110,23 @@ def summarize(audio_file_name=audio_file_name):
def transcribe(model_name=st.session_state.model_name):
match model_name:
case "whisper-diarization":
latest_model_version = replicate_client.models.get("thomasmol/whisper-diarization").versions.list()[0].id
latest_model_version = (
replicate_client.models.get("thomasmol/whisper-diarization")
.versions.list()[0]
.id
)
with open(converted_file_name, "rb") as audio:
transcription = replicate_client.run(
f"thomasmol/whisper-diarization:{latest_model_version}",
input={"file": audio, "transcript_output_format": "segments_only"},
)
return transcription
case "incredibly-fast-whisper":
latest_model_version = replicate_client.models.get("vaibhavs10/incredibly-fast-whisper").versions.list()[0].id
latest_model_version = (
replicate_client.models.get("vaibhavs10/incredibly-fast-whisper")
.versions.list()[0]
.id
)
with open(converted_file_name, "rb") as audio:
try:
transcription = replicate.run(
Expand All @@ -124,7 +141,6 @@ def transcribe(model_name=st.session_state.model_name):
st.error("Model error 😫 Try to switch model 👍", icon="🚨")
st.stop()


def detected_num_speakers(transcription):
speakers = [i["speaker"] for i in transcription[0:-1]]
return len(set(speakers))
Expand Down

0 comments on commit e4085f2

Please sign in to comment.