File size: 6,762 Bytes
f7d29a6
 
 
 
10a9825
f7d29a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740f48a
f7d29a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740f48a
 
 
 
 
 
f7d29a6
 
 
 
740f48a
f7d29a6
 
 
740f48a
 
472db3e
740f48a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d29a6
 
 
 
 
 
 
 
 
 
 
740f48a
f7d29a6
740f48a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d29a6
 
 
 
 
740f48a
f7d29a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740f48a
 
 
 
f7d29a6
 
 
740f48a
 
 
 
 
 
f7d29a6
 
 
 
 
740f48a
f7d29a6
740f48a
f7d29a6
740f48a
f7d29a6
740f48a
f7d29a6
 
740f48a
f7d29a6
 
 
 
 
 
740f48a
 
 
 
f7d29a6
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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("""
<style>
    :root {
        --primary: #7017ff;
        --secondary: #ff2d55;
    }
    .header {
        background: linear-gradient(135deg, var(--primary), var(--secondary));
        color: white;
        padding: 2rem;
        border-radius: 15px;
        text-align: center;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        margin-bottom: 2rem;
    }
    .response-box {
        background: rgba(255,255,255,0.1);
        border-radius: 10px;
        padding: 1.5rem;
        margin: 1rem 0;
        border: 1px solid rgba(255,255,255,0.2);
    }
    .stButton>button {
        background: linear-gradient(45deg, var(--primary), var(--secondary)) !important;
        color: white !important;
        border-radius: 25px;
        padding: 0.8rem 2rem;
        font-weight: 600;
        transition: transform 0.2s;
    }
    .stButton>button:hover {
        transform: scale(1.05);
    }
    .movie-card {
        background: rgba(0,0,0,0.2);
        border-radius: 10px;
        padding: 1rem;
        margin: 0.5rem 0;
    }
</style>
""", unsafe_allow_html=True)

# --------------------------
# Data Loading & Processing
# --------------------------
@st.cache_resource
def load_movie_data():
    try:
        # Try loading wiki_movies dataset
        dataset = load_dataset("movie_rationales")
        df = pd.DataFrame(dataset)
        
        # Create synthetic movie data from Wikipedia snippets
        df['title'] = df['title'].apply(lambda x: x.replace("_", " "))
        df['context'] = "Title: " + df['title'] + "\nContent: " + df['text'].str[:500] + "..."
        return df.sample(1000)  # Return random 1000 entries
        
    except Exception as e:
        st.warning(f"Couldn't load dataset: {str(e)}. Using synthetic data.")
        movies = [
            {
                "title": "The Dark Knight",
                "context": "Title: The Dark Knight\nPlot: Batman faces the Joker in a battle for Gotham's soul...\nCast: Christian Bale, Heath Ledger\nYear: 2008\nDirector: Christopher Nolan"
            },
            {
                "title": "Inception",
                "context": "Title: Inception\nPlot: A thief who enters the dreams of others...\nCast: Leonardo DiCaprio, Tom Hardy\nYear: 2010\nDirector: Christopher Nolan"
            },
            {
                "title": "Pulp Fiction",
                "context": "Title: Pulp Fiction\nPlot: The lives of two mob hitmen, a boxer, and a gangster's wife intertwine...\nCast: John Travolta, Samuel L. Jackson\nYear: 1994\nDirector: Quentin Tarantino"
            }
        ]
        return pd.DataFrame(movies)

@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 Functions
# --------------------------
def get_groq_response(query, context):
    try:
        client = Groq(api_key=os.getenv("GROQ_API_KEY", "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"))
        
        prompt = f"""You are a film expert analyzing this question:
        
        Question: {query}
        
        Using these verified sources:
        {context}
        
        Provide a detailed response with:
        1. 🎬 Direct Answer
        2. πŸ“– Explanation
        3. πŸŽ₯ Relevant Scenes
        4. πŸ† Awards/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
    except Exception as e:
        return f"Error getting response: {str(e)}"

# --------------------------
# Main Application
# --------------------------
def main():
    # Load data and models
    df = load_movie_data()
    embedder, index = setup_retrieval(df)
    
    # Header Section
    st.markdown("""
    <div class="header">
        <h1>🎞️ CineMaster AI</h1>
        <h3>Your Personal Movie Encyclopedia</h3>
    </div>
    """, unsafe_allow_html=True)
    
    # Sidebar
    with st.sidebar:
        st.image("https://cdn-icons-png.flaticon.com/512/2598/2598702.png", width=120)
        st.subheader("Sample Questions")
        examples = [
            "Who played the Joker in The Dark Knight?",
            "Explain the ending of Inception",
            "List Tarantino's movies",
            "What's the plot of Pulp Fiction?",
            "Who directed The Dark Knight?"
        ]
        for ex in examples:
            st.code(ex, language="bash")
        
        st.markdown("---")
        st.markdown("**Database Info**")
        st.write(f"πŸ“Š {len(df)} movies loaded")
        st.write("πŸ” Using FAISS for vector search")
        st.write("πŸ€– Powered by Llama 3 70B")
    
    # Main Interface
    query = st.text_input("🎯 Ask any movie question:", 
                        placeholder="e.g., 'Who played the villain in The Dark Knight?'")
    
    if st.button("πŸš€ Get Expert Analysis", type="primary"):
        if query:
            with st.spinner("πŸ” Searching through movie database..."):
                query_embed = embedder.encode([query])
                _, indices = index.search(query_embed, 3)
                contexts = [df.iloc[i]['context'] for i in indices[0]]
                combined_context = "\n\n---\n\n".join(contexts)
                
            with st.spinner("πŸŽ₯ Generating cinematic insights..."):
                answer = get_groq_response(query, combined_context)
                
            st.markdown("---")
            with st.container():
                st.markdown("## 🎬 Expert Analysis")
                st.markdown(f'<div class="response-box">{answer}</div>', unsafe_allow_html=True)
                
                st.markdown("## πŸ“š Reference Materials")
                for i, ctx in enumerate(contexts, 1):
                    with st.expander(f"Source {i}", expanded=(i==1)):
                        st.markdown(f'<div class="movie-card">{ctx}</div>', unsafe_allow_html=True)
        else:
            st.warning("Please enter a movie-related question")

if __name__ == "__main__":
    main()