import streamlit as st import pandas as pd import faiss import numpy as np from datasets import load_dataset from sentence_transformers import SentenceTransformer from groq import Groq import os # -------------------------- # Configuration & Styling # -------------------------- st.set_page_config( page_title="CineMaster AI - Movie Expert", page_icon="🎬", layout="wide", initial_sidebar_state="expanded" ) st.markdown(""" """, unsafe_allow_html=True) # -------------------------- # Movie Dataset & Embeddings # -------------------------- # Replace load_movie_data() with: @st.cache_resource def load_movie_data(): dataset = load_dataset("wiki_movies", split="train") df = pd.DataFrame(dataset) df['context'] = df.apply(lambda x: f"Title: {x['title']}\nPlot: {x['plot']}\nCast: {x['cast']}", axis=1) return df @st.cache_resource def setup_retrieval(df): embedder = SentenceTransformer('all-MiniLM-L6-v2') embeddings = embedder.encode(df['context'].tolist()) index = faiss.IndexFlatL2(embeddings.shape[1]) index.add(embeddings) return embedder, index # -------------------------- # Groq API Setup # -------------------------- def get_groq_client(): return Groq( api_key=os.getenv("GROQ_API_KEY", "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") ) def movie_expert(query, context): prompt = f"""You are a film expert. Answer using this context: {context} Question: {query} Format response with: 1. 🎥 Direct Answer 2. 📖 Detailed Explanation 3. 🏆 Key Cast Members 4. 🌟 Trivia (if available) """ response = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model="llama3-70b-8192", temperature=0.3 ) return response.choices[0].message.content # -------------------------- # Main Application # -------------------------- def main(): df = load_movie_data() embedder, index = setup_retrieval(df) # Header Section st.markdown("""