Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,160 +1,30 @@
|
|
| 1 |
-
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import matplotlib as mpl
|
| 6 |
-
import pycaret
|
| 7 |
import streamlit as st
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
from PIL import Image
|
| 11 |
-
from PIL import ImageColor
|
| 12 |
-
from PIL import ImageDraw
|
| 13 |
-
from PIL import ImageFont
|
| 14 |
-
|
| 15 |
-
hide_streamlit_style = """
|
| 16 |
-
<style>
|
| 17 |
-
#MainMenu {visibility: hidden;}
|
| 18 |
-
footer {visibility: hidden;}
|
| 19 |
-
</style>
|
| 20 |
-
"""
|
| 21 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
| 22 |
-
|
| 23 |
-
with st.sidebar:
|
| 24 |
-
image = Image.open('./itaca_logo.png')
|
| 25 |
-
st.image(image,use_column_width=True)
|
| 26 |
-
page = option_menu(menu_title='Menu',
|
| 27 |
-
menu_icon="robot",
|
| 28 |
-
options=["Clustering Analysis",
|
| 29 |
-
"Anomaly Detection"],
|
| 30 |
-
icons=["chat-dots",
|
| 31 |
-
"key"],
|
| 32 |
-
default_index=0
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
st.title('ITACA Insurance Core AI Module')
|
| 36 |
-
|
| 37 |
-
if page == "Clustering Analysis":
|
| 38 |
-
st.header('Clustering Analysis')
|
| 39 |
-
|
| 40 |
-
st.write(
|
| 41 |
-
"""
|
| 42 |
-
"""
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# import pycaret unsupervised models
|
| 46 |
-
from pycaret.clustering import *
|
| 47 |
-
# import ClusteringExperiment
|
| 48 |
-
from pycaret.clustering import ClusteringExperiment
|
| 49 |
-
|
| 50 |
-
# Upload the CSV file
|
| 51 |
-
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 52 |
-
|
| 53 |
-
# Define the unsupervised model
|
| 54 |
-
clusteringmodel = ['kmeans', 'ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics', 'birch']
|
| 55 |
-
selected_model = st.selectbox("Choose a clustering model", clusteringmodel)
|
| 56 |
-
|
| 57 |
-
# Define the options for the dropdown list
|
| 58 |
-
numclusters = [2, 3, 4, 5, 6]
|
| 59 |
-
# selected_clusters = st.selectbox("Choose a number of clusters", numclusters)
|
| 60 |
-
selected_clusters = st.slider("Choose a number of clusters", min_value=2, max_value=10, value=4)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Read and display the CSV file
|
| 64 |
-
if uploaded_file is not None:
|
| 65 |
-
try:
|
| 66 |
-
delimiter = ','
|
| 67 |
-
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
|
| 68 |
-
except ValueError:
|
| 69 |
-
delimiter = '|'
|
| 70 |
-
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
|
| 71 |
-
|
| 72 |
-
s = setup(insurance_claims, session_id = 123, log_experiment='mlflow', experiment_name='fraud_detection')
|
| 73 |
-
|
| 74 |
-
exp_clustering = ClusteringExperiment()
|
| 75 |
-
|
| 76 |
-
# init setup on exp
|
| 77 |
-
exp_clustering.setup(insurance_claims, session_id = 123)
|
| 78 |
-
|
| 79 |
-
if st.button("Prediction"):
|
| 80 |
-
with st.spinner("Analyzing..."):
|
| 81 |
-
# train kmeans model
|
| 82 |
-
cluster_model = create_model(selected_model, num_clusters = selected_clusters)
|
| 83 |
-
|
| 84 |
-
cluster_model_2 = assign_model(cluster_model)
|
| 85 |
-
cluster_model_2
|
| 86 |
-
|
| 87 |
-
all_metrics = get_metrics()
|
| 88 |
-
all_metrics
|
| 89 |
-
|
| 90 |
-
cluster_results = pull()
|
| 91 |
-
cluster_results
|
| 92 |
-
|
| 93 |
-
# plot pca cluster plot
|
| 94 |
-
plot_model(cluster_model, plot = 'cluster', display_format = 'streamlit')
|
| 95 |
-
|
| 96 |
-
if selected_model != 'ap':
|
| 97 |
-
plot_model(cluster_model, plot = 'tsne', display_format = 'streamlit')
|
| 98 |
-
|
| 99 |
-
if selected_model not in ('ap', 'meanshift', 'dbscan', 'optics'):
|
| 100 |
-
plot_model(cluster_model, plot = 'elbow', display_format = 'streamlit')
|
| 101 |
-
|
| 102 |
-
if selected_model not in ('ap', 'meanshift', 'sc', 'hclust', 'dbscan', 'optics'):
|
| 103 |
-
plot_model(cluster_model, plot = 'silhouette', display_format = 'streamlit')
|
| 104 |
-
|
| 105 |
-
if selected_model not in ('ap', 'sc', 'hclust', 'dbscan', 'optics', 'birch'):
|
| 106 |
-
plot_model(cluster_model, plot = 'distance', display_format = 'streamlit')
|
| 107 |
-
|
| 108 |
-
if selected_model != 'ap':
|
| 109 |
-
plot_model(cluster_model, plot = 'distribution', display_format = 'streamlit')
|
| 110 |
-
|
| 111 |
-
elif page == "Anomaly Detection":
|
| 112 |
-
st.header('Anomaly Detection')
|
| 113 |
-
|
| 114 |
-
st.write(
|
| 115 |
-
"""
|
| 116 |
-
"""
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
# import pycaret anomaly
|
| 120 |
-
from pycaret.anomaly import *
|
| 121 |
-
# import AnomalyExperiment
|
| 122 |
-
from pycaret.anomaly import AnomalyExperiment
|
| 123 |
-
|
| 124 |
-
# Upload the CSV file
|
| 125 |
-
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 126 |
-
|
| 127 |
-
# Define the unsupervised model
|
| 128 |
-
anomalymodel = ['abod', 'cluster', 'cof', 'iforest', 'histogram', 'knn', 'lof', 'svm', 'pca', 'mcd', 'sod', 'sos']
|
| 129 |
-
selected_model = st.selectbox("Choose an anomaly model", anomalymodel)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
try:
|
| 134 |
-
delimiter = ','
|
| 135 |
-
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter)
|
| 136 |
-
except ValueError:
|
| 137 |
-
delimiter = '|'
|
| 138 |
-
insurance_claims = pd.read_csv (uploaded_file, sep=delimiter, encoding='latin-1')
|
| 139 |
-
|
| 140 |
-
s = setup(insurance_claims, session_id = 123)
|
| 141 |
|
| 142 |
-
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
# train model
|
| 150 |
-
anomaly_model = create_model(selected_model)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
plot_model(anomaly_model, plot = 'umap', display_format = 'streamlit')
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
import mlflow
|
| 4 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Set the MLflow tracking URI (e.g., local file system or a remote server)
|
| 7 |
+
mlflow.set_tracking_uri("http://localhost:5000")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
def main():
|
| 10 |
+
st.title("Streamlit and MLflow Integration")
|
| 11 |
|
| 12 |
+
# Retrieve the list of experiments
|
| 13 |
+
experiments = mlflow.list_experiments()
|
| 14 |
+
experiment_names = [exp.name for exp in experiments]
|
| 15 |
+
experiment_id_map = {exp.name: exp.experiment_id for exp in experiments}
|
| 16 |
|
| 17 |
+
# Create a dropdown menu to select an experiment
|
| 18 |
+
selected_experiment = st.selectbox("Select an experiment:", experiment_names)
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Get the runs for the selected experiment
|
| 21 |
+
if selected_experiment:
|
| 22 |
+
experiment_id = experiment_id_map[selected_experiment]
|
| 23 |
+
runs = mlflow.search_runs(experiment_ids=[experiment_id])
|
| 24 |
|
| 25 |
+
# Display the runs in a table
|
| 26 |
+
st.write("Runs for the selected experiment:")
|
| 27 |
+
st.write(runs)
|
| 28 |
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
main()
|
|
|