Spaces:
Sleeping
Sleeping
Update app.py
#3
by
Soumyadeep4790
- opened
app.py
CHANGED
@@ -9,8 +9,9 @@ citizen_data = pd.read_csv("citizen_data_50000.csv")
|
|
9 |
services_data = pd.read_csv("citizen_services_50000.csv")
|
10 |
merged_data = pd.merge(citizen_data, services_data, on="citizen_id")
|
11 |
|
12 |
-
# Encode categorical features
|
13 |
-
categorical_cols = ['gender', 'education', 'marital_status', 'social_category'
|
|
|
14 |
encoders = {}
|
15 |
for col in categorical_cols:
|
16 |
le = LabelEncoder()
|
@@ -21,17 +22,17 @@ for col in categorical_cols:
|
|
21 |
scaler = StandardScaler()
|
22 |
merged_data[['age', 'annual_income']] = scaler.fit_transform(merged_data[['age', 'annual_income']])
|
23 |
|
24 |
-
# Define feature columns
|
25 |
feature_cols = ['age', 'gender', 'education', 'marital_status', 'social_category',
|
26 |
-
'annual_income']
|
27 |
features_matrix = merged_data[feature_cols].values
|
28 |
|
29 |
# Train KNN model
|
30 |
knn = NearestNeighbors(n_neighbors=10, metric='cosine')
|
31 |
knn.fit(features_matrix)
|
32 |
|
33 |
-
# Recommendation function with
|
34 |
-
def recommend(age, gender, education, marital, social, income,
|
35 |
input_data = {
|
36 |
'age': [age],
|
37 |
'gender': [encoders['gender'].transform([gender])[0]],
|
@@ -39,11 +40,13 @@ def recommend(age, gender, education, marital, social, income, occupation):
|
|
39 |
'marital_status': [encoders['marital_status'].transform([marital])[0]],
|
40 |
'social_category': [encoders['social_category'].transform([social])[0]],
|
41 |
'annual_income': [income],
|
42 |
-
|
|
|
|
|
43 |
}
|
44 |
input_vector = pd.DataFrame(input_data)
|
45 |
|
46 |
-
# Normalize numeric
|
47 |
input_vector[['age', 'annual_income']] = scaler.transform(input_vector[['age', 'annual_income']])
|
48 |
|
49 |
# Get neighbors and calculate weighted similarity
|
@@ -60,7 +63,7 @@ def recommend(age, gender, education, marital, social, income, occupation):
|
|
60 |
recommended_services = top_users[service_cols].sum().sort_values(ascending=False).head(5)
|
61 |
return [f"{service} β
" for service in recommended_services.index]
|
62 |
|
63 |
-
# Gradio UI
|
64 |
iface = gr.Interface(
|
65 |
fn=recommend,
|
66 |
inputs=[
|
@@ -69,11 +72,14 @@ iface = gr.Interface(
|
|
69 |
gr.Dropdown(encoders['education'].classes_.tolist(), label="Education Level"),
|
70 |
gr.Dropdown(encoders['marital_status'].classes_.tolist(), label="Marital Status"),
|
71 |
gr.Dropdown(encoders['social_category'].classes_.tolist(), label="Social Category"),
|
72 |
-
gr.Slider(0, 2000000, value=300000, step=10000, label="Annual Income")
|
|
|
|
|
|
|
73 |
],
|
74 |
outputs=gr.List(label="Top 5 Recommended Services"),
|
75 |
-
title="π§ Citizen Service Recommender
|
76 |
-
description="Get recommended services based on citizens similar to you β
|
77 |
)
|
78 |
|
79 |
-
iface.launch()
|
|
|
9 |
services_data = pd.read_csv("citizen_services_50000.csv")
|
10 |
merged_data = pd.merge(citizen_data, services_data, on="citizen_id")
|
11 |
|
12 |
+
# Encode categorical features including location
|
13 |
+
categorical_cols = ['gender', 'education', 'marital_status', 'social_category',
|
14 |
+
'village', 'block', 'district']
|
15 |
encoders = {}
|
16 |
for col in categorical_cols:
|
17 |
le = LabelEncoder()
|
|
|
22 |
scaler = StandardScaler()
|
23 |
merged_data[['age', 'annual_income']] = scaler.fit_transform(merged_data[['age', 'annual_income']])
|
24 |
|
25 |
+
# Define feature columns including location
|
26 |
feature_cols = ['age', 'gender', 'education', 'marital_status', 'social_category',
|
27 |
+
'annual_income', 'village', 'block', 'district']
|
28 |
features_matrix = merged_data[feature_cols].values
|
29 |
|
30 |
# Train KNN model
|
31 |
knn = NearestNeighbors(n_neighbors=10, metric='cosine')
|
32 |
knn.fit(features_matrix)
|
33 |
|
34 |
+
# Recommendation function with location
|
35 |
+
def recommend(age, gender, education, marital, social, income, village, block, district):
|
36 |
input_data = {
|
37 |
'age': [age],
|
38 |
'gender': [encoders['gender'].transform([gender])[0]],
|
|
|
40 |
'marital_status': [encoders['marital_status'].transform([marital])[0]],
|
41 |
'social_category': [encoders['social_category'].transform([social])[0]],
|
42 |
'annual_income': [income],
|
43 |
+
'village': [encoders['village'].transform([village])[0]],
|
44 |
+
'block': [encoders['block'].transform([block])[0]],
|
45 |
+
'district': [encoders['district'].transform([district])[0]],
|
46 |
}
|
47 |
input_vector = pd.DataFrame(input_data)
|
48 |
|
49 |
+
# Normalize numeric columns
|
50 |
input_vector[['age', 'annual_income']] = scaler.transform(input_vector[['age', 'annual_income']])
|
51 |
|
52 |
# Get neighbors and calculate weighted similarity
|
|
|
63 |
recommended_services = top_users[service_cols].sum().sort_values(ascending=False).head(5)
|
64 |
return [f"{service} β
" for service in recommended_services.index]
|
65 |
|
66 |
+
# Gradio UI with location inputs
|
67 |
iface = gr.Interface(
|
68 |
fn=recommend,
|
69 |
inputs=[
|
|
|
72 |
gr.Dropdown(encoders['education'].classes_.tolist(), label="Education Level"),
|
73 |
gr.Dropdown(encoders['marital_status'].classes_.tolist(), label="Marital Status"),
|
74 |
gr.Dropdown(encoders['social_category'].classes_.tolist(), label="Social Category"),
|
75 |
+
gr.Slider(0, 2000000, value=300000, step=10000, label="Annual Income"),
|
76 |
+
gr.Dropdown(encoders['village'].classes_.tolist(), label="Village"),
|
77 |
+
gr.Dropdown(encoders['block'].classes_.tolist(), label="Block"),
|
78 |
+
gr.Dropdown(encoders['district'].classes_.tolist(), label="District"),
|
79 |
],
|
80 |
outputs=gr.List(label="Top 5 Recommended Services"),
|
81 |
+
title="π§ Citizen Service Recommender",
|
82 |
+
description="Get recommended services based on citizens similar to you β using your demographic and location data.",
|
83 |
)
|
84 |
|
85 |
+
iface.launch()
|