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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import gradio as gr
import numpy as np
from sklearn.preprocessing import StandardScaler
import pandas as pd
import os
import zlib
from typing import Dict, List, Tuple, Optional, Literal
from langchain_mistralai import MistralAIEmbeddings
from langchain_core.embeddings import Embeddings
import os
from dotenv import load_dotenv
from ranking_agent import rank_with_ai
from scipy.sparse import load_npz
from rapidfuzz import process, fuzz
import re

load_dotenv()

class MovieRecommender:
    def __init__(self, data_dir: str = "amazon_movies_2023"):
        self.data_dir = data_dir
        self.embeddings = MistralAIEmbeddings(
            model="mistral-embed",
            mistral_api_key=os.getenv("MISTRAL_API_KEY")
        )
        # Load both types of embeddings
        self.load_embeddings()
        
    def load_embeddings(self) -> None:
        # Load LLM embeddings
        llm_embeddings_path = os.path.join(self.data_dir, "title_embeddings.npz")
        try:
            llm_data = np.load(llm_embeddings_path)
            self.llm_embeddings = llm_data['embeddings']
            self.llm_item_ids = llm_data['item_ids'].astype(str)  # Ensure string type
            print(f"Loaded LLM embeddings with shape: {self.llm_embeddings.shape}")
            print(f"Number of LLM item IDs: {len(self.llm_item_ids)}")
        except (IOError, zlib.error) as e:
            raise RuntimeError(
                f"Error loading LLM embeddings file: {str(e)}\n"
                "The embeddings file appears to be corrupted or invalid."
            )
            
        # Load GCL embeddings
        gcl_embeddings_path = os.path.join(self.data_dir, "gcl_embeddings.npz")
        try:
            gcl_data = np.load(gcl_embeddings_path)
            self.gcl_embeddings = gcl_data['embeddings']
            self.gcl_item_ids = gcl_data['item_ids'].astype(str)  # Ensure string type
            print(f"Loaded GCL embeddings with shape: {self.gcl_embeddings.shape}")
            print(f"Number of GCL item IDs: {len(self.gcl_item_ids)}")
        except (IOError, zlib.error) as e:
            raise RuntimeError(
                f"Error loading GCL embeddings file: {str(e)}\n"
                "Please run gcl_embeddings.py first to generate GCL embeddings."
            )
        
        # Load movie mapping
        mapping_path = os.path.join(self.data_dir, "title_embeddings_mapping.csv")
        self.movies_df = pd.read_csv(mapping_path)
        self.movies_df['item_id'] = self.movies_df['item_id'].astype(str)  # Ensure string type
        
        # Create standardized embeddings for both types
        scaler = StandardScaler()
        self.llm_embeddings = scaler.fit_transform(self.llm_embeddings)
        self.gcl_embeddings = scaler.fit_transform(self.gcl_embeddings)
        
        # Create item_id to index mappings for both types
        self.llm_id_to_idx = {str(item_id): idx for idx, item_id in enumerate(self.llm_item_ids)}
        self.gcl_id_to_idx = {str(item_id): idx for idx, item_id in enumerate(self.gcl_item_ids)}
        
        # Create title to id mapping for search
        self.title_to_id = dict(zip(self.movies_df['title'], self.movies_df['item_id']))
        
        # Store all titles for search
        self.all_titles = self.movies_df['title'].tolist()
        
        print(f"Number of movies in mapping: {len(self.movies_df)}")
        print(f"Number of titles with LLM embeddings: {len(set(self.llm_id_to_idx.keys()) & set(self.title_to_id.values()))}")
        print(f"Number of titles with GCL embeddings: {len(set(self.gcl_id_to_idx.keys()) & set(self.title_to_id.values()))}")
        
        # Pre-process titles for fuzzy matching
        self.clean_titles = {self.clean_title_for_comparison(title): title for title in self.title_to_id.keys()}
        
    def clean_title_for_comparison(self, title):
        """Clean title for comparison purposes"""
        # Remove special characters and extra spaces
        title = re.sub(r'[^\w\s]', '', str(title))
        # Convert to lowercase and strip
        return ' '.join(title.lower().split())

    def search_movies(self, query: str) -> List[str]:
        if not query:
            return []  # Return empty if no query to avoid overwhelming UI
        
        clean_query = self.clean_title_for_comparison(query)
        # Use rapidfuzz to find matches across entire dataset
        matches = process.extract(
            clean_query,
            self.clean_titles.keys(),
            scorer=fuzz.WRatio,  # WRatio works well for movie titles
            limit=None,  # No limit - show all matches
            score_cutoff=60  # Only return matches with score > 60
        )
        
        # Convert matches back to original titles
        return [self.clean_titles[match[0]] for match in matches]
        
    def get_text_embedding(self, text: str) -> np.ndarray:
        """Get embedding for text using LangChain Mistral embeddings"""
        try:
            embedding = self.embeddings.embed_query(text)
            # Convert embedding to numpy array
            embedding = np.array(embedding, dtype=np.float32)
            # Normalize the embedding
            if np.any(embedding):  # Only normalize if not all zeros
                embedding = embedding / np.linalg.norm(embedding)
            return embedding
        except Exception as e:
            print(f"Error getting embedding from Mistral API: {str(e)}")
            return None
        
    def get_recommendations(self, selected_movies: List[str], embedding_type: str = "LLM + GCL", user_preferences: str = "", alpha: float = 0.5) -> str:
        """
        Get recommendations using proper embedding aggregation:
        - e_h: embedding from user history (selected movies)
        - e_u: embedding from user preferences (text)
        - Combined: alpha * e_u + (1-alpha) * e_h
        """
        if not selected_movies and not user_preferences:
            return "Please select some movies or provide preferences."

        # Choose embeddings based on type
        if embedding_type == "LLM + GCL":
            embeddings = self.gcl_embeddings
            id_to_idx = self.gcl_id_to_idx
        else:
            embeddings = self.llm_embeddings
            id_to_idx = self.llm_id_to_idx

        user_profile = None
        
        # Get embedding from user history (e_h)
        e_h = None
        if selected_movies:
            movie_ids = [self.title_to_id[title] for title in selected_movies if title in self.title_to_id]
            if movie_ids:
                selected_embeddings = []
                for movie_id in movie_ids:
                    if movie_id in id_to_idx:
                        idx = id_to_idx[movie_id]
                        selected_embeddings.append(embeddings[idx])
                
                if selected_embeddings:
                    e_h = np.mean(selected_embeddings, axis=0)
        
        # Get embedding from user preferences (e_u)
        e_u = None
        if user_preferences.strip():
            e_u = self.get_text_embedding(user_preferences)
        
        # Apply aggregation algorithm
        if e_h is not None and e_u is not None:
            # Both available: alpha * e_u + (1-alpha) * e_h
            user_profile = alpha * e_u + (1 - alpha) * e_h
            print(f"Using combined embedding: α={alpha} (preferences weight)")
        elif e_u is not None:
            # Only preferences available
            user_profile = e_u
            print("Using preferences-only embedding")
        elif e_h is not None:
            # Only history available
            user_profile = e_h
            print("Using history-only embedding")
        else:
            return "Could not create user profile from provided input."
        
        # Calculate similarity with all movies
        # Normalize user profile and embeddings for proper cosine similarity
        user_profile_norm = user_profile / np.linalg.norm(user_profile)
        embeddings_norm = embeddings / np.linalg.norm(embeddings, axis=1, keepdims=True)
        
        # Calculate cosine similarity (normalized dot product)
        similarities = np.dot(embeddings_norm, user_profile_norm)
        
        print(f"Similarity range: {similarities.min():.3f} to {similarities.max():.3f}")
        
        # Get top 100 most similar movies
        top_indices = np.argsort(similarities)[-100:][::-1]
        
        # Filter out selected movies and create recommendations
        seen_titles = set(selected_movies) if selected_movies else set()
        seen_clean_titles = set(self.clean_title_for_comparison(title) for title in seen_titles)
        final_recommendations = []
        
        # Get reverse mapping for the chosen embedding type
        if embedding_type == "LLM + GCL":
            idx_to_id = {idx: item_id for item_id, idx in self.gcl_id_to_idx.items()}
        else:
            idx_to_id = {idx: item_id for item_id, idx in self.llm_id_to_idx.items()}
        
        for idx in top_indices:
            if idx not in idx_to_id:
                continue
                
            item_id = idx_to_id[idx]
            
            # Find the title for this item_id
            title = None
            for t, id_ in self.title_to_id.items():
                if id_ == item_id:
                    title = t
                    break
            
            if not title:
                continue
                
            clean_title = self.clean_title_for_comparison(title)
            
            # Skip if exact title is in seen titles
            if title in seen_titles:
                continue
                
            # Skip if clean version of title is in seen titles
            if clean_title in seen_clean_titles:
                continue
                
            # Skip collections/trilogies if user has seen any part
            is_collection = False
            for seen_title in seen_titles:
                seen_clean = self.clean_title_for_comparison(seen_title)
                if seen_clean in clean_title or clean_title in seen_clean:
                    if any(marker in title.lower() for marker in ['collection', 'trilogy', 'series', 'complete']):
                        is_collection = True
                        break
            if is_collection:
                continue
            
            # Check if this is a duplicate of already recommended movie
            is_duplicate = any(
                fuzz.ratio(clean_title, self.clean_title_for_comparison(rec[0])) > 90
                for rec in final_recommendations
            )
            if is_duplicate:
                continue
                
            # Add with similarity score
            final_recommendations.append((title, similarities[idx]))
            if len(final_recommendations) >= 100:
                break
        
        if not final_recommendations:
            return "No recommendations found based on your input."
        
        return final_recommendations[:100]  # Return top 100 for ranking agent

