CoolFace
Apppublic

Atulkumar001/Universal-Entertainment-AI

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
app.py458 linesDownload Raw Back to root
1# PREMIUM NETFLIX × OPENAI APP.PY2# Copy-Paste Entire File3 4import gradio as gr5import os6import pickle7import faiss8import numpy as np9from sentence_transformers import SentenceTransformer10from huggingface_hub import InferenceClient11 12# =====================================================13# LOAD DATABASE14# =====================================================15 16DATABASE_READY = (17    os.path.exists("vector_store/faiss_index.bin")18    and19    os.path.exists("vector_store/metadata.pkl")20)21 22if DATABASE_READY:23 24    index = faiss.read_index(25        "vector_store/faiss_index.bin"26    )27 28    with open(29        "vector_store/metadata.pkl",30        "rb"31    ) as f:32 33        metadata = pickle.load(f)34 35else:36 37    index = None38    metadata = None39 40# =====================================================41# LOAD EMBEDDING MODEL42# =====================================================43 44print("Loading embedding model...")45 46model = SentenceTransformer(47    "all-MiniLM-L6-v2"48)49 50# =====================================================51# LOAD LLM52# =====================================================53 54hf_token = os.getenv("HF_TOKEN")55 56client = InferenceClient(57    model="meta-llama/Meta-Llama-3-8B-Instruct",58    token=hf_token59)60 61# =====================================================62# AI SEARCH63# =====================================================64 65def entertainment_ai(message, history):66 67    if index is None:68 69        history.append(70            {71                "role": "assistant",72                "content":73                """74⚡ Database still loading.75 76Please wait 1–2 minutes77and refresh.78"""79            }80        )81 82        return history, ""83 84    if not message.strip():85        return history, ""86 87    query_embedding = model.encode(88        [message]89    )90 91    query_embedding = np.array(92        query_embedding93    ).astype("float32")94 95    distances, indices = index.search(96        query_embedding,97        598    )99 100    retrieved_context = []101    retrieved_metadata = []102 103    for idx in indices[0]:104 105        doc = metadata[idx]106 107        retrieved_context.append(108            doc["context"]109        )110 111        retrieved_metadata.append(112            f"""113🎬 {doc['series']}114S{doc['season']}E{doc['episode']}115• {doc['title']}116"""117        )118 119    combined_context = (120        "\n\n---\n\n".join(121            retrieved_context122        )123    )124 125    messages = [126 127        {128            "role": "system",129            "content": """130You are CineMind AI,131an advanced entertainment132analysis assistant.133 134Rules:135- Answer ONLY using context136- Never invent plot details137- Explain naturally138- Sound intelligent139- Keep answers concise140"""141        },142 143        {144            "role": "user",145            "content":146            f"""147Question:148{message}149 150Retrieved Context:151{combined_context}152"""153        }154    ]155 156    try:157 158        response = (159            client.chat.completions.create(160                messages=messages,161                max_tokens=220,162                temperature=0.25163            )164        )165 166        ai_answer = (167            response168            .choices[0]169            .message.content170        )171 172    except Exception as e:173 174        ai_answer = (175            f"❌ Error: {str(e)}"176        )177 178    metadata_text = "\n".join(179        retrieved_metadata180    )181 182    final_response = f"""183{ai_answer}184 185━━━━━━━━━━━━━━━━━━━186 187**Retrieved Sources**188 189{metadata_text}190"""191 192    history.append(193    (message, final_response)194)195 196    return history, ""197 198# =====================================================199# PREMIUM CSS200# =====================================================201 202custom_css = """203 204@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Orbitron:wght@600&display=swap');205 206body {207    background: #0d0d0d !important;208}209 210.gradio-container {211 212    background:213    radial-gradient(214        circle at top,215        #1b0f10 0%,216        #0d0d0d 45%,217        #050505 100%218    ) !important;219 220    color: white !important;221    font-family: 'Inter', sans-serif;222}223 224/* HERO */225 226.hero-title {227 228    text-align: center;229 230    font-size: 56px;231 232    font-weight: 700;233 234    letter-spacing: -2px;235 236    background:237    linear-gradient(238        90deg,239        #ff3b3b,240        #ff6a6a241    );242 243    -webkit-background-clip: text;244    -webkit-text-fill-color: transparent;245 246    margin-bottom: 6px;247}248 249.hero-subtitle {250 251    text-align: center;252 253    color: #a0a0a0;254 255    font-size: 18px;256 257    margin-bottom: 25px;258}259 260/* CHAT */261 262.chat-window {263 264    border-radius: 30px !important;265 266    border:267    1px solid rgba(268        255,269        255,270        255,271        0.08272    ) !important;273 274    background:275    rgba(276        255,277        255,278        255,279        0.04280    ) !important;281 282    backdrop-filter:283    blur(20px);284 285    box-shadow:286    0 0 50px rgba(287        255,288        0,289        0,290        0.08291    );292}293 294/* INPUT */295 296textarea {297 298    background:299    rgba(300        255,301        255,302        255,303        0.06304    ) !important;305 306    color:307    white !important;308 309    border:310    1px solid rgba(311        255,312        255,313        255,314        0.08315    ) !important;316 317    border-radius:318    18px !important;319 320    padding:321    18px !important;322 323    font-size:324    16px !important;325}326 327/* BUTTON */328 329button {330 331    background:332    linear-gradient(333        135deg,334        #ff2d2d,335        #ff5e5e336    ) !important;337 338    color:339    white !important;340 341    border:342    none !important;343 344    border-radius:345    18px !important;346 347    font-weight:348    600 !important;349 350    transition:351    0.25s ease !important;352 353    box-shadow:354    0 0 20px rgba(355        255,356        0,357        0,358        0.25359    ) !important;360}361 362button:hover {363 364    transform:365    translateY(-2px);366 367    box-shadow:368    0 0 30px rgba(369        255,370        0,371        0,372        0.35373    ) !important;374}375 376footer {377    display: none !important;378}379 380"""381 382# =====================================================383# UI384# =====================================================385 386with gr.Blocks() as demo:387 388    gr.HTML(389        """390<div class="hero-title">391CineMind AI392</div>393 394<div class="hero-subtitle">395Netflix × OpenAI Entertainment Intelligence Engine396</div>397"""398    )399 400    chatbot = gr.Chatbot(401    height=600,402    elem_classes="chat-window"403)404 405    with gr.Row():406 407        user_input = gr.Textbox(408            placeholder=409            "Ask anything about TV shows, characters, plots...",410            lines=2,411            scale=8412        )413 414        send_button = gr.Button(415            "🎬 Analyze",416            scale=1417        )418 419    gr.Examples(420        examples=[421            ["Who is Ranko Zamani?"],422            ["Why does Reddington want Elizabeth?"],423            ["Who is Walter White?"],424            ["What is the Red Wedding?"],425            ["Who is Homelander?"],426            ["What happened to Rachel?"]427        ],428        inputs=user_input429    )430 431    send_button.click(432        entertainment_ai,433        inputs=[434            user_input,435            chatbot436        ],437        outputs=[438            chatbot,439            user_input440        ]441    )442 443    user_input.submit(444        entertainment_ai,445        inputs=[446            user_input,447            chatbot448        ],449        outputs=[450            chatbot,451            user_input452        ]453    )454 455demo.launch(456    css=custom_css,457    theme=gr.themes.Soft()458)