Sahil2507 commited on
Commit
3ce909f
·
verified ·
1 Parent(s): 258ff2f

Upload mainflask copy.py

Browse files
Files changed (1) hide show
  1. mainflask copy.py +127 -0
mainflask copy.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
5
+ from tensorflow.keras.models import Sequential
6
+ from tensorflow.keras.layers import Dense, Dropout
7
+ import tensorflow as tf
8
+
9
+ # Data Initialization
10
+ location_df = pd.DataFrame({
11
+ 'location': ['Theni', 'Chennai', 'Thiruvallur', 'Coimbatore', 'Ariyalur'],
12
+ 'population_density': [21000, 11000, 8400, 7100, 6900],
13
+ 'gender_ratio': [0.92, 0.87, 0.95, 0.98, 0.89],
14
+ 'income_level': [45000, 52000, 48000, 38000, 35000],
15
+ 'farming_cycle': ['Kharif', 'Rabi', 'Mixed', 'Kharif', 'Mixed'],
16
+ 'seasonal_pattern': ['Monsoon', 'Winter', 'Year-round', 'Monsoon', 'Year-round']
17
+ })
18
+ scheme_df = pd.DataFrame({
19
+ 'scheme': ['Sukanya Samriddhi Yojana', 'Senior citizen saving schemes', 'Kisan Vikas Patra', 'Post office saving account', 'Monthly income scheme'],
20
+ 'ROI': [12.5, 8.2, 15.3, 10.1, 9.8],
21
+ 'target_gender': ['Both', 'Male', 'Both', 'Female', 'Both'],
22
+ 'target_age_group': ['Adult', 'Young', 'Adult', 'Senior', 'Young'],
23
+ 'tax_benefit': ['Yes', 'No', 'Yes', 'No', 'Yes'],
24
+ 'risk_level': ['Low', 'Medium', 'High', 'Low','Medium']
25
+ })
26
+ user_feedback_df = pd.DataFrame({
27
+ 'user_id': [1, 2, 3, 4, 5],
28
+ 'location': ['Theni', 'Chennai', 'Thiruvallur', 'Coimbatore', 'Ariyalur'],
29
+ 'scheme': ['Sukanya Samriddhi Yojana', 'Senior citizen saving schemes', 'Kisan Vikas Patra', 'Post office saving account', 'Monthly income scheme'],
30
+ 'rating': [4.5, 3.8, 4.2, 3.9, 4.0]
31
+ })
32
+
33
+ # Data Preprocessing
34
+ scaler = MinMaxScaler()
35
+ location_df[['population_density', 'income_level']] = scaler.fit_transform(
36
+ location_df[['population_density', 'income_level']]
37
+ )
38
+
39
+ encoder = OneHotEncoder(sparse_output=False)
40
+ location_encoder = OneHotEncoder(sparse_output=False)
41
+ location_cat_encoded = location_encoder.fit_transform(location_df[['farming_cycle', 'seasonal_pattern']])
42
+ location_cat_cols = location_encoder.get_feature_names_out(['farming_cycle', 'seasonal_pattern'])
43
+ location_encoded_df = pd.DataFrame(location_cat_encoded, columns=location_cat_cols)
44
+
45
+ scheme_encoder = OneHotEncoder(sparse_output=False)
46
+ scheme_cat_encoded = scheme_encoder.fit_transform(scheme_df[['target_gender', 'target_age_group', 'tax_benefit', 'risk_level']])
47
+ scheme_cat_cols = scheme_encoder.get_feature_names_out(['target_gender', 'target_age_group', 'tax_benefit', 'risk_level'])
48
+ scheme_encoded_df = pd.DataFrame(scheme_cat_encoded, columns=scheme_cat_cols)
49
+
50
+ location_features = pd.concat([
51
+ location_df[['population_density', 'gender_ratio', 'income_level']],
52
+ location_encoded_df
53
+ ], axis=1)
54
+
55
+ scheme_features = pd.concat([
56
+ scheme_df[['ROI']],
57
+ scheme_encoded_df
58
+ ], axis=1)
59
+
60
+ def create_autoencoder(input_dim):
61
+ model = Sequential([
62
+ Dense(32, activation='relu', input_shape=(input_dim,)),
63
+ Dropout(0.2),
64
+ Dense(16, activation='relu'),
65
+ Dropout(0.2),
66
+ Dense(8, activation='relu'),
67
+ Dense(16, activation='relu'),
68
+ Dense(32, activation='relu'),
69
+ Dense(input_dim, activation='sigmoid')
70
+ ])
71
+ model.compile(optimizer='adam', loss='mse')
72
+ return model
73
+
74
+ location_autoencoder = create_autoencoder(location_features.shape[1])
75
+ location_autoencoder.fit(location_features, location_features, epochs=10, batch_size=2, verbose=0)
76
+
77
+ scheme_autoencoder = create_autoencoder(scheme_features.shape[1])
78
+ scheme_autoencoder.fit(scheme_features, scheme_features, epochs=10, batch_size=2, verbose=0)
79
+
80
+ def create_recommendation_model(location_dim, scheme_dim):
81
+ location_input = tf.keras.Input(shape=(location_dim,))
82
+ scheme_input = tf.keras.Input(shape=(scheme_dim,))
83
+
84
+ x1 = Dense(16, activation='relu')(location_input)
85
+ x2 = Dense(16, activation='relu')(scheme_input)
86
+
87
+ concat = tf.keras.layers.Concatenate()([x1, x2])
88
+ x = Dense(32, activation='relu')(concat)
89
+ x = Dropout(0.2)(x)
90
+ x = Dense(16, activation='relu')(x)
91
+ x = Dense(8, activation='relu')(x)
92
+ output = Dense(1, activation='sigmoid')(x)
93
+
94
+ model = tf.keras.Model(inputs=[location_input, scheme_input], outputs=output)
95
+ model.compile(optimizer='adam', loss='mse')
96
+ return model
97
+
98
+ X_loc = location_features.values
99
+ X_scheme = scheme_features.values
100
+ y = user_feedback_df['rating'].values / 5.0
101
+
102
+ recommendation_model = create_recommendation_model(X_loc.shape[1], X_scheme.shape[1])
103
+ recommendation_model.fit([X_loc, X_scheme], y, epochs=10, batch_size=2, verbose=0)
104
+
105
+ # Streamlit UI
106
+ st.title('Scheme Recommendation System')
107
+
108
+ # User input
109
+ location = st.selectbox('Select Location', location_df['location'].values)
110
+
111
+ if st.button('Get Recommendations'):
112
+ if location not in location_df['location'].values:
113
+ st.error('Invalid location')
114
+ else:
115
+ loc_features = location_features.loc[location_df['location'] == location].values
116
+ loc_repeated = np.repeat(loc_features, len(scheme_features), axis=0)
117
+ preds = recommendation_model.predict([loc_repeated, scheme_features.values], verbose=0)
118
+
119
+ st.subheader(f'Recommendations for {location}')
120
+
121
+ # Create a DataFrame for displaying results
122
+ results = pd.DataFrame({
123
+ 'Scheme': scheme_df['scheme'],
124
+ 'Score': preds.flatten()
125
+ }).sort_values(by='Score', ascending=False)
126
+
127
+ st.dataframe(results)