Spaces:
Build error
Build error
import streamlit as st | |
import pandas as pd | |
import random | |
import plotly.graph_objects as go | |
import plotly.express as px | |
def generate_health_conditions(): | |
return [ | |
{"condition": "π Heart disease", "emoji": "π", "spending": {"Minnesota": 100, "Florida": 150, "California": 200, "New York": 120, "Texas": 180}, "treatment": "Regular checkups with a cardiologist", "savings": "$1000"}, | |
{"condition": "π€ Trauma-related disorders", "emoji": "π", "spending": {"Minnesota": 90, "Florida": 110, "California": 150, "New York": 100, "Texas": 130}, "treatment": "Counseling and physical therapy", "savings": "$500"}, | |
{"condition": "π¦ Cancer", "emoji": "ποΈ", "spending": {"Minnesota": 80, "Florida": 120, "California": 180, "New York": 100, "Texas": 150}, "treatment": "Early detection and treatment", "savings": "$2000"}, | |
{"condition": "π§ Mental disorders", "emoji": "π§", "spending": {"Minnesota": 70, "Florida": 100, "California": 140, "New York": 90, "Texas": 120}, "treatment": "Therapy and medication", "savings": "$1500"}, | |
{"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"}, | |
{"condition": "π Diabetes", "emoji": "π©Έ", "spending": {"Minnesota": 50, "Florida": 80, "California": 100, "New York": 60, "Texas": 90}, "treatment": "Regular checkups and medication", "savings": "$1200"}, | |
{"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"}, | |
{"condition": "π©Ί Hypertension", "emoji": "π", "spending": {"Minnesota": 30, "Florida": 60, "California": 80, "New York": 40, "Texas": 70}, "treatment": "Lifestyle changes and medication", "savings": "$900"}, | |
{"condition": "π¬ Hyperlipidemia", "emoji": "π¬", "spending": {"Minnesota": 20, "Florida": 50, "California": 70, "New York": 30, "Texas": 60}, "treatment": "Lifestyle changes and medication", "savings": "$700"}, | |
{"condition": "𦴠Back problems", "emoji": "π§", "spending": {"Minnesota": 10, "Florida": 40, | |
def calculate_total_spending(health_conditions): | |
total_spending = 0 | |
for condition in health_conditions: | |
for state, spending in condition["spending"].items(): | |
total_spending += spending | |
return round(total_spending, 1) | |
def generate_sunburst_chart(health_conditions, total_spending): | |
fig_sunburst = go.Figure(go.Treemap( | |
labels=[f"{condition['emoji']} {condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()], | |
parents=[f"{condition['condition']} ({state})" for condition in health_conditions for state in condition['spending'].keys()], | |
values=[spending for condition in health_conditions for spending in condition['spending'].values()], | |
branchvalues="total", | |
)) | |
fig_sunburst.update_layout( | |
title=f"Top Health Conditions in Different States by Spending (Total: ${total_spending}B)", | |
margin=dict(l=0, r=0, t=50, b=0), | |
) | |
return fig_sunburst | |
def roll(state, condition): | |
frequencies = [random.randint(1, 10) for _ in range(1000)] | |
spending = condition["spending"][state] | |
frequencies = [round(frequency * spending / 100, 1) for frequency in frequencies] | |
return frequencies | |
def generate_bar_chart(state, condition, frequencies): | |
fig_bar = px.bar( | |
x=[f"Variant {i}" for i in range(1, 11)], | |
y=frequencies[:10], | |
labels={'x': 'Variant', 'y': 'Cost'}, | |
title=f"Variants of {condition['condition']} ({condition['emoji']}) in {state}", | |
) | |
return fig_bar | |
def generate_recommendation(condition): | |
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." | |
def main(): | |
states = ["Minnesota", "Florida", "California", "New York", "Texas"] | |
top_n = 10 | |
health_conditions = generate_health_conditions() | |
total_spending = calculate_total_spending(health_conditions) | |
fig_sunburst = generate_sunburst_chart(health_conditions, total_spending) | |
st.plotly_chart(fig_sunburst) | |
condition_idx = st.selectbox("Select your current health condition", range(top_n)) | |
condition = health_conditions[condition_idx] | |
st.write(generate_recommendation(condition)) | |
state = st.selectbox("Select your state", states) | |
frequencies = roll(state, condition) | |
fig_bar = generate_bar_chart(state, condition, frequencies) | |
st.plotly_chart(fig_bar) | |
if __name__ == "__main__": | |
main() | |