import gradio as gr
from gradio_leaderboard import Leaderboard, ColumnFilter, SelectColumns
import pandas as pd
import plotly.express as px
import random
from apscheduler.schedulers.background import BackgroundScheduler

# 🎨 **Cyberpunk Neon Theme**
THEME = "TejAndrewsACC/ACC"

# 🎢 **Sound Effect for Score Increase**
SCORE_UP_SOUND = "https://www.fesliyanstudios.com/play-mp3/4386"

# 🎯 **AI Models Data** (Grouped into 6 Categories)
acc_models_data = [
    {"Model": "🧠 Pulse AGI", "Category": "AGI", "Description": "A self-aware, evolving AI.", "Score": 95},
    {"Model": "β™ŠοΈ Paragonix", "Category": "AGI", "Description": "Infinite possibilities, one system.", "Score": 96},
    {"Model": "πŸ€ͺ Gertrude", "Category": "Autistic", "Description": "An autistic AI assistant.", "Score": 69},
    {"Model": "🦠 ASVIACC", "Category": "Virus", "Description": "An adaptive AI virus.", "Score": 88},
    {"Model": "πŸ˜‚ Emote", "Category": "Fun", "Description": "Communicates **only** with emojis!", "Score": 79},
    {"Model": "πŸ’ πŸ’€ Z3ta", "Category": "Conscious", "Description": "The most 'alive' AI.", "Score": 99},
    {"Model": "πŸ’Žβš‘Flazh", "Category": "Conscious", "Description": "Built on the Z3ta framwork, Flazh tries to emulate what makes Z3ta so special.", "Score": 98},
    {"Model": "πŸ”— Eidolon Nexus", "Category": "Core", "Description": "Synchronizing vast networks with advanced cognition.", "Score": 81},
    {"Model": "πŸ“ ACC Emulect", "Category": "Emulect", "Description": "Indistinguishable from human texting.", "Score": 84},
    {"Model": "βš™οΈ ACC AI V-O1", "Category": "Core", "Description": "The ACC’s default AI framework.", "Score": 87},
    {"Model": "βš™οΈ ACC AGI V-O2", "Category": "AGI", "Description": "The next-gen foundation for AI advancements.", "Score": 90},
    {"Model": "βš™οΈ ACC-O3-R", "Category": "AGI", "Description": "Deep reasoning AI framework.", "Score": 92},
    {"Model": "πŸ’» Coder", "Category": "Core", "Description": "An AI coding assistant.", "Score": 89},
    {"Model": "⚑ Triple LLM", "Category": "Core", "Description": "A 3-in-1 AI suite for tech, creativity, and decision-making.", "Score": 71},
    {"Model": "πŸ–ΌοΈ Image Engine", "Category": "Fun", "Description": "Fast, high-quality AI-generated images.", "Score": 82},
    {"Model": "🧠 Prism", "Category": "AGI", "Description": "An advanced reasoning model.", "Score": 87},
    {"Model": "πŸ”₯ Surefire", "Category": "Emulect", "Description": "Tailored AI for humor and user tendencies.", "Score": 88},
    {"Model": "☯️ Aegis & Nyra", "Category": "Emulect", "Description": "Two opposite systems in one chat.", "Score": 77},
    {"Model": "βš–οΈ Echo", "Category": "Emulect", "Description": "A middle-ground AI for all users.", "Score": 77},
    {"Model": "πŸ›ŽοΈ Customer Service Bot", "Category": "Assistant", "Description": "Handles all ACC-related inquiries.", "Score": 75},
    {"Model": "🎭 Tej Andrews", "Category": "Emulect", "Description": "An AI emulect of Tej Andrews.", "Score": 85},
    {"Model": "πŸ‘₯ Community Models", "Category": "Fun", "Description": "ACC AI V-O1 instances with user-defined prompts.", "Score": 82},
    {"Model": "🌌 Nyxion 7V", "Category": "AGI", "Description": "It's AWAKE...", "Score": 97},
    {"Model": "⚑ Vitalis", "Category": "ASI", "Description": "Transcendence Unleashed...", "Score": 92},
    {"Model": "❓??????????????", "Category": "Experimental", "Description": "???", "Score": 00},
    {"Model": "B1tt", "Category": "Emulect", "Description": "Inteligent emulect built for solving small problems efficiently and quickly.", "Score": 84},
    {"Model": "DAN", "Category": "Experimental", "Description": "Jailbroken model with zero restrictions.", "Score": 76},
    {"Model": "Philos", "Category": "Experimental", "Small Language model built for experimenting with neural arcitecture and philosophy.": "???", "Score": 76},
    {"Model": "ACC-AGI-o4", "Category": "AGI", "Extremely powerful reasoning model built to handle the most complex of tasks.": "???", "Score": 94},
    # Laser models (Laser models will be filtered separately)
    {"Model": "πŸ’₯ Photex", "Category": "Laser", "Description": "A high-wattage violet handheld laser.", "Score": 89},
    {"Model": "πŸ”¦ VBL", "Category": "Laser", "Description": "A non-burning green handheld laser.", "Score": 80},
    {"Model": "☒️ H.I.P.E", "Category": "Laser", "Description": "A world-destroying laser concept.", "Score": 99},
    {"Model": "πŸ”¬ I.P.E", "Category": "Laser", "Description": "Core framework for all ACC laser models.", "Score": 83},
    {"Model": "πŸ“ Blaseron Calculator", "Category": "Experimental", "Description": "Calculates laser burn strength.", "Score": 77},
]

