Summery-Youtube / app.py
hadxu's picture
remove demo.m4a if exists
d0d6bbf
import streamlit as st
from pytube import YouTube
import yt_dlp
import os
# from faster_whisper import WhisperModel
# model = WhisperModel("large-v2", device="cpu", device_index=0, compute_type="float16")
DESCRIPTION = """
Welcome to the **YouTube Video summary** powered by Llama-2 models.
"""
st.title("YouTube Video summary")
st.markdown(DESCRIPTION)
def get_video_title(youtube_url: str) -> str:
yt = YouTube(youtube_url)
embed_url = f"https://www.youtube.com/embed/{yt.video_id}"
embed_html = f'<iframe src="{embed_url}" frameborder="0" allowfullscreen></iframe>'
return yt.title, embed_html
def initialize_session_state():
if "youtube_url" not in st.session_state:
st.session_state.youtube_url = ""
if "model_choice" not in st.session_state:
st.session_state.model_choice = "Llama2-70b"
if "setup_done" not in st.session_state:
st.session_state.setup_done = False
if "doneYoutubeurl" not in st.session_state:
st.session_state.doneYoutubeurl = ""
def sidebar():
with st.sidebar:
st.markdown("Enter the YouTube Video URL below🔗")
st.session_state.youtube_url = st.text_input("YouTube Video URL:")
if st.session_state.youtube_url:
# Get the video title
video_title, embed_html = get_video_title(st.session_state.youtube_url)
st.markdown(f"### {video_title}")
# Embed the video
st.markdown(embed_html, unsafe_allow_html=True)
sidebar()
initialize_session_state()
ydl_opts = {
'outtmpl': 'demo.m4a',
'format': 'm4a/bestaudio/best',
# ℹ️ See help(yt_dlp.postprocessor) for a list of available Postprocessors and their arguments
'postprocessors': [{ # Extract audio using ffmpeg
'key': 'FFmpegExtractAudio',
'preferredcodec': 'm4a',
}],
}
if st.session_state.youtube_url:
with st.status("Get video Audio..."):
if os.path.exists('demo.m4a'):
os.remove('demo.m4a')
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
error_code = ydl.download([st.session_state.youtube_url])
audio_file = open('demo.m4a', 'rb')
audio_bytes = audio_file.read()
st.audio(audio_bytes, format='audio/ogg')
# segments, info = model.transcribe("demo.m4a", beam_size=5)
# st.markdown("Detected language '%s' with probability %f" % (info.language, info.language_probability))
# full_response = ""
# message_placeholder = st.empty()
# for segment in segments:
# # print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text))
# full_response += segment.text + " "
# st.write(segment.text)
# message_placeholder.markdown(full_response + "▌")
# message_placeholder.markdown(full_response)