def create_interface():
    try:
        recommender = MovieRecommender()
    except Exception as e:
        print(f"Error initializing recommender: {str(e)}")
        return None
    
    with gr.Blocks() as iface:
        gr.Markdown(
        """
        # Movie Recommender
        Get personalized movie recommendations based on your taste and preferences utilizing zero-shot predicitons from Foundation Recommender!
        
        **How to use:**
        1. Search and select movies you've enjoyed
        2. Describe the type of film you are looking for. Consider factors such as genre, length, whether it is animated, etc.
        3. Adjust the preference weight (α) to balance between your description and movie history
        4. Get personalized recommendations
        """
        )
        
        selected_movies = gr.State([])
        retrieval_results = gr.State([])  # Store retrieval results for ranking
        
        with gr.Row():
            with gr.Column():
                # Movie search and selection
                movie_search_input = gr.Textbox(
                    label="Search movies",
                    placeholder="Type to search...",
                    interactive=True,
                    every=True
                )
                
                # Show search results as a list of clickable buttons
                search_results = gr.Radio(
                    choices=[],
                    label="Search Results",
                    interactive=True,
                    visible=True
                )
                
                # Display selected movies with functional red cross buttons
                with gr.Column(elem_id="selected_movies_container") as selected_movies_container:
                    selected_display = gr.HTML(
                        label="Your Selected Movies",
                        value="<p><i>No movies selected yet</i></p>"
                    )
                    
                    # Individual delete buttons (simpler approach)  
                    delete_buttons = []
                    for i in range(20):  # Support up to 20 movies
                        btn = gr.Button(f"× Remove Movie {i+1}", visible=False, size="sm", variant="secondary")
                        delete_buttons.append(btn)
                    
                    # Clear all button
                    clear_btn = gr.Button("Clear All", size="sm", variant="secondary")
                
                # User preferences text field
                user_preferences = gr.Textbox(
                    label="Describe what kind of movie you're looking for",
                    placeholder="E.g., 'A movie with a female main character'",
                    lines=3
                )
                
                # Alpha slider
                alpha = gr.Slider(
                    minimum=0,
                    maximum=1,
                    value=0.5,
                    step=0.1,
                    label="Preference Weight (α)",
                    info="0: Use only movie history, 1: Use only your description"
                )
                
                # Embedding type selection (defaulting to GCL)
                embedding_type = gr.Radio(
                    choices=["LLM + GCL", "LLM"],
                    value="LLM + GCL",
                    label="Embedding Type",
                    info="Choose between pure language model embeddings (LLM) or graph-enhanced embeddings (LLM + GCL)"
                )
                
                # Get recommendations button
                recommend_btn = gr.Button("Get Recommendations", variant="primary")
            
            with gr.Column():
                # Display recommendations with streaming
                recommendations = gr.Markdown(
                    label="Your Personalized Recommendations",
                    value="Recommendations will appear here"
                )
        
        def update_search_results(query):
            """Update search results based on input"""
            if not query or len(query.strip()) < 2:
                return gr.Radio(choices=[], visible=False)
            
            matches = recommender.search_movies(query)
            # Limit display to first 20 for UI performance
            display_matches = matches[:20] if len(matches) > 20 else matches
            
            if display_matches:
                return gr.Radio(choices=display_matches, visible=True)
            else:
                return gr.Radio(choices=[], visible=False)
        
        def format_selected_movies_display(movies):
            """Format selected movies with remove buttons on same line"""
            if not movies:
                return "<p><i>No movies selected yet</i></p>"
            
            html_items = []
            for i, movie in enumerate(movies):
                html_items.append(f"""
                    <div style="display: flex; align-items: center; justify-content: space-between; 
                                padding: 8px 12px; margin: 4px 0; background-color: #f8f9fa; 
                                border-radius: 6px; border-left: 3px solid #007bff;">
                        <span style="flex-grow: 1; font-size: 14px; margin-right: 10px;">{i+1}. {movie}</span>
                    </div>
                """)
            
            return f"<div>{''.join(html_items)}</div>"

        def update_delete_buttons_visibility(movies):
            """Update visibility and labels of delete buttons"""
            button_updates = []
            for i in range(20):  # Support up to 20 movies
                if i < len(movies):
                    movie_name = movies[i][:40] + ("..." if len(movies[i]) > 40 else "")
                    button_updates.append(gr.Button(f"🗑️ {movie_name}", visible=True, size="sm", variant="secondary"))
                else:
                    button_updates.append(gr.Button(f"× Remove Movie {i+1}", visible=False, size="sm", variant="secondary"))
            
            return button_updates

        def delete_movie_by_index(index, current_movies):
            """Delete movie at specific index"""
            if not current_movies or index >= len(current_movies):
                return current_movies, format_selected_movies_display(current_movies)
            
            current_movies.pop(index)
            return current_movies, format_selected_movies_display(current_movies)

        def handle_movie_selection(selected_movie, current_movies):
            """Handle movie selection from radio buttons"""
            if not selected_movie:
                return [current_movies, format_selected_movies_display(current_movies)] + update_delete_buttons_visibility(current_movies)
            
            # Check if it's a movie title (exists in our database)
            if selected_movie in recommender.title_to_id:
                # It's a movie selection - add it to the list
                current_movies = current_movies or []
                # Remove the 5-movie limit - users can now select as many as they want
                    
                if selected_movie not in current_movies:
                    current_movies.append(selected_movie)
                
                return [current_movies, format_selected_movies_display(current_movies)] + update_delete_buttons_visibility(current_movies)
            else:
                # Not a movie from database
                return [current_movies, format_selected_movies_display(current_movies)] + update_delete_buttons_visibility(current_movies)

        def clear_all_movies():
            """Clear all selected movies"""
            empty_movies = []
            return [empty_movies, "<p><i>No movies selected yet</i></p>"] + update_delete_buttons_visibility(empty_movies)

        def get_recommendations(movies, emb_type, preferences, pref_weight):
            """Get recommendations: retrieval phase only, then delegate to ranking_agent with streaming"""
            if not movies and not preferences:
                yield "Please select some movies or provide preferences"
                return
            
            try:
                # RETRIEVAL PHASE: Get top 100 candidates using proper embedding aggregation
                print(f"\n=== RETRIEVAL PHASE ===")
                print(f"Selected movies: {movies}")
                print(f"User preferences: '{preferences}'")
                print(f"Alpha weight: {pref_weight}")
                print(f"Embedding type: {emb_type}")
                
                yield "🔍 Searching for similar movies..."
                
                recommendations = recommender.get_recommendations(
                    selected_movies=movies, 
                    embedding_type=emb_type,
                    user_preferences=preferences,
                    alpha=pref_weight
                )
                
                # Handle error cases
                if isinstance(recommendations, str):
                    yield recommendations
                    return
                
                # Print retrieval results
                print(f"\nRETRIEVAL RESULTS: Found {len(recommendations)} candidates")
                print("Top 100 from retrieval phase:")
                for i, (title, score) in enumerate(recommendations[:100], 1):
                    print(f"  {i:2d}. {title} (score: {score:.3f})")
                
                # RERANKING + EXPLANATION PHASE: Delegate to ranking_agent with streaming
                print(f"\n=== RERANKING PHASE ===")
                print(f"Calling rank_with_ai with:")
                print(f"  - {len(recommendations)} recommendations")
                print(f"  - preferences: '{preferences}'")
                print(f"  - alpha: {pref_weight}")
                print(f"  - user_movies: {movies}")
                
                yield "🤖 AI is ranking and explaining your recommendations..."
                
                # Stream the responses from ranking agent
                for partial_result in rank_with_ai(
                    recommendations=recommendations, 
                    user_preferences=preferences, 
                    alpha=pref_weight,
                    user_movies=movies
                ):
                    yield partial_result
                    
            except Exception as e:
                print(f"ERROR in get_recommendations: {str(e)}")
                import traceback
                traceback.print_exc()
                yield f"Error getting recommendations: {str(e)}"

        # Event handlers
        movie_search_input.change(
            fn=update_search_results,
            inputs=movie_search_input,
            outputs=search_results
        )
        
        search_results.change(
            fn=handle_movie_selection,
            inputs=[search_results, selected_movies],
            outputs=[selected_movies, selected_display] + delete_buttons
        )
        
        # Add individual delete button handlers
        for i, btn in enumerate(delete_buttons):
            def make_delete_handler(btn_idx):
                def delete_handler(current_movies):
                    updated_movies, updated_display = delete_movie_by_index(btn_idx, current_movies)
                    return [updated_movies, updated_display] + update_delete_buttons_visibility(updated_movies)
                return delete_handler
            
            btn.click(
                fn=make_delete_handler(i),
                inputs=[selected_movies],
                outputs=[selected_movies, selected_display] + delete_buttons
            )
        
        clear_btn.click(
            fn=clear_all_movies,
            inputs=[],
            outputs=[selected_movies, selected_display] + delete_buttons
        )
        
        recommend_btn.click(
            fn=get_recommendations,
            inputs=[selected_movies, embedding_type, user_preferences, alpha],
            outputs=recommendations
        )
    
    return iface

if __name__ == "__main__":
    iface = create_interface()
    if iface is not None:
        iface.launch()
    else:
        print("\nPlease fix the issues above and try again.")