Skip to content

Commit

Permalink
fix: session error between pages, improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiogra committed Jul 16, 2023
1 parent 59e9730 commit a4f7be2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
13 changes: 4 additions & 9 deletions app/header.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import logging

import streamlit as st
from helpers import switch_page
from loguru import logger as log
from streamlit_option_menu import option_menu
from style import CSS

logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
from helpers import switch_page
from style import CSS

DEFAULT_PAGE = "Separate"

Expand Down Expand Up @@ -56,6 +50,7 @@ def header(logo_and_title=True):
key="",
)
if page != st.session_state.get("page", DEFAULT_PAGE):
log.info(f"Go to {page}")
switch_page(page)

if logo_and_title:
Expand Down
1 change: 1 addition & 0 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def _standardize_name(name: str) -> str:

@st.cache_data(show_spinner=False)
def switch_page(page_name: str):
st.session_state.executed = False
st.session_state.page = page_name

page_name = _standardize_name(page_name)
Expand Down
8 changes: 7 additions & 1 deletion app/pages/Karaoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def show_karaoke(pathname):
with st.columns([1, 4, 1])[1]:
if events.name == "onPlay":
sess.player_restart = True
log.info(f"Play Karaoke - {sess.selected_value}")
log.info(f"Play Karaoke - {sess.selected_value} - {sess.delay}s delay")

elif (
events.name == "onProgress"
Expand Down Expand Up @@ -140,7 +140,9 @@ def body():
if selected_value is None or selected_value == "":
with yt_cols[2]:
if st.button("🎲 Random song", use_container_width=True):
reset_karaoke()
sess.last_dir, sess.url = get_random_song()
log.info(f"Random song - {sess.last_dir}")
sess.selected_value = sess.last_dir
sess.random_song = True
sess.video_options = []
Expand Down Expand Up @@ -182,6 +184,7 @@ def body():
with st.spinner(
"Separating vocals from music, it could take a few minutes... Don't close this page!"
):
log.info(f"Separating vocals from {sess.selected_value}")
sess.filename = download_audio_from_youtube(sess.url, in_path)
if sess.filename is None:
st.stop()
Expand All @@ -193,6 +196,7 @@ def body():
if cancel_button.button(
"Cancel", use_container_width=True, type="secondary"
):
log.info(f"Cancel separation of vocals from {filename}")
st.experimental_rerun()
separate(
input=in_path / filename,
Expand All @@ -205,6 +209,8 @@ def body():
sess.last_dir = ".".join(sess.filename.split(".")[:-1])
sess.executed = True
cancel_button.empty()
log.info(f"Separating Done - {sess.selected_value}")

else:
sess.executed = True

Expand Down
6 changes: 4 additions & 2 deletions app/service/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import string
from typing import List

from loguru import logger as log
import streamlit as st
import yt_dlp
from pytube import Search
Expand All @@ -26,7 +27,7 @@ def download_audio_from_youtube(url, output_path):
if not os.path.exists(output_path):
os.makedirs(output_path)

with yt_dlp.YoutubeDL() as ydl:
with yt_dlp.YoutubeDL({"quiet": True}) as ydl:
info_dict = ydl.extract_info(url, download=False)
if info_dict.get("duration", 0) > 360:
st.error("Song is too long. Please use a song no longer than 6 minutes.")
Expand All @@ -43,7 +44,7 @@ def download_audio_from_youtube(url, output_path):
}
],
"outtmpl": os.path.join(output_path, video_title),
#'quiet': True,
"quiet": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
Expand All @@ -56,6 +57,7 @@ def query_youtube(query: str) -> Search:


def search_youtube(query: str, limit=5) -> List:
log.info(f"{query}")
if len(query) > 3:
search = query_youtube(query + " lyrics")
st.session_state.search_results = search.results
Expand Down

0 comments on commit a4f7be2

Please sign in to comment.