Spaces:
Sleeping
Sleeping
fabiogra
commited on
Commit
•
ae202f9
1
Parent(s):
b07d122
fix: cleanup and small fixes
Browse files- app/footer.py +3 -7
- app/header.py +5 -5
- app/helpers.py +9 -19
- app/service/youtube.py +9 -4
app/footer.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from streamlit.components.v1 import html
|
4 |
-
from htbuilder import HtmlElement, div, a, p, img, styles
|
5 |
from htbuilder.units import percent, px
|
|
|
6 |
|
7 |
|
8 |
def image(src_as_string, **style):
|
@@ -50,10 +49,7 @@ def layout(*args):
|
|
50 |
st.markdown(style, unsafe_allow_html=True)
|
51 |
|
52 |
for arg in args:
|
53 |
-
if isinstance(arg, str):
|
54 |
-
body(arg)
|
55 |
-
|
56 |
-
elif isinstance(arg, HtmlElement):
|
57 |
body(arg)
|
58 |
|
59 |
st.markdown(str(foot), unsafe_allow_html=True)
|
|
|
1 |
import streamlit as st
|
2 |
+
from htbuilder import HtmlElement, a, div, img, p, styles
|
|
|
|
|
3 |
from htbuilder.units import percent, px
|
4 |
+
from streamlit.components.v1 import html
|
5 |
|
6 |
|
7 |
def image(src_as_string, **style):
|
|
|
49 |
st.markdown(style, unsafe_allow_html=True)
|
50 |
|
51 |
for arg in args:
|
52 |
+
if isinstance(arg, str) or isinstance(arg, HtmlElement):
|
|
|
|
|
|
|
53 |
body(arg)
|
54 |
|
55 |
st.markdown(str(foot), unsafe_allow_html=True)
|
app/header.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
|
3 |
-
from helpers import switch_page
|
4 |
-
from style import CSS
|
5 |
import logging
|
6 |
|
|
|
|
|
7 |
from streamlit_option_menu import option_menu
|
|
|
8 |
|
9 |
logging.basicConfig(
|
10 |
format="%(asctime)s %(levelname)-8s %(message)s",
|
@@ -17,7 +16,6 @@ def header(logo_and_title=True):
|
|
17 |
if "first_run" not in st.session_state:
|
18 |
st.session_state.first_run = True
|
19 |
for key in [
|
20 |
-
"search_results",
|
21 |
"selected_value",
|
22 |
"filename",
|
23 |
"executed",
|
@@ -30,6 +28,8 @@ def header(logo_and_title=True):
|
|
30 |
st.session_state[key] = None
|
31 |
st.session_state.video_options = []
|
32 |
st.session_state.tot_delay = 0
|
|
|
|
|
33 |
if "page" not in st.session_state:
|
34 |
st.session_state.page = "Karaoke"
|
35 |
switch_page(st.session_state.page)
|
|
|
|
|
|
|
|
|
|
|
1 |
import logging
|
2 |
|
3 |
+
import streamlit as st
|
4 |
+
from helpers import switch_page
|
5 |
from streamlit_option_menu import option_menu
|
6 |
+
from style import CSS
|
7 |
|
8 |
logging.basicConfig(
|
9 |
format="%(asctime)s %(levelname)-8s %(message)s",
|
|
|
16 |
if "first_run" not in st.session_state:
|
17 |
st.session_state.first_run = True
|
18 |
for key in [
|
|
|
19 |
"selected_value",
|
20 |
"filename",
|
21 |
"executed",
|
|
|
28 |
st.session_state[key] = None
|
29 |
st.session_state.video_options = []
|
30 |
st.session_state.tot_delay = 0
|
31 |
+
if "search_results" not in st.session_state:
|
32 |
+
st.session_state.search_results = []
|
33 |
if "page" not in st.session_state:
|
34 |
st.session_state.page = "Karaoke"
|
35 |
switch_page(st.session_state.page)
|
app/helpers.py
CHANGED
@@ -1,6 +1,8 @@
|
|
|
|
1 |
import random
|
|
|
2 |
from io import BytesIO
|
3 |
-
import
|
4 |
|
5 |
import matplotlib.pyplot as plt
|
6 |
import numpy as np
|
@@ -8,28 +10,11 @@ import requests
|
|
8 |
import streamlit as st
|
9 |
from PIL import Image
|
10 |
from pydub import AudioSegment
|
11 |
-
from base64 import b64encode
|
12 |
-
from pathlib import Path
|
13 |
from streamlit.runtime.scriptrunner import RerunData, RerunException
|
14 |
from streamlit.source_util import get_pages
|
15 |
from streamlit_player import st_player
|
16 |
|
17 |
extensions = ["mp3", "wav", "ogg", "flac"] # we will look for all those file types.
|
18 |
-
example_songs = [1, 2, 3]
|
19 |
-
|
20 |
-
|
21 |
-
def img_to_bytes(img_path):
|
22 |
-
img_bytes = Path(img_path).read_bytes()
|
23 |
-
encoded = b64encode(img_bytes).decode()
|
24 |
-
return encoded
|
25 |
-
|
26 |
-
|
27 |
-
# @st.cache_data(show_spinner=False)
|
28 |
-
def img_to_html(img_path):
|
29 |
-
img_html = "<div style='display: flex; justify-content: center; align-items: center; height: 50vh;'><img src='data:image/png;base64,{}' class='img-fluid' style='max-width: 100%; max-height: 100%;' ></div>".format(
|
30 |
-
img_to_bytes(img_path)
|
31 |
-
)
|
32 |
-
return img_html
|
33 |
|
34 |
|
35 |
@st.cache_data(show_spinner=False)
|
@@ -76,8 +61,13 @@ def plot_audio(_audio_segment: AudioSegment, *args, **kwargs) -> Image.Image:
|
|
76 |
return image
|
77 |
|
78 |
|
|
|
|
|
|
|
|
|
|
|
79 |
def get_random_song():
|
80 |
-
sample_songs =
|
81 |
name, url = random.choice(list(sample_songs.items()))
|
82 |
return name, url
|
83 |
|
|
|
1 |
+
import json
|
2 |
import random
|
3 |
+
from base64 import b64encode
|
4 |
from io import BytesIO
|
5 |
+
from pathlib import Path
|
6 |
|
7 |
import matplotlib.pyplot as plt
|
8 |
import numpy as np
|
|
|
10 |
import streamlit as st
|
11 |
from PIL import Image
|
12 |
from pydub import AudioSegment
|
|
|
|
|
13 |
from streamlit.runtime.scriptrunner import RerunData, RerunException
|
14 |
from streamlit.source_util import get_pages
|
15 |
from streamlit_player import st_player
|
16 |
|
17 |
extensions = ["mp3", "wav", "ogg", "flac"] # we will look for all those file types.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
@st.cache_data(show_spinner=False)
|
|
|
61 |
return image
|
62 |
|
63 |
|
64 |
+
@st.cache_data(show_spinner=False)
|
65 |
+
def load_list_of_songs():
|
66 |
+
return json.load(open("sample_songs.json"))
|
67 |
+
|
68 |
+
|
69 |
def get_random_song():
|
70 |
+
sample_songs = load_list_of_songs()
|
71 |
name, url = random.choice(list(sample_songs.items()))
|
72 |
return name, url
|
73 |
|
app/service/youtube.py
CHANGED
@@ -1,12 +1,17 @@
|
|
|
|
1 |
import os
|
2 |
-
|
3 |
-
import yt_dlp
|
4 |
import string
|
5 |
import time
|
6 |
-
import
|
|
|
7 |
import streamlit as st
|
|
|
8 |
from pytube import Search
|
9 |
|
|
|
|
|
|
|
10 |
|
11 |
def _sanitize_filename(filename):
|
12 |
safe_chars = "-_.() %s%s" % (
|
@@ -24,7 +29,7 @@ def download_audio_from_youtube(url, output_path):
|
|
24 |
|
25 |
with yt_dlp.YoutubeDL() as ydl:
|
26 |
info_dict = ydl.extract_info(url, download=False)
|
27 |
-
if info_dict.get("duration") > 360:
|
28 |
st.error("Song is too long. Please use a song no longer than 6 minutes.")
|
29 |
return
|
30 |
video_title = info_dict.get("title", None)
|
|
|
1 |
+
import logging
|
2 |
import os
|
3 |
+
import re
|
|
|
4 |
import string
|
5 |
import time
|
6 |
+
from typing import List
|
7 |
+
|
8 |
import streamlit as st
|
9 |
+
import yt_dlp
|
10 |
from pytube import Search
|
11 |
|
12 |
+
logger = logging.getLogger("pytube")
|
13 |
+
logger.setLevel(logging.ERROR)
|
14 |
+
|
15 |
|
16 |
def _sanitize_filename(filename):
|
17 |
safe_chars = "-_.() %s%s" % (
|
|
|
29 |
|
30 |
with yt_dlp.YoutubeDL() as ydl:
|
31 |
info_dict = ydl.extract_info(url, download=False)
|
32 |
+
if info_dict.get("duration", 0) > 360:
|
33 |
st.error("Song is too long. Please use a song no longer than 6 minutes.")
|
34 |
return
|
35 |
video_title = info_dict.get("title", None)
|