import streamlit as st
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
from transformers import pipeline

# Define the Hugging Face model pipeline
nlp = pipeline("sentiment-analysis")

# Define the hospital data as a Python list of dictionaries
hospital_data = [
    {"name": "Mayo Clinic", "beds": 1392, "latitude": 44.0205, "longitude": -92.4630},
    {"name": "University of Minnesota Medical Center", "beds": 908, "latitude": 44.9737, "longitude": -93.2278},
    {"name": "Abbott Northwestern Hospital", "beds": 631, "latitude": 44.9482, "longitude": -93.2616},
    {"name": "St. Cloud Hospital", "beds": 489, "latitude": 45.5563, "longitude": -94.1672},
    {"name": "Fairview Southdale Hospital", "beds": 342, "latitude": 44.8788, "longitude": -93.3521}
]

# Save the hospital data as a CSV file
hospital_df = pd.DataFrame(hospital_data)
hospital_df.to_csv("hospital_data.csv", index=False)

# Define the Streamlit app
def app():
    # Set the app title
    st.title("Minnesota Hospital Data")
    
    # Load the hospital data from the CSV file
    hospital_df = pd.read_csv("hospital_data.csv")
    
    # Display the hospital data as a table
    st.write("Hospital Data:", hospital_df)
    
    # Analyze the sentiment of the hospital names using the Hugging Face model
    sentiment_scores = [nlp(hospital["name"])[0]["score"] for hospital in hospital_data]
    sentiment_colors = ["red" if score < 0.5 else "green" for score in sentiment_scores]
    hospital_df["sentiment_score"] = sentiment_scores
    
    # Create a treemap chart of the hospital data
    treemap_fig = px.treemap(hospital_df, path=["name"], values="beds",
                             color="sentiment_score", color_continuous_scale=["red", "green"],
                             hover_data=["latitude", "longitude"])
    treemap_fig.update_traces(hovertemplate="<b>%{label}</b><br>Beds: %{value}<br>Latitude: %{customdata[0]}<br>Longitude: %{customdata[1]}")
    treemap_fig.update_layout(margin=dict(t=25, b=25, r=25, l=25))
    st.plotly_chart(treemap_fig)
    
    # Display the top five largest hospitals in Minnesota
    st.subheader("Top 5 Largest Hospitals in Minnesota")
    largest_hospitals = hospital_df.nlargest(5, "beds")
    st.write(largest_hospitals)
    
if __name__ == "__main__":
    app()