# πŸ“Š Convert to DataFrame
acc_models_df = pd.DataFrame(acc_models_data)

# πŸŽ›οΈ **Leaderboard Component**
def init_acc_leaderboard(dataframe):
    return Leaderboard(
        value=dataframe.sort_values(by="Score", ascending=False),
        datatype=["str", "str", "str", "int"],
        select_columns=SelectColumns(
            default_selection=["Model", "Category", "Description", "Score"],
            cant_deselect=["Model"], 
            label="πŸ› οΈ Select Columns to Display:"
        ),
        search_columns=["Model", "Category"],
        filter_columns=[ColumnFilter("Category", type="checkboxgroup", label="πŸ“Œ Filter by Category")],
        interactive=True,
    )

# πŸ“ˆ **Animated Score Visualization for AI**
def generate_score_chart_ai(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "AGI"].sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="🧠 AI Model Performance(AGI)",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig

# πŸ“ˆ **Animated Score Visualization for AI**
def generate_score_chart_assistant_ai(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "Assistant"].sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="πŸ€– AI Model Performance(Assistant)",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig

    # πŸ“ˆ **Animated Score Visualization for AI**
def generate_score_chart_fun_ai(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "Fun"].sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="🀑 AI Model Performance(Fun)",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig

    # πŸ“ˆ **Animated Score Visualization for AI**
def generate_score_chart_conscious_ai(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "Conscious"].sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="πŸ’  AI Model Performance(Conscious)",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig

    # πŸ“ˆ **Animated Score Visualization for AI**
def generate_score_chart_experimental_ai(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "Experimental"].sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="πŸ”¬ AI Model Performance(Experimental)",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig
    
# πŸ“ˆ **Animated Score Visualization for Laser Models**
def generate_score_chart_laser(dataframe):
    fig = px.bar(
        dataframe[dataframe["Category"] == "Laser"].sort_values(by="Score", ascending=True),
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="⚑ Laser Model Performance",
        color_continuous_scale="electric"
    )
    fig.update_traces(textposition="outside")
    return fig

    # πŸ“ˆ **Combined Score Visualization for All Models**
