awacke1 commited on
Commit
1ba601f
Β·
1 Parent(s): 6907b9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -28
app.py CHANGED
@@ -1,36 +1,78 @@
1
  import streamlit as st
 
2
  import geopandas as gpd
3
- import plotly.express as px
4
 
5
- @st.cache
6
- def load_usa_map():
7
- return gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def generate_speech_textarea(text):
10
- st.markdown(f'<p id="speech" hidden>{text}</p><button onclick="speechSynthesis.speak(new SpeechSynthesisUtterance(document.getElementById(\'speech\').textContent))">πŸ”Š Speak</button>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
11
 
12
- state_data = {
13
- 'US-MN': {'name': 'Minnesota', 'trivia': '1️⃣ Home to over 10,000 lakes 🌊\n2️⃣ Boundary Waters Canoe Area πŸ›Ά', 'largest_company': 'UnitedHealth Group', 'revenue': 257.1},
14
- 'US-CA': {'name': 'California', 'trivia': '1️⃣ Known for Hollywood 🎬\n2️⃣ Golden Gate Bridge πŸŒ‰', 'largest_company': 'Apple', 'revenue': 365.8},
15
- # ... (your other states)
16
- }
17
 
18
- usa = load_usa_map()
19
- usa = usa[usa['iso_a2'].str.startswith('US', na=False)] # Filter only US states
20
 
21
- selected_state = st.selectbox("πŸ“ Choose a state:", list(state_data.keys()), format_func=lambda x: f"{state_data[x]['name']} ({x})")
 
22
 
23
- state_geom_df = usa[usa['iso_a2'] == selected_state]
24
- if not state_geom_df.empty:
25
- geom = state_geom_df.geometry.iloc[0]
26
- fig = px.choropleth(usa, geojson=usa.geometry, locations=usa.index, scope="usa")
27
- fig.add_trace(px.scatter_geo(lat=[geom.centroid.y], lon=[geom.centroid.x]).data[0])
28
- st.plotly_chart(fig)
29
-
30
- data = state_data[selected_state]
31
- text = f"πŸ” Trivia:\n{data['trivia']}\n🏒 Largest Company: {data['largest_company']}\nπŸ’΅ Revenue: {data['revenue']}B"
32
- with st.expander(f"{state_data[selected_state]['name']} ({selected_state}) πŸ˜ƒ"):
33
- generate_speech_textarea(text)
34
- st.markdown(text)
35
- else:
36
- st.error(f"🚫 No data for {selected_state}.")
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import streamlit.components.v1 as components
3
  import geopandas as gpd
4
+ import matplotlib.pyplot as plt
5
 
6
+ # Function to generate HTML with textarea for speech synthesis
7
+ def generate_speech_textarea(text_to_speak):
8
+ documentHTML5 = '''
9
+ <!DOCTYPE html>
10
+ <html>
11
+ <head>
12
+ <title>Read It Aloud</title>
13
+ <script type="text/javascript">
14
+ function readAloud() {{
15
+ const text = document.getElementById("textArea").value;
16
+ const speech = new SpeechSynthesisUtterance(text);
17
+ window.speechSynthesis.speak(speech);
18
+ }}
19
+ </script>
20
+ </head>
21
+ <body>
22
+ <h1>πŸ”Š Read It Aloud</h1>
23
+ <textarea id="textArea" rows="10" cols="80" readonly>'''
24
+ documentHTML5 = documentHTML5 + text_to_speak
25
+ documentHTML5 = documentHTML5 + '''
26
+ </textarea>
27
+ <br>
28
+ <button onclick="readAloud()">πŸ”Š Read Aloud</button>
29
+ </body>
30
+ </html>
31
+ '''
32
+ components.html(documentHTML5, width=1280, height=500)
33
 
34
+ # Function to display the state outline
35
+ def plot_state_outline(state_code):
36
+ # Read U.S. geometries file
37
+ gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
38
+ # Filter data for the given state
39
+ gdf_state = gdf[gdf['iso_a3'] == 'USA']
40
+ # Plot the geometry
41
+ ax = gdf_state.boundary.plot()
42
+ plt.title(f"{state_code} State Outline")
43
+ st.pyplot(plt)
44
 
45
+ # States list and associated icons
46
+ states = ['MN', 'CA', 'WA', 'FL', 'TX', 'NY', 'NV']
47
+ icons = ['πŸ¦†', '🌴', '🍎', '🌞', '🀠', 'πŸ—½', '🎰']
 
 
48
 
49
+ # Main code
50
+ st.title('U.S. States Trivia πŸ—ΊοΈ')
51
 
52
+ for i, (state, icon) in enumerate(zip(states, icons)):
53
+ st.markdown(f"{i + 1}. {state} {icon}")
54
 
55
+ # Expanders for each state to outline fascinating facts
56
+ with st.expander(f"See Fascinating Facts about {state}"):
57
+ text_to_speak = ""
58
+
59
+ if state == 'MN':
60
+ text_to_speak = "πŸ¦† **Minnesota** \n🏞️ Known as the 'Land of 10,000 Lakes' \n🎣 Famous for its fishing \nπŸ›Ά Boundary Waters offers incredible canoeing \nπŸŽ“ Home to prestigious colleges \n❄️ Cold winters but lovely summers."
61
+ elif state == 'CA':
62
+ text_to_speak = "🌴 **California** \nπŸŒ‰ Home to the Golden Gate Bridge \n🎬 Center of the American entertainment industry \nπŸ‡ Famous for Napa Valley's wine \n🌲 Home to Redwood National Park \nπŸ„β€β™€οΈ Excellent beaches and surf spots."
63
+ elif state == 'WA':
64
+ text_to_speak = "🍎 **Washington** \nβ˜• Known for its coffee culture \nπŸ—» Home to Mount Rainier \n🍏 Leading apple-producing state \n🐟 Rich in seafood, especially salmon \n🌧️ Known for its rainy weather."
65
+ elif state == 'FL':
66
+ text_to_speak = "🌞 **Florida** \n🏝️ Famous for its beaches \n🎒 Home to various amusement parks like Disney World \nπŸš€ Space launches from Cape Canaveral \n🐊 Known for the Everglades and alligators \n🍊 Major orange producer."
67
+ elif state == 'TX':
68
+ text_to_speak = "🀠 **Texas** \nπŸ›’οΈ Known for its oil and gas industry \nπŸ– Famous for its barbecue \n🎸 Rich musical heritage \nπŸ„ Home to many cattle ranches \n🌡 Includes part of the Chihuahuan Desert."
69
+ elif state == 'NY':
70
+ text_to_speak = "πŸ—½ **New York** \nπŸ™οΈ Home to New York City, the largest city in the U.S. \n🍎 Known as the Big Apple \n🎭 Major hub for arts and culture \n🏞️ Adirondack Mountains offer outdoor adventures \nπŸ• Famous for its style of pizza."
71
+ elif state == 'NV':
72
+ text_to_speak = "🎰 **Nevada** \nπŸŒ† Known for Las Vegas and its casinos \n🏜️ Includes part of the Mojave Desert \nπŸŽͺ Entertainment is a major industry \nπŸ’Ž Known for the Hoover Dam \nπŸ‘½ Area 51 is located here."
73
+
74
+ st.markdown(text_to_speak)
75
+ plot_state_outline(state)
76
+
77
+ if st.button(f"πŸ”Š Read {state}'s Facts Aloud"):
78
+ generate_speech_textarea(text_to_speak)