Roommate / app.py
keerthuAi's picture
Update app.py
7644ad7 verified
import pandas as pd
import random
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeRegressor
import gradio as gr
# -----------------------------
# πŸ”§ Step 1: Setup options
# -----------------------------
sleeping_pattern = ['early_bird', 'night_owl']
food_preference = ['veg', 'non_veg', 'vegan']
cleanliness = ['low', 'medium', 'high']
smoking = ['yes', 'no']
pet_friendly = ['yes', 'no']
personality = ['introvert', 'extrovert']
work_mode = ['wfh', 'office']
music_preference = ['loud', 'soft', 'quiet']
alcohol = ['yes', 'no', 'occasionally']
guests = ['yes', 'no']
education = ['high_school', 'bachelor', 'master', 'phd']
states = ['Karnataka', 'Tamil Nadu', 'Kerala', 'Maharashtra', 'Delhi']
languages = ['English', 'Hindi', 'Kannada', 'Tamil', 'Telugu']
hobbies = ['reading', 'gaming', 'sports', 'music', 'cooking']
duration_options = [6, 12, 18, 24, 36]
# -----------------------------
# πŸ§ͺ Step 2: Create dummy dataset
# -----------------------------
data = []
for _ in range(300):
row = {
'sleeping_pattern': random.choice(sleeping_pattern),
'food_preference': random.choice(food_preference),
'cleanliness': random.choice(cleanliness),
'smoking': random.choice(smoking),
'pet_friendly': random.choice(pet_friendly),
'personality': random.choice(personality),
'work_mode': random.choice(work_mode),
'music_preference': random.choice(music_preference),
'alcohol': random.choice(alcohol),
'guests_frequently': random.choice(guests),
'education': random.choice(education),
'duration_of_stay': random.choice(duration_options),
'state': random.choice(states),
'language': random.choice(languages),
'hobby': random.choice(hobbies),
'compatibility_score': random.randint(50, 100)
}
data.append(row)
df = pd.DataFrame(data)
# -----------------------------
# 🧠 Step 3: Encode and train model
# -----------------------------
label_encoders = {}
for column in df.columns:
if df[column].dtype == "object":
le = LabelEncoder()
df[column] = le.fit_transform(df[column])
label_encoders[column] = le
X = df.drop("compatibility_score", axis=1)
y = df["compatibility_score"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = DecisionTreeRegressor(random_state=42)
model.fit(X_train, y_train)
# -----------------------------
# πŸ‘― Step 4: Generate roommate profiles
# -----------------------------
roommates = []
for _ in range(10):
roommates.append({
'sleeping_pattern': random.choice(sleeping_pattern),
'food_preference': random.choice(food_preference),
'cleanliness': random.choice(cleanliness),
'smoking': random.choice(smoking),
'pet_friendly': random.choice(pet_friendly),
'personality': random.choice(personality),
'work_mode': random.choice(work_mode),
'music_preference': random.choice(music_preference),
'alcohol': random.choice(alcohol),
'guests_frequently': random.choice(guests),
'education': random.choice(education),
'duration_of_stay': random.choice(duration_options),
'state': random.choice(states),
'language': random.choice(languages),
'hobby': random.choice(hobbies),
})
# -----------------------------
# πŸ” Step 5: Prediction function
# -----------------------------
def predict_compatibility(sleeping_pattern, food_preference, cleanliness, smoking, pet_friendly,
personality, work_mode, music_preference, alcohol, guests_frequently,
education, duration_of_stay, state, language, hobby):
inputs = [
label_encoders['sleeping_pattern'].transform([sleeping_pattern])[0],
label_encoders['food_preference'].transform([food_preference])[0],
label_encoders['cleanliness'].transform([cleanliness])[0],
label_encoders['smoking'].transform([smoking])[0],
label_encoders['pet_friendly'].transform([pet_friendly])[0],
label_encoders['personality'].transform([personality])[0],
label_encoders['work_mode'].transform([work_mode])[0],
label_encoders['music_preference'].transform([music_preference])[0],
label_encoders['alcohol'].transform([alcohol])[0],
label_encoders['guests_frequently'].transform([guests_frequently])[0],
label_encoders['education'].transform([education])[0],
int(duration_of_stay),
label_encoders['state'].transform([state])[0],
label_encoders['language'].transform([language])[0],
label_encoders['hobby'].transform([hobby])[0],
]
prediction = model.predict([inputs])[0]
# Find best roommate match
best_match = None
highest_score = -1
for roommate in roommates:
roommate_encoded = [
label_encoders['sleeping_pattern'].transform([roommate['sleeping_pattern']])[0],
label_encoders['food_preference'].transform([roommate['food_preference']])[0],
label_encoders['cleanliness'].transform([roommate['cleanliness']])[0],
label_encoders['smoking'].transform([roommate['smoking']])[0],
label_encoders['pet_friendly'].transform([roommate['pet_friendly']])[0],
label_encoders['personality'].transform([roommate['personality']])[0],
label_encoders['work_mode'].transform([roommate['work_mode']])[0],
label_encoders['music_preference'].transform([roommate['music_preference']])[0],
label_encoders['alcohol'].transform([roommate['alcohol']])[0],
label_encoders['guests_frequently'].transform([roommate['guests_frequently']])[0],
label_encoders['education'].transform([roommate['education']])[0],
roommate['duration_of_stay'],
label_encoders['state'].transform([roommate['state']])[0],
label_encoders['language'].transform([roommate['language']])[0],
label_encoders['hobby'].transform([roommate['hobby']])[0],
]
score = 100 - sum(abs(a - b) for a, b in zip(inputs, roommate_encoded))
if score > highest_score:
highest_score = score
best_match = roommate
return f"🎯 Predicted Score: {round(prediction)} / 100\n🏠 Best Roommate Match: {best_match['personality'].capitalize()} from {best_match['state']} with similar habits!"
# -----------------------------
# 🎨 Step 6: Build Gradio UI
# -----------------------------
with gr.Blocks(title="Roommate Compatibility Predictor", theme="soft") as interface:
gr.Markdown("## πŸ§‘β€πŸ€β€πŸ§‘ Roommate Compatibility Predictor")
gr.Markdown("Enter your preferences and background info to see how compatible you are with potential roommates! 🎯")
with gr.Row():
with gr.Column():
gr.Markdown("### 🎯 Personal Preferences")
sp = gr.Dropdown(sleeping_pattern, label="πŸ›Œ Sleeping Pattern")
fp = gr.Dropdown(food_preference, label="🍽️ Food Preference")
cl = gr.Dropdown(cleanliness, label="🧼 Cleanliness Level")
sm = gr.Dropdown(smoking, label="🚬 Smoking")
pf = gr.Dropdown(pet_friendly, label="🐢 Pet Friendly")
ps = gr.Dropdown(personality, label="🧠 Personality")
with gr.Column():
gr.Markdown("### 🍹 Lifestyle Choices")
wm = gr.Dropdown(work_mode, label="πŸ’» Work Mode")
mp = gr.Dropdown(music_preference, label="🎡 Music Preference")
al = gr.Dropdown(alcohol, label="🍷 Alcohol Consumption")
gf = gr.Dropdown(guests, label="🏠 Guests Frequently")
with gr.Row():
with gr.Column():
gr.Markdown("### 🌍 Background Information")
ed = gr.Dropdown(education, label="πŸŽ“ Education")
ds = gr.Number(label="πŸ•’ Duration of Stay (in months)")
st = gr.Dropdown(states, label="πŸ“ State")
lg = gr.Dropdown(languages, label="πŸ—£οΈ Language")
hb = gr.Dropdown(hobbies, label="🎯 Hobby/Interest")
submit_btn = gr.Button("πŸš€ Predict Compatibility")
output = gr.Textbox(label="Result", lines=3)
submit_btn.click(
fn=predict_compatibility,
inputs=[sp, fp, cl, sm, pf, ps, wm, mp, al, gf, ed, ds, st, lg, hb],
outputs=output
)
interface.launch()