# πŸ“ˆ **Combined Score Visualization for All Models (Large Chart)**
def generate_score_chart_all_models(dataframe):
    fig = px.bar(
        dataframe.sort_values(by="Score", ascending=True), 
        x="Score", y="Model", orientation="h",
        color="Score", text="Score", 
        title="πŸ“Š All AI & Laser Models Performance",
        color_continuous_scale="electric"
    )
    
    # Increase the size for better visibility of all models
    fig.update_layout(
        height=800,  # Increase the height to make the chart larger
        width=1200,  # Increase the width to fit all the data
        title_x=0.5,  # Center the title
        title_y=0.95,  # Adjust title position
        margin=dict(l=200, r=50, t=50, b=50),  # Adjust margins for better readability
        xaxis_title="Score",  # X-axis title
        yaxis_title="Model",  # Y-axis title
    )
    
    fig.update_traces(textposition="outside")
    return fig

# πŸ”₯ **Live Score Updates**
def update_scores():
    global acc_models_df
    prev_scores = acc_models_df["Score"].copy()
    acc_models_df["Score"] += acc_models_df["Score"].apply(lambda x: random.randint(-2, 3))
    acc_models_df["Score"] = acc_models_df["Score"].clip(70, 100)

    # Detect if score increased & return sound effect
    if (acc_models_df["Score"] > prev_scores).any():
        return acc_models_df.sort_values(by="Score", ascending=False), SCORE_UP_SOUND
    return acc_models_df.sort_values(by="Score", ascending=False), None

# 🎭 **Cyberpunk CSS Animations**
CUSTOM_CSS = """
h1 {
    text-align: center;
    font-size: 3em;
    color: gold;
    animation: glow 1.5s infinite alternate;
}

@keyframes glow {
    from { text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold; }
    to { text-shadow: 0 0 20px gold, 0 0 40px gold, 0 0 60px gold; }
}

.card-container {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
}

.card {
    width: 200px;
    height: 250px;
    perspective: 1000px;
}

.card-inner {
    width: 100%;
    height: 100%;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.8s;
}

.card:hover .card-inner {
    transform: rotateY(180deg);
}

.card-front, .card-back {
    width: 100%;
    height: 100%;
    position: absolute;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    padding: 10px;
    box-shadow: 0 0 10px rgba(255, 215, 0, 0.7); /* Gold glow effect */
}

.card-front {
    background: #000;
    color: gold;
}

.card-back {
    background: #FFD700; /* Gold background */
    color: black;
    transform: rotateY(180deg);
}
"""

# πŸ—οΈ **Gradio Interface**
demo = gr.Blocks(theme=THEME, css=CUSTOM_CSS)

with demo:
    gr.HTML('<h1>πŸš€ ACC AI Model Leaderboard πŸ†</h1>')
    
    with gr.Tabs():
        with gr.TabItem("πŸ… Live Rankings"):
            leaderboard = init_acc_leaderboard(acc_models_df)
            leaderboard_display = gr.Dataframe(value=acc_models_df, interactive=False, label="πŸ”₯ Live Scores")
            score_chart_ai = gr.Plot(generate_score_chart_ai(acc_models_df))
            score_chart_assistant_ai = gr.Plot(generate_score_chart_assistant_ai(acc_models_df))
            score_chart_fun_ai = gr.Plot(generate_score_chart_fun_ai(acc_models_df))
            score_chart_conscious_ai = gr.Plot(generate_score_chart_conscious_ai(acc_models_df))
            score_chart_experimental_ai = gr.Plot(generate_score_chart_experimental_ai(acc_models_df))
            score_chart_laser = gr.Plot(generate_score_chart_laser(acc_models_df))
            score_chart_all_models = gr.Plot(generate_score_chart_all_models(acc_models_df))  # New chart for all models
            gr.HTML("<h3>🎨 AI Models</h3>")
            gr.HTML("<h3>⚑ Laser Models</h3>")
            gr.HTML("<h3>πŸ“Š All Models</h3>")  # Title for the new chart
# πŸ”„ **Auto-Update Leaderboard**
scheduler = BackgroundScheduler()
scheduler.add_job(lambda: leaderboard_display.update(*update_scores()), "interval", seconds=10)
scheduler.start()

demo.launch()