-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontend.py
More file actions
90 lines (72 loc) · 2.97 KB
/
Copy pathFrontend.py
File metadata and controls
90 lines (72 loc) · 2.97 KB
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
import streamlit as st
import requests
import pandas as pd
# Set page configuration
st.set_page_config(page_title="Insurance Claim Predictor", page_icon="🏥")
st.title("🏥 Insurance Claim Prediction")
st.markdown("Enter the details below to predict if an insurance claim will be requested.")
# API URL (Ensure your FastAPI server is running)
API_URL = "http://127.0.0.1:8000/predict"
# Create the form for user input
with st.form("prediction_form"):
st.subheader("Claim Details")
col1, col2 = st.columns(2)
with col1:
claim_reason = st.selectbox(
"Claim Reason",
options=["Travel", "Medical", "Phone", "Other"],
help="Select the primary reason for the claim"
)
claim_amount = st.number_input(
"Claim Amount",
min_value=1.0,
max_value=100000.0,
value=1200.0,
step=100.0
)
with col2:
confidentiality = st.selectbox(
"Data Confidentiality",
options=["Low", "High", "Medium", "Very low"],
help="Select the required data confidentiality level"
)
premium_ratio = st.slider(
"Premium/Amount Ratio",
min_value=0.001,
max_value=0.999,
value=0.050,
format="%.3f"
)
submit_button = st.form_submit_button("Predict Claim Status")
# Handle form submission
if submit_button:
payload = {
"claim_reason": claim_reason,
"data_confidentiality": confidentiality,
"claim_amount": claim_amount,
"premium_amount_ratio": premium_ratio
}
try:
with st.spinner("Analyzing data..."):
response = requests.post(API_URL, json=payload)
if response.status_code == 201:
result = response.json()
prediction = result["Prediction"]
confidence = result["Confidence"]
# Display results
st.divider()
if prediction == "Yes":
st.error(f"### Prediction: Claim Likely ({prediction})")
else:
st.success(f"### Prediction: No Claim Likely ({prediction})")
st.metric("Confidence Score", f"{confidence:.2%}")
with st.expander("View Raw API Response"):
st.json(result)
else:
st.error(f"Error: API returned status code {response.status_code}")
st.info(response.text)
except requests.exceptions.ConnectionError:
st.error("Could not connect to the FastAPI server. Please make sure `APIwithModel.py` is running at http://127.0.0.1:8000")
except Exception as e:
st.error(f"An unexpected error occurred: {e}")
st.sidebar.info("This app uses a Machine Learning model to predict insurance claim requests based on historical data.")