Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,102 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
3 |
import random
|
4 |
-
import plotly.graph_objects as go
|
5 |
-
import plotly.express as px
|
6 |
-
|
7 |
-
def generate_health_conditions():
|
8 |
-
return [
|
9 |
-
{"condition": "๐ Heart disease", "emoji": "๐", "spending": {"Minnesota": 100, "Florida": 150, "California": 200, "New York": 120, "Texas": 180}, "treatment": "Regular checkups with a cardiologist", "savings": "$1000"},
|
10 |
-
{"condition": "๐ค Trauma-related disorders", "emoji": "๐", "spending": {"Minnesota": 90, "Florida": 110, "California": 150, "New York": 100, "Texas": 130}, "treatment": "Counseling and physical therapy", "savings": "$500"},
|
11 |
-
{"condition": "๐ฆ Cancer", "emoji": "๐๏ธ", "spending": {"Minnesota": 80, "Florida": 120, "California": 180, "New York": 100, "Texas": 150}, "treatment": "Early detection and treatment", "savings": "$2000"},
|
12 |
-
{"condition": "๐ง Mental disorders", "emoji": "๐ง", "spending": {"Minnesota": 70, "Florida": 100, "California": 140, "New York": 90, "Texas": 120}, "treatment": "Therapy and medication", "savings": "$1500"},
|
13 |
-
{"condition": "๐ฆด Osteoarthritis and joint disorders", "emoji": "๐ฅ", "spending": {"Minnesota": 60, "Florida": 90, "California": 120, "New York": 80, "Texas": 100}, "treatment": "Low-impact exercise and physical therapy", "savings": "$800"},
|
14 |
-
{"condition": "๐ Diabetes", "emoji": "๐ฉธ", "spending": {"Minnesota": 50, "Florida": 80, "California": 100, "New York": 60, "Texas": 90}, "treatment": "Regular checkups and medication", "savings": "$1200"},
|
15 |
-
{"condition": "๐ซ Chronic obstructive pulmonary disease and asthma", "emoji": "๐ซ", "spending": {"Minnesota": 40, "Florida": 70, "California": 90, "New York": 50, "Texas": 80}, "treatment": "Inhalers and breathing exercises", "savings": "$600"},
|
16 |
-
{"condition": "๐ฉบ Hypertension", "emoji": "๐", "spending": {"Minnesota": 30, "Florida": 60, "California": 80, "New York": 40, "Texas": 70}, "treatment": "Lifestyle changes and medication", "savings": "$900"},
|
17 |
-
{"condition": "๐ฌ Hyperlipidemia", "emoji": "๐ฌ", "spending": {"Minnesota": 20, "Florida": 50, "California": 70, "New York": 30, "Texas": 60}, "treatment": "Lifestyle changes and medication", "savings": "$700"},
|
18 |
-
{"condition": "๐ฆด Back problems", "emoji": "๐ง", "spending": {"Minnesota": 10, "Florida": 40,
|
19 |
-
|
20 |
-
def calculate_total_spending(health_conditions):
|
21 |
-
total_spending = 0
|
22 |
-
for condition in health_conditions:
|
23 |
-
for state, spending in condition["spending"].items():
|
24 |
-
total_spending += spending
|
25 |
-
return round(total_spending, 1)
|
26 |
-
|
27 |
-
def generate_sunburst_chart(health_conditions, total_spending):
|
28 |
-
fig_sunburst = go.Figure(go.Treemap(
|
29 |
-
labels=[f"{condition['emoji']} {condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()],
|
30 |
-
parents=[f"{condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()],
|
31 |
-
values=[spending for condition in health_conditions for spending in condition['spending'].values()],
|
32 |
-
branchvalues="total",
|
33 |
-
))
|
34 |
-
|
35 |
-
fig_sunburst.update_layout(
|
36 |
-
title=f"Top Health Conditions in Different States by Spending (Total: ${total_spending}B)",
|
37 |
-
margin=dict(l=0, r=0, t=50, b=0),
|
38 |
-
)
|
39 |
-
return fig_sunburst
|
40 |
-
|
41 |
-
def roll(state, condition):
|
42 |
-
frequencies = [random.randint(1, 10) for _ in range(1000)]
|
43 |
-
spending = condition["spending"][state]
|
44 |
-
frequencies = [round(frequency * spending / 100, 1) for frequency in frequencies]
|
45 |
-
return frequencies
|
46 |
-
|
47 |
-
def generate_bar_chart(state, condition, frequencies):
|
48 |
-
fig_bar = px.bar(
|
49 |
-
x=[f"Variant {i}" for i in range(1, 11)],
|
50 |
-
y=frequencies[:10],
|
51 |
-
labels={'x': 'Variant', 'y': 'Cost'},
|
52 |
-
title=f"Variants of {condition['condition']} ({condition['emoji']}) in {state}",
|
53 |
-
)
|
54 |
-
return fig_bar
|
55 |
-
|
56 |
-
def generate_recommendation(condition):
|
57 |
-
return f"Based on the severity of your {condition['condition']}, we recommend {condition['treatment']} for early treatment. This could save you up to {condition['savings']} in healthcare costs."
|
58 |
-
|
59 |
-
def main():
|
60 |
-
states = ["Minnesota", "Florida", "California", "New York", "Texas"]
|
61 |
-
top_n = 10
|
62 |
-
|
63 |
-
health_conditions = generate_health_conditions()
|
64 |
-
|
65 |
-
total_spending = calculate_total_spending(health_conditions)
|
66 |
-
|
67 |
-
fig_sunburst = generate_sunburst_chart(health_conditions, total_spending)
|
68 |
-
|
69 |
-
st.plotly_chart(fig_sunburst)
|
70 |
-
|
71 |
-
condition_idx = st.selectbox("Select your current health condition", range(top_n))
|
72 |
-
|
73 |
-
condition = health_conditions[condition_idx]
|
74 |
-
|
75 |
-
st.write(generate_recommendation(condition))
|
76 |
-
|
77 |
-
state = st.selectbox("Select your state", states)
|
78 |
-
|
79 |
-
frequencies = roll(state, condition)
|
80 |
-
|
81 |
-
fig_bar = generate_bar_chart(state, condition, frequencies)
|
82 |
-
|
83 |
-
st.plotly_chart(fig_bar)
|
84 |
-
|
85 |
-
if __name__ == "__main__":
|
86 |
-
main()
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from graphviz import Digraph
|
3 |
+
import time
|
4 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Define the emoji to use for the swim lanes
|
7 |
+
SWIM_LANES = {
|
8 |
+
"Data Pipelines": "๐",
|
9 |
+
"Build and Train Models": "๐งช",
|
10 |
+
"Deploy and Predict": "๐"
|
11 |
+
}
|
12 |
+
|
13 |
+
# Define the graph structure
|
14 |
+
graph = Digraph()
|
15 |
+
graph.attr(rankdir="TB") # Top to Bottom or LR Left to Right
|
16 |
+
graph.attr(fontsize="20")
|
17 |
+
graph.attr(compound="true")
|
18 |
+
graph.attr(nodesep="0.5")
|
19 |
+
|
20 |
+
# Define the nodes
|
21 |
+
graph.node("๐ Data Collection")
|
22 |
+
graph.node("๐งน Data Cleaning")
|
23 |
+
graph.node("๐ง Data Transformation")
|
24 |
+
graph.node("๐ Feature Engineering")
|
25 |
+
graph.node("โ๏ธ Model Selection")
|
26 |
+
graph.node("๐ Model Training")
|
27 |
+
graph.node("๐ข Model Deployment")
|
28 |
+
graph.node("๐ก Model Serving")
|
29 |
+
graph.node("๐ฎ Predictions")
|
30 |
+
graph.node("๐ Feedback Collection")
|
31 |
+
graph.node("๐ค Feedback Processing")
|
32 |
+
graph.node("โ๏ธ Model Updating")
|
33 |
+
|
34 |
+
# Add the edges
|
35 |
+
graph.edge("๐ Data Collection", "๐งน Data Cleaning")
|
36 |
+
graph.edge("๐งน Data Cleaning", "๐ง Data Transformation")
|
37 |
+
graph.edge("๐ง Data Transformation", "๐ Feature Engineering")
|
38 |
+
graph.edge("๐ Feature Engineering", "โ๏ธ Model Selection")
|
39 |
+
graph.edge("โ๏ธ Model Selection", "๐ Model Training")
|
40 |
+
graph.edge("๐ Model Training", "๐ข Model Deployment")
|
41 |
+
graph.edge("๐ข Model Deployment", "๐ก Model Serving")
|
42 |
+
graph.edge("๐ก Model Serving", "๐ฎ Predictions")
|
43 |
+
graph.edge("๐ฎ Predictions", "๐ Feedback Collection")
|
44 |
+
graph.edge("๐ Feedback Collection", "๐ค Feedback Processing")
|
45 |
+
graph.edge("๐ค Feedback Processing", "โ๏ธ Model Updating")
|
46 |
+
graph.edge("โ๏ธ Model Updating", "๐ Model Training")
|
47 |
+
|
48 |
+
# Add the swim lanes
|
49 |
+
with graph.subgraph(name="cluster_0") as c:
|
50 |
+
c.attr(rank="1")
|
51 |
+
c.attr(label=SWIM_LANES["Data Pipelines"])
|
52 |
+
c.edge("๐ Data Collection", "๐งน Data Cleaning", style="invis")
|
53 |
+
c.edge("๐งน Data Cleaning", "๐ง Data Transformation", style="invis")
|
54 |
+
|
55 |
+
with graph.subgraph(name="cluster_1") as c:
|
56 |
+
c.attr(rank="2")
|
57 |
+
c.attr(label=SWIM_LANES["Build and Train Models"])
|
58 |
+
c.edge("๐ Feature Engineering", "โ๏ธ Model Selection", style="invis")
|
59 |
+
c.edge("โ๏ธ Model Selection", "๐ Model Training", style="invis")
|
60 |
+
|
61 |
+
with graph.subgraph(name="cluster_2") as c:
|
62 |
+
c.attr(rank="3")
|
63 |
+
c.attr(label=SWIM_LANES["Deploy and Predict"])
|
64 |
+
c.edge("๐ข Model Deployment", "๐ก Model Serving", style="invis")
|
65 |
+
c.edge("๐ก Model Serving", "๐ฎ Predictions", style="invis")
|
66 |
+
|
67 |
+
with graph.subgraph(name="cluster_3") as c:
|
68 |
+
c.attr(rank="4")
|
69 |
+
c.attr(label="Reinforcement Learning Human Feedback")
|
70 |
+
c.edge("๐ฎ Predictions", "๐ Feedback Collection", style="invis")
|
71 |
+
c.edge("๐ Feedback Collection", "๐ค Feedback Processing", style="invis")
|
72 |
+
c.edge("๐ค Feedback Processing", "โ๏ธ Model Updating", style="invis")
|
73 |
+
|
74 |
+
def render_graph():
|
75 |
+
st.graphviz_chart(graph.source)
|
76 |
+
|
77 |
+
def update_graph():
|
78 |
+
for i in range(60):
|
79 |
+
# Update the graph with new inputs randomly
|
80 |
+
graph.node("๐ Data Collection", label=f"๐ Data Collection\nData {random.randint(0,100)}")
|
81 |
+
graph.node("๐งน Data Cleaning", label=f"๐งน Data Cleaning\nCleaned Data {random.randint(0,100)}")
|
82 |
+
graph.node("๐ง Data Transformation", label=f"๐ง Data Transformation\nTransformed Data {random.randint(0,100)}")
|
83 |
+
graph.node("๐ Feature Engineering", label=f"๐ Feature Engineering\nFeatures {random.randint(0,100)}")
|
84 |
+
graph.node("โ๏ธ Model Selection", label=f"โ๏ธ Model Selection\nSelected Model {random.randint(0,100)}")
|
85 |
+
graph.node("๐ Model Training", label=f"๐ Model Training\nTrained Model {random.randint(0,100)}")
|
86 |
+
graph.node("๐ข Model Deployment", label=f"๐ข Model Deployment\nDeployed Model {random.randint(0,100)}")
|
87 |
+
graph.node("๐ก Model Serving", label=f"๐ก Model Serving\nServed Model {random.randint(0,100)}")
|
88 |
+
graph.node("๐ฎ Predictions", label=f"๐ฎ Predictions\nPredicted Results {random.randint(0,100)}")
|
89 |
+
graph.node("๐ Feedback Collection", label=f"๐ Feedback Collection\nFeedback {random.randint(0,100)}")
|
90 |
+
graph.node("๐ค Feedback Processing", label=f"๐ค Feedback Processing\nProcessed Feedback {random.randint(0,100)}")
|
91 |
+
graph.node("โ๏ธ Model Updating", label=f"โ๏ธ Model Updating\nUpdated Model {random.randint(0,100)}")
|
92 |
+
|
93 |
+
# Render the updated graph
|
94 |
+
render_graph()
|
95 |
+
|
96 |
+
# Wait for 1 second
|
97 |
+
time.sleep(1)
|
98 |
+
# Render the initial graph
|
99 |
+
render_graph()
|
100 |
+
|
101 |
+
# Update the graph every second for 60 seconds
|
102 |
+
update_graph()
|