|
import os |
|
import duckdb |
|
import streamlit as st |
|
from huggingface_hub import hf_hub_download |
|
import pandas as pd |
|
import tempfile |
|
import re |
|
|
|
HF_REPO_ID = "stcoats/temp-duckdb-upload" |
|
HF_FILENAME = "ycsep.duckdb" |
|
LOCAL_PATH = "./ycsep.duckdb" |
|
|
|
st.set_page_config(layout="wide") |
|
st.title("YCSEP Audio Dataset Viewer") |
|
|
|
|
|
if not os.path.exists(LOCAL_PATH): |
|
st.write("Downloading from HF Hub...") |
|
hf_hub_download( |
|
repo_id=HF_REPO_ID, |
|
repo_type="dataset", |
|
filename=HF_FILENAME, |
|
local_dir=".", |
|
local_dir_use_symlinks=False |
|
) |
|
st.success("Download complete.") |
|
|
|
|
|
@st.cache_resource(show_spinner=False) |
|
def get_duckdb_connection(): |
|
return duckdb.connect(LOCAL_PATH, read_only=True) |
|
|
|
try: |
|
con = get_duckdb_connection() |
|
st.success("Connected to DuckDB.") |
|
except Exception as e: |
|
st.error(f"DuckDB connection failed: {e}") |
|
st.stop() |
|
|
|
|
|
query = st.text_input("Search text (case-insensitive)", "").strip() |
|
|
|
|
|
if query: |
|
search_terms = query.lower().split() |
|
conditions = " AND ".join(["LOWER(text) LIKE ?" for _ in search_terms]) |
|
sql = f""" |
|
SELECT id, channel, video_id, speaker, start_time, end_time, upload_date, text, pos_tags, audio |
|
FROM data |
|
WHERE {conditions} |
|
LIMIT 100 |
|
""" |
|
df = con.execute(sql, [f"%{term}%" for term in search_terms]).df() |
|
else: |
|
df = con.execute(""" |
|
SELECT id, channel, video_id, speaker, start_time, end_time, upload_date, text, pos_tags, audio |
|
FROM data |
|
LIMIT 100 |
|
""").df() |
|
|
|
st.markdown(f"### Showing {len(df)} results") |
|
|
|
if len(df) == 0: |
|
st.warning("No matches found.") |
|
else: |
|
def render_audio_html(audio_bytes): |
|
try: |
|
if isinstance(audio_bytes, (bytes, bytearray, memoryview)): |
|
data = bytes(audio_bytes) |
|
elif isinstance(audio_bytes, list): |
|
data = bytes(audio_bytes) |
|
else: |
|
return "" |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp: |
|
tmp.write(data) |
|
tmp.flush() |
|
return f'<audio controls style="height:20px;width:100%;"><source src="file://{tmp.name}" type="audio/mpeg"></audio>' |
|
except Exception: |
|
return "" |
|
|
|
df["Audio"] = df["audio"].apply(render_audio_html) |
|
df = df.drop(columns=["audio"]) |
|
df = df[["id", "channel", "video_id", "speaker", "start_time", "end_time", "upload_date", "text", "pos_tags", "Audio"]] |
|
|
|
from st_aggrid import AgGrid, GridOptionsBuilder |
|
|
|
st.markdown("### Results Table (Sortable with Embedded Audio)") |
|
|
|
gb = GridOptionsBuilder.from_dataframe(df.drop(columns=["Audio"])) |
|
gb.configure_default_column(resizable=True, sortable=True, filter=True) |
|
grid_options = gb.build() |
|
|
|
AgGrid(df.drop(columns=["Audio"]), gridOptions=grid_options, fit_columns_on_grid_load=True) |
|
|
|
st.markdown("### Audio Controls in Table") |
|
for i in range(len(df)): |
|
st.markdown(df.loc[i, "Audio"], unsafe_allow_html=True) |
|
|
|
|