File size: 7,125 Bytes
b7bf6bb
 
 
 
 
 
 
 
 
 
 
 
5fee098
b7bf6bb
5fee098
b7bf6bb
 
 
618d223
b7bf6bb
 
966a6a5
b7bf6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e5ad52
 
 
 
 
b7bf6bb
3e5ad52
 
b7bf6bb
3e5ad52
 
 
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
from fastapi import FastAPI, BackgroundTasks
from contextlib import asynccontextmanager
from pymongo import MongoClient
import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
import joblib
import asyncio
import logging
import os
from datetime import datetime

# Configure logging to write only to console
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger(__name__)


# MongoDB connection setup
db_name = 'property-listing'
collection_name = 'activities'
connection_string = os.getenv('CONNECTION_STRING')

client = MongoClient(connection_string)
db = client[db_name]
collection = db[collection_name]

# Load pre-trained SVD model and user-item matrix columns
svd = joblib.load('svd_model.joblib')
user_item_matrix_columns = joblib.load('all_columns.joblib')
item_factors = svd.components_.T

# Define the actions we're interested in
ALL_COLUMNS = ['nxt_img_listing', 'read_more_listing', 'nxt_img_detail', 'read_more_detail', 'time_spent']

# Global variables to store the latest session and recommendations
latest_session_id = None
latest_recommendations = None

async def check_for_new_session():
    global latest_session_id, latest_recommendations
    last_document_count = 0
    while True:
        try:
            # Find the most recent document in the collection
            latest_doc = collection.find_one(sort=[('timestamp', -1)])
            current_document_count = collection.count_documents({})
            
            if latest_doc:
                if latest_doc['sessionId'] != latest_session_id or current_document_count > last_document_count:
                    latest_session_id = latest_doc['sessionId']
                    logger.info(f"New activity detected for session: {latest_session_id}")
                    latest_recommendations = generate_recommendations_for_session(latest_session_id)
                    if latest_recommendations:
                        logger.info(f"Generated recommendations for session {latest_session_id}: {latest_recommendations}")
                    else:
                        logger.warning(f"No recommendations generated for session {latest_session_id}")
                    last_document_count = current_document_count
                else:
                    logger.info("No new activity detected")
            else:
                logger.warning("No documents found in the collection")
            
            await asyncio.sleep(5)  # Check every 5 seconds
        except Exception as e:
            logger.error(f"Error in check_for_new_session: {e}")
            await asyncio.sleep(5)  # Wait before retrying

def generate_recommendations_for_session(session_id):
    try:
        # Retrieve all documents for the given session
        session_data = list(collection.find({'sessionId': session_id}))
        if not session_data:
            logger.warning(f"No data found for session {session_id}")
            return None

        # Convert session data to a DataFrame
        raw_df = pd.DataFrame(session_data)
        
        # Aggregate data by id and action
        aggregated_data = raw_df.groupby(['id', 'action']).agg(
            presence=('action', 'size'),
            total_duration=('duration', 'sum')
        ).reset_index()
        
        # Create a pivot table from the aggregated data
        pivot_df = aggregated_data.pivot_table(
            index=['id'],
            columns='action',
            values=['presence', 'total_duration'],
            fill_value=0
        )
        
        # Flatten column names
        pivot_df.columns = ['_'.join(col).strip() for col in pivot_df.columns.values]
        
        # Ensure all expected columns exist in the pivot table
        for col in ALL_COLUMNS:
            if f'presence_{col}' not in pivot_df.columns and col != 'time_spent':
                pivot_df[f'presence_{col}'] = 0
            elif col == 'time_spent' and 'total_duration_time_spent' not in pivot_df.columns:
                pivot_df['total_duration_time_spent'] = 0
        
        # Calculate interaction score for each row
        pivot_df['interaction_score'] = pivot_df.apply(calculate_interaction_score, axis=1)
        
        # Create a user vector based on the interaction scores
        user_vector = pd.Series(index=user_item_matrix_columns, dtype=float).fillna(0)
        for property_id, score in pivot_df['interaction_score'].items():
            if property_id in user_vector.index:
                user_vector[property_id] = score
        
        # Transform the user vector using the SVD model
        user_vector_array = user_vector.values.reshape(1, -1)
        user_latent = svd.transform(user_vector_array)
        
        # Calculate similarity scores between the user vector and item factors
        similarity_scores = cosine_similarity(user_latent, item_factors)
        
        # Get the indices of the top 10 most similar items
        top_indices = similarity_scores.argsort()[0][-10:][::-1]
        
        # Get the corresponding property IDs for the top indices
        recommendations = user_item_matrix_columns[top_indices].tolist()
        
        return recommendations
    except Exception as e:
        logger.error(f"Error in generate_recommendations_for_session: {e}")
        return None

def calculate_interaction_score(row):
    try:
        # Calculate the score based on the presence of different actions
        score = (
            row.get('presence_nxt_img_listing', 0) * 1 +
            row.get('presence_read_more_listing', 0) * 2 +
            row.get('presence_nxt_img_detail', 0) * 3 +
            row.get('presence_read_more_detail', 0) * 4 +
            row.get('total_duration_time_spent', 0) / 10
        )
        
        # Apply bounce penalty if the session duration is less than 15 seconds
        if 'total_duration_time_spent' in row and row['total_duration_time_spent'] < 15:
            score -= 10
        
        return score
    except Exception as e:
        logger.error(f"Error in calculate_interaction_score: {e}")
        return 0

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup: create background task
    task = asyncio.create_task(check_for_new_session())
    yield
    # Shutdown: cancel background task
    task.cancel()
    try:
        await task
    except asyncio.CancelledError:
        logger.info("Background task cancelled")

# Create FastAPI application instance
app = FastAPI(lifespan=lifespan)

@app.get("/")
async def root():
    return {"message": "Welcome to the Rec API"}

@app.get("/recommendations")
async def get_recommendations():
    """
    API endpoint to get the latest recommendations.
    Returns:
    list: An array of recommended property IDs, or an empty array if no recommendations are available.
    """
    if latest_recommendations:
        logger.info(f"Returning recommendations: {latest_recommendations}")
        return latest_recommendations
    else:
        logger.info("No recommendations available")
        return []