Chri12345 commited on
Commit
c4bf16e
Β·
verified Β·
1 Parent(s): 1e84bae

Upload folder using huggingface_hub

Browse files
Files changed (7) hide show
  1. .DS_Store +0 -0
  2. README.md +12 -8
  3. app.py +89 -0
  4. best_reg.joblib +3 -0
  5. ohe.joblib +3 -0
  6. requirements.txt +9 -0
  7. scaler.joblib +3 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
README.md CHANGED
@@ -1,13 +1,17 @@
1
  ---
2
- title: Assignment4
3
- emoji: πŸ“ˆ
4
- colorFrom: yellow
5
- colorTo: yellow
6
  sdk: streamlit
7
- sdk_version: 1.38.0
 
 
 
8
  app_file: app.py
9
- pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: My Streamlit Project
3
+ emoji: πŸ”₯
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: streamlit
7
+ python_version: '3.10'
8
+ tags:
9
+ - streamlit
10
+ - data-visualization
11
  app_file: app.py
12
+ sdk_version: 1.38.0
 
13
  ---
14
 
15
+ ## πŸ“œ **License**
16
+
17
+ Licensed under the MIT License. See the LICENSE file for more details. If you don't like the license, well... good luck changing it! πŸ˜„
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import joblib
5
+ from sklearn.ensemble import RandomForestClassifier
6
+ from sklearn.preprocessing import StandardScaler,MinMaxScaler, OneHotEncoder
7
+ import shap
8
+ from streamlit_shap import st_shap
9
+
10
+ # Page configuration
11
+ st.set_page_config(
12
+ page_title="Kiva loan amount predictor",
13
+ page_icon="πŸ’°")
14
+
15
+ st.title('Predict Kiva loan amounts')
16
+
17
+
18
+ # Load model and preprocessing objects
19
+ @st.cache_resource
20
+ def load_model_objects():
21
+ model_rf = joblib.load('best_reg.joblib')
22
+ scaler = joblib.load('scaler.joblib')
23
+ ohe = joblib.load('ohe.joblib')
24
+ return model_rf, scaler, ohe
25
+
26
+ model_rf, scaler, ohe = load_model_objects()
27
+
28
+ # Create SHAP explainer
29
+ explainer = shap.TreeExplainer(model_rf)
30
+
31
+ # App description
32
+ with st.expander("What's this app?"):
33
+ st.markdown("""
34
+ This app helps you determine how much you will be succesfully funded with on Kiva
35
+ """)
36
+
37
+ st.subheader('Describe what you want to loan to')
38
+
39
+ # User inputs
40
+ col1, col2 = st.columns(2)
41
+
42
+ with col1:
43
+ Sector = st.selectbox('sector', options=ohe.categories_[0])
44
+ Country = st.selectbox('country', options=ohe.categories_[1])
45
+ Gender = st.selectbox('borrower_genders', options=ohe.categories_[2])
46
+
47
+ with col2:
48
+ term_in_months = st.number_input('Lenght of loan in months', min_value=0, value=1)
49
+ lender_count = st.number_input('Number of Lenders', min_value=1,value=1)
50
+
51
+ # Prediction button
52
+ if st.button('Predict Price πŸš€'):
53
+ # Prepare categorical features
54
+ cat_features = pd.DataFrame({'sector': [Sector], 'country': [Country],'borrower_genders': [Gender]})
55
+ cat_encoded = pd.DataFrame(ohe.transform(cat_features).todense(),
56
+ columns=ohe.get_feature_names_out(['sector', 'country', 'borrower_genders']))
57
+
58
+ # Prepare numerical features
59
+ num_features = pd.DataFrame({
60
+ 'term_in_months': [term_in_months],
61
+ 'lender_count': [lender_count],
62
+ })
63
+ num_scaled = pd.DataFrame(scaler.transform(num_features), columns=num_features.columns)
64
+
65
+ # Combine features
66
+ features = pd.concat([num_scaled, cat_encoded], axis=1)
67
+
68
+ # Make prediction
69
+ predicted_price = model_rf.predict(features)[0]
70
+
71
+ # Display prediction
72
+ st.metric(label="Predicted loan amount", value=f'{round(predicted_price)} USD')
73
+
74
+
75
+ # SHAP explanation
76
+ st.subheader('Price Factors Explained πŸ€–')
77
+ shap_values = explainer.shap_values(features)
78
+ st_shap(shap.force_plot(explainer.expected_value, shap_values, features), height=400, width=600)
79
+
80
+ st.markdown("""
81
+ This plot shows how each feature contributes to the predicted price:
82
+ - Blue bars push the price lower
83
+ - Red bars push the price higher
84
+ - The length of each bar indicates the strength of the feature's impact
85
+ """)
86
+
87
+ # Footer
88
+ st.markdown("---")
89
+ st.markdown("Developed with ❀️ using Streamlit")
best_reg.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:062fd7833eba30d26f48650cf05ba1afcc94dbfca5ccc64f85cc42fd373e1a82
3
+ size 8027665
ohe.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9d7ffa3ce8ec22fe74f0fe2efe9151cfbeec443c48f6ae490a8fbe6cbcac8d15
3
+ size 3160
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ requests==2.32.2
2
+ pandas==2.2.2
3
+ numpy==1.26.4
4
+ matplotlib==3.8.4
5
+ streamlit==1.32.0
6
+ shap==0.46.0
7
+ joblib==1.4.2
8
+ streamlit_shap==1.0.2
9
+ scikit-learn==1.5.2
scaler.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f086078aa92ac1945e9551628176c435472ca10512d25e073349b5398d87817c
3
+ size 1079