File size: 9,032 Bytes
ed192dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import seaborn as sns
import gradio as gr
import sqlite3
from datetime import datetime, timedelta

def generate_sample_data():
    # Generate sample data
    np.random.seed(42)
    n_customers = 1000
    days_ago = [int(x) for x in np.random.randint(0, 365, n_customers)]
    
    crm_data = pd.DataFrame({
        'customer_id': range(1, n_customers + 1),
        'interactions': np.random.randint(1, 100, n_customers),
        'transactions': np.random.uniform(10, 1000, n_customers),
        'converted': np.random.choice([0, 1], n_customers, p=[0.7, 0.3]),
        'timestamp': [datetime.now() - timedelta(days=d) for d in days_ago]
    })
    
    social_days = [int(x) for x in np.random.randint(0, 365, n_customers)]
    social_data = pd.DataFrame({
        'customer_id': range(1, n_customers + 1),
        'interactions': np.random.randint(1, 200, n_customers),
        'open_rate': np.random.uniform(0.1, 0.9, n_customers),
        'timestamp': [datetime.now() - timedelta(days=d) for d in social_days]
    })
    
    # Enhanced financial data with more relevant metrics
    financial_days = [int(x) for x in np.random.randint(0, 365, n_customers)]
    financial_data = pd.DataFrame({
        'customer_id': range(1, n_customers + 1),
        'transaction_amount': np.random.uniform(50, 5000, n_customers),
        'transaction_frequency': np.random.randint(1, 20, n_customers),  # New column
        'average_purchase': np.random.uniform(100, 2000, n_customers),   # New column
        'total_spend': np.random.uniform(1000, 50000, n_customers),     # New column
        'transaction_date': [datetime.now() - timedelta(days=d) for d in financial_days]
    })
    
    return crm_data, social_data, financial_data

def init_database():
    conn = sqlite3.connect('sales_intelligence.db')
    cursor = conn.cursor()
    
    # Create tables if they don't exist
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS financial_data (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            customer_id INTEGER,
            transaction_amount FLOAT,
            transaction_frequency INTEGER,
            average_purchase FLOAT,
            total_spend FLOAT,
            transaction_date DATETIME
        )
    ''')
    
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS crm_data (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            customer_id INTEGER,
            interactions INTEGER,
            transactions FLOAT,
            converted INTEGER,
            timestamp DATETIME
        )
    ''')
    
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS social_media_data (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            customer_id INTEGER,
            interactions INTEGER,
            open_rate FLOAT,
            timestamp DATETIME
        )
    ''')
    
    # Generate and insert sample data
    crm_data, social_data, financial_data = generate_sample_data()
    
    try:
        crm_data.to_sql('crm_data', conn, if_exists='replace', index=False)
        social_data.to_sql('social_media_data', conn, if_exists='replace', index=False)
        financial_data.to_sql('financial_data', conn, if_exists='replace', index=False)
        
        print(f"Inserted {len(crm_data)} CRM records")
        print(f"Inserted {len(social_data)} social media records")
        print(f"Inserted {len(financial_data)} financial records")
        
    except sqlite3.Error as e:
        print(f"Error inserting data: {e}")
        
    conn.commit()
    conn.close()
    print("Database initialized with sample data!")

def segment_prospects(df, data_source):
    print("Segmenting prospects...")
    
    if data_source.lower() == 'financial_databases':
        # Special handling for financial data
        kmeans = KMeans(n_clusters=3)
        df['segment'] = kmeans.fit_predict(df[['transaction_amount', 'transaction_frequency', 'average_purchase']])
        segment_labels = ['Low Value', 'Medium Value', 'High Value']
        df['segment_label'] = [segment_labels[s] for s in df['segment']]
        
    elif 'interactions' in df.columns and 'transactions' in df.columns:
        kmeans = KMeans(n_clusters=3)
        df['segment'] = kmeans.fit_predict(df[['interactions', 'transactions']])
        
    print("Columns after segmentation:", df.columns)
    return df

def performance_analysis(df, data_source):
    print("Analyzing performance...")
    insights = {}
    
    if data_source.lower() == 'financial_databases':
        # Specific analysis for financial data
        if 'segment' in df.columns:
            # Overall metrics
            insights['overall_metrics'] = {
                'total_revenue': float(df['total_spend'].sum()),
                'average_transaction': float(df['transaction_amount'].mean()),
                'total_customers': len(df),
                'average_frequency': float(df['transaction_frequency'].mean())
            }
            
            # Segment-specific metrics
            segment_metrics = df.groupby('segment').agg({
                'transaction_amount': ['mean', 'max'],
                'transaction_frequency': 'mean',
                'total_spend': 'sum',
                'average_purchase': 'mean'
            }).round(2)
            
            # Convert the segment metrics to a more readable format
            for segment in df['segment'].unique():
                insights[f'segment_{segment}'] = {
                    'avg_transaction': float(segment_metrics.loc[segment, ('transaction_amount', 'mean')]),
                    'max_transaction': float(segment_metrics.loc[segment, ('transaction_amount', 'max')]),
                    'avg_frequency': float(segment_metrics.loc[segment, ('transaction_frequency', 'mean')]),
                    'total_revenue': float(segment_metrics.loc[segment, ('total_spend', 'sum')]),
                    'avg_purchase': float(segment_metrics.loc[segment, ('average_purchase', 'mean')])
                }
            
            return pd.DataFrame.from_dict(insights, orient='index')
    else:
        # Original analysis for other data sources
        if 'segment' in df.columns:
            insights = df.groupby('segment').mean()
            return insights
            
    return pd.DataFrame()

def load_data(data_source):
    conn = sqlite3.connect('sales_intelligence.db')
    if data_source.lower() == 'crm':
        return pd.read_sql('SELECT * FROM crm_data', conn)
    elif data_source.lower() == 'social_media':
        return pd.read_sql('SELECT * FROM social_media_data', conn)
    elif data_source.lower() == 'financial_databases':
        return pd.read_sql('SELECT * FROM financial_data', conn)
    else:
        return pd.DataFrame()

def preprocess_data(df):
    # Add any necessary preprocessing steps here
    return df

def predict_lead_conversion(df):
    # Example model for lead conversion prediction
    X = df[['interactions', 'transactions']]
    y = df['converted']
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    return model, accuracy

def sales_intelligence_platform(data_source):
    print("Processing data source:", data_source)
    data = load_data(data_source)
    
    if data.empty:
        return {"error": f"No data found for source: {data_source}. Valid sources are: 'CRM', 'social_media', 'financial_databases'"}
    
    data = preprocess_data(data)
    data = segment_prospects(data, data_source)
    model, accuracy = predict_lead_conversion(data) if data_source.lower() == 'crm' else (None, None)
    insights = performance_analysis(data, data_source)
    
    if insights.empty:
        return {"error": "Could not generate insights from the data"}
    
    result_dict = insights.to_dict()
    
    # Add some helpful messages
    if data_source.lower() == 'financial_databases':
        result_dict['analysis_description'] = {
            'segment_0': 'Low Value Customers',
            'segment_1': 'Medium Value Customers',
            'segment_2': 'High Value Customers'
        }
    
    return result_dict

# Initialize the database with sample data
init_database()

# Create Gradio interface
iface = gr.Interface(
    fn=sales_intelligence_platform,
    inputs=gr.Dropdown(
        choices=["CRM", "social_media", "financial_databases"],
        label="Select Data Source"
    ),
    outputs="json",
    title="Sales Intelligence Platform",
    description="A platform powered by AI to manage sales data and provide insights. Choose a data source to analyze.",
    theme="dark"
)

if __name__ == "__main__":
    iface.launch()