-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
executable file
·183 lines (152 loc) · 7.43 KB
/
demo.py
File metadata and controls
executable file
·183 lines (152 loc) · 7.43 KB
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
#!/usr/bin/env python3
"""
Demonstration script for the movie recommender system.
This script runs tests to verify the system is working correctly.
Can run with or without OpenAI API key.
"""
import sys
import os
from pathlib import Path
# Add src to Python path
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))
def demo_basic_functionality():
"""Demonstrate basic functionality without requiring API keys."""
print("🎬 Movie Recommender System Demo")
print("=" * 50)
try:
# Test imports
print("📦 Testing imports...")
from movie_recommender.config import get_config
from movie_recommender.data_processor import MovieDataProcessor
from movie_recommender.vector_store import MovieVectorStore
from movie_recommender.embeddings import get_embedding_provider
print("✅ All imports successful")
# Test configuration
print("\n⚙️ Testing configuration...")
config = get_config()
print(f"✅ Config loaded:")
print(f" - Embedding provider: {config.embedding_provider}")
print(f" - Embedding model: {config.embedding_model_name}")
print(f" - LLM model: {config.llm_model_name}")
# Test data processor
print("\n📊 Testing data processor...")
processor = MovieDataProcessor(chunk_size=100, chunk_overlap=10)
# Create sample data
import pandas as pd
sample_data = pd.DataFrame({
'Title': ['The Matrix', 'Inception', 'Gladiator'],
'Year': [1999, 2010, 2000],
'Genres': ['Action, Sci-Fi', 'Sci-Fi, Thriller', 'Action, Drama'],
'Director': ['The Wachowskis', 'Christopher Nolan', 'Ridley Scott'],
'Star Cast': ['Keanu Reeves, Laurence Fishburne', 'Leonardo DiCaprio, Marion Cotillard', 'Russell Crowe, Joaquin Phoenix'],
'IMDb Rating': [8.7, 8.8, 8.5],
'Duration (minutes)': [136, 148, 155],
'Certificates': ['R', 'PG-13', 'R'],
'MetaScore': [73.0, 74.0, 67.0]
})
descriptions = processor.create_movie_descriptions(sample_data)
print(f"✅ Generated {len(descriptions)} descriptions")
chunks = processor.chunk_descriptions(descriptions)
print(f"✅ Created {len(chunks)} text chunks")
# Test vector store initialization (without building index)
print("\n🧬 Testing vector store...")
try:
# Try with mock API key for basic initialization test
vector_store = MovieVectorStore(api_key="test-key")
stats = vector_store.get_stats()
print(f"✅ Vector store initialized - status: {stats['status']}")
except Exception as e:
if "api_key" in str(e).lower():
print("✅ Vector store class working (needs real OpenAI API key for full functionality)")
else:
raise e
print("\n🎉 Basic demo completed successfully!")
print("\nWhat's working:")
print(" ✅ Configuration management")
print(" ✅ Data processing and description generation")
print(" ✅ Text chunking")
print(" ✅ Vector store initialization")
print(" ✅ Embedding provider system")
return True
except Exception as e:
print(f"❌ Error during demo: {e}")
import traceback
traceback.print_exc()
return False
def demo_with_sample_data():
"""Demonstrate with minimal sample data and vector search."""
print("\n🔬 Advanced Demo with Vector Search")
print("=" * 50)
# Check for API key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("⚠️ OPENAI_API_KEY not set. This demo requires API access for embeddings.")
print(" Set your API key: export OPENAI_API_KEY='your-key-here'")
return False
try:
import pandas as pd
from movie_recommender.data_processor import MovieDataProcessor
from movie_recommender.vector_store import MovieVectorStore
# Create sample dataset
sample_movies = pd.DataFrame({
'Title': ['The Matrix', 'Inception', 'Gladiator', 'Pulp Fiction', 'The Godfather'],
'Year': [1999, 2010, 2000, 1994, 1972],
'Genres': ['Action, Sci-Fi', 'Sci-Fi, Thriller', 'Action, Drama', 'Crime, Drama', 'Crime, Drama'],
'Director': ['The Wachowskis', 'Christopher Nolan', 'Ridley Scott', 'Quentin Tarantino', 'Francis Ford Coppola'],
'Star Cast': ['Keanu Reeves, Laurence Fishburne', 'Leonardo DiCaprio, Marion Cotillard', 'Russell Crowe, Joaquin Phoenix', 'John Travolta, Samuel L. Jackson', 'Marlon Brando, Al Pacino'],
'IMDb Rating': [8.7, 8.8, 8.5, 8.9, 9.2],
'Duration (minutes)': [136, 148, 155, 154, 175],
'Certificates': ['R', 'PG-13', 'R', 'R', 'R'],
'MetaScore': [73.0, 74.0, 67.0, 95.0, 100.0]
})
print(f"📊 Working with {len(sample_movies)} sample movies")
# Process data
processor = MovieDataProcessor(chunk_size=150, chunk_overlap=20)
descriptions = processor.create_movie_descriptions(sample_movies)
chunks = processor.chunk_descriptions(descriptions)
print(f"✅ Created {len(chunks)} chunks from {len(descriptions)} descriptions")
# Build vector store (this will use OpenAI embeddings)
print("🧬 Building vector store with OpenAI embeddings...")
vector_store = MovieVectorStore(api_key=api_key)
vector_store.build_index(chunks)
stats = vector_store.get_stats()
print(f"✅ Vector store built: {stats['total_vectors']} vectors, {stats['unique_movies']} movies")
# Test search functionality
print("\n🔍 Testing search functionality...")
test_queries = [
"science fiction movie",
"action movie with gladiators",
"crime drama",
"movie with high rating"
]
for query in test_queries:
results = vector_store.search(query, k=2)
print(f"Query: '{query}' → Found: {[r['Title'] for r in results[:2]]}")
print("\n🎉 Advanced demo completed successfully!")
print("The system is working correctly with OpenAI embeddings.")
return True
except Exception as e:
print(f"❌ Error during advanced demo: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = demo_basic_functionality()
if success:
print("\n" + "="*50)
try:
choice = input("Run advanced demo with OpenAI embeddings? (y/N): ").strip().lower()
if choice in ['y', 'yes']:
demo_with_sample_data()
else:
print("\n💡 To run the full system:")
print(" 1. Set OPENAI_API_KEY environment variable")
print(" 2. Run: python main.py --mode build")
print(" 3. Run: python main.py --mode ui")
except (EOFError, KeyboardInterrupt):
print("\n💡 To run the full system:")
print(" 1. Set OPENAI_API_KEY environment variable")
print(" 2. Run: python main.py --mode build")
print(" 3. Run: python main.py --mode ui")
print("\n👋 Demo finished!")