Spaces:
Runtime error
Runtime error
File size: 3,991 Bytes
3fc26b3 f65ace6 3fc26b3 ce1a83b 3fc26b3 ce1a83b 3fc26b3 ce1a83b 3fc26b3 ce1a83b 3fc26b3 ce1a83b 3fc26b3 | 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 | import streamlit as st
import torch
import torch.nn as nn
import numpy as np
import pickle
import json
from PIL import Image
st.set_page_config(layout="wide")
st.markdown("""
<style>
body, html, .css-18e3th9 {
font-size: 20px !important;
}
h1 {
font-size: 3rem !important;
}
h2 {
font-size: 2.5rem !important;
}
label, .stSlider label, .stSelectbox label, .stButton button, .stTextInput label {
font-size: 22px !important;
}
div[data-baseweb="select"] > div {
font-size: 20px !important;
}
.stNumberInput input {
font-size: 20px !important;
}
div[data-testid="stImage"] > figcaption {
font-size: 1.2rem !important;
}
</style>
""", unsafe_allow_html=True)
with open('embeddings.pkl', 'rb') as f:
embeddings = pickle.load(f)
with open('label_mappings.json', 'r') as f:
label_mappings = json.load(f)
class MLPRegressor(nn.Module):
def __init__(self, input_dim, hidden_dims=[512, 128], dropout=0.2):
super().__init__()
layers = []
prev = input_dim
for h in hidden_dims:
layers += [
nn.Linear(prev, h),
nn.ReLU(),
nn.Dropout(dropout)
]
prev = h
layers.append(nn.Linear(prev, 1))
layers.append(nn.Tanh())
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
num_text_features = 3 * 384
num_numeric_features = 1
num_categorical_features = 4
input_dim = num_text_features + num_numeric_features + num_categorical_features
model = MLPRegressor(input_dim=input_dim)
model.load_state_dict(torch.load('mlp_model.pth', map_location='cpu'))
model.eval()
st.title("Investor Risk Score Estimator")
st.markdown("---")
st.subheader("Select 3 images in order of your preference:")
image_ids = list(range(1, 17))
cols = st.columns(4)
selection = {}
for i, img_id in enumerate(image_ids):
col = cols[i % 4]
image_path = f"../images/{img_id}.png"
image = Image.open(image_path)
col.image(image, caption=f"ID {img_id}", use_container_width=True)
selection[img_id] = col.checkbox(f"Select ID {img_id}")
selected = [img_id for img_id, chosen in selection.items() if chosen]
if len(selected) < 3:
st.info(f"Please select {3 - len(selected)} more image(s).")
st.stop()
elif len(selected) > 3:
st.error("You have selected more than 3 images. Please select exactly 3.")
st.stop()
else:
st.success("✅ 3 images selected. You can proceed!")
st.subheader("Set preference order for selected images:")
pref_1 = st.selectbox("Preference 1", selected, key="p1")
remaining_2 = [x for x in selected if x != pref_1]
pref_2 = st.selectbox("Preference 2", remaining_2, key="p2")
pref_3 = [x for x in remaining_2 if x != pref_2][0]
st.write(f"Preference 3: {pref_3}")
st.header("Sociodemographic Information:")
age = st.slider("Age", 18, 100, 30)
gender = st.selectbox("Gender", list(label_mappings['gender'].keys()))
education = st.selectbox("Education", list(label_mappings['education'].keys()))
income = st.selectbox("Income", list(label_mappings['income'].keys()))
marital_status = st.selectbox("Marital Status", list(label_mappings['marital_status'].keys()))
emb1 = embeddings[pref_1]
emb2 = embeddings[pref_2]
emb3 = embeddings[pref_3]
emb_features = np.hstack([emb1, emb2, emb3])
numeric_features = np.array([age])
categorical_features = np.array([
label_mappings['gender'][gender],
label_mappings['education'][education],
label_mappings['income'][income],
label_mappings['marital_status'][marital_status]
])
final_input = np.hstack([emb_features, numeric_features, categorical_features]).astype(np.float32)
final_tensor = torch.tensor(final_input).unsqueeze(0)
if st.button("Calculate Risk Score"):
with torch.no_grad():
prediction = model(final_tensor).item()
st.success(f"Predicted Risk Score: {prediction:.3f}")
|