import streamlit as st
import pandas as pd
from huggingface_hub import HfApi, hf_hub_download
import os
# --- CONFIGURATION ---
REPO_ID = "Razoa/nirinsbeat"
TOKEN = os.environ.get("HF_TOKEN")
PAYPAL_EMAIL = "nirinsbeatz@gmail.com"
st.set_page_config(page_title="NIRINSBEATZ", layout="centered")
# --- STYLE CSS (COMPACT & PRO) ---
st.markdown("""
""", unsafe_allow_html=True)
# --- DATABASE ---
@st.cache_data(show_spinner=False)
def load_data():
try:
file_path = hf_hub_download(repo_id=REPO_ID, filename="data.csv", repo_type="space", token=TOKEN)
return pd.read_csv(file_path)
except:
return pd.DataFrame(columns=["titre", "categorie", "prix", "file_url", "image_url"])
def save_and_upload(dataframe):
dataframe.to_csv("data.csv", index=False)
api = HfApi()
api.upload_file(path_or_fileobj="data.csv", path_in_repo="data.csv", repo_id=REPO_ID, repo_type="space", token=TOKEN)
st.cache_data.clear()
df = load_data()
# --- HEADER ---
st.markdown('
NIRINSBEATZ
', unsafe_allow_html=True)
# --- SIDEBAR ---
choice = st.sidebar.selectbox("Genre", ["🏠 Home", "🔥 Afrobeat", "🎵 Afropop", "⚡ Drill", "💃 Zouk"])
secret_input = st.sidebar.text_input("Search beat...", placeholder="Type here...")
admin_mode = (secret_input == "admin-login")
# --- CATALOGUE ---
if not admin_mode:
q_params = st.query_params
is_success = q_params.get("payment") == "success"
target_id = q_params.get("id")
clean_choice = choice.split(" ")[1] if " " in choice else choice
display_df = df if clean_choice == "Home" else df[df['categorie'] == clean_choice]
if display_df.empty:
st.info("No beats found.")
else:
for index, row in display_df.iterrows():
try:
val = int(''.join(filter(str.isdigit, str(row['prix']))))
usd = round(val / 4500, 2)
except: usd = "19.99"
ret_url = f"https://huggingface.co/spaces/{REPO_ID}?payment=success&id={index}"
pay_url = f"https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business={PAYPAL_EMAIL}&item_name={row['titre']}&amount={usd}¤cy_code=USD&return={ret_url}"
st.markdown(f'''
{row["titre"]}
${usd} ({row["prix"]})
''', unsafe_allow_html=True)
st.markdown(f'
', unsafe_allow_html=True)
if is_success and str(target_id) == str(index):
st.markdown(f'
⬇️ DOWNLOAD HQ', unsafe_allow_html=True)
else:
st.markdown(f'
💳 Buy Now', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
# --- ADMIN PANEL ---
else:
st.header("🔑 Admin")
if st.text_input("Password", type="password") == "admin123":
t1, t2 = st.tabs(["Upload", "Manage"])
with t1:
with st.form("up"):
t = st.text_input("Title"); c = st.selectbox("Cat", ["Afrobeat", "Afropop", "Drill", "Zouk"]); p = st.text_input("Price (Ar)")
a = st.file_uploader("Audio", type=["mp3", "wav"]); i = st.file_uploader("Image", type=["jpg", "png"])
if st.form_submit_button("Publish"):
api = HfApi(); ap = f"beats/{a.name}"; ip = f"images/{i.name}"
api.upload_file(path_or_fileobj=a, path_in_repo=ap, repo_id=REPO_ID, repo_type="space", token=TOKEN)
api.upload_file(path_or_fileobj=i, path_in_repo=ip, repo_id=REPO_ID, repo_type="space", token=TOKEN)
new = pd.DataFrame([{"titre":t, "categorie":c, "prix":p, "file_url":f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{ap}", "image_url":f"https://huggingface.co/spaces/{REPO_ID}/resolve/main/{ip}"}])
df = pd.concat([df, new], ignore_index=True); save_and_upload(df); st.rerun()
with t2:
for idx, row in df.iterrows():
if st.button(f"Delete {row['titre']}", key=f"del_{idx}"):
df = df.drop(idx); save_and_upload(df); st.rerun()