Update app.py
Browse files
app.py
CHANGED
|
@@ -64,8 +64,8 @@ def call_ai_model_analysis(analysis_text):
|
|
| 64 |
return response
|
| 65 |
|
| 66 |
# Streamlit app layout
|
| 67 |
-
st.title("Climate Impact on Sports Performance and Infrastructure")
|
| 68 |
-
st.write("Analyze and visualize the impact of climate conditions on sports performance and infrastructure.")
|
| 69 |
|
| 70 |
# Inputs for climate conditions
|
| 71 |
temperature = st.number_input("Temperature (°C):", min_value=-50, max_value=50, value=25)
|
|
@@ -76,27 +76,37 @@ air_quality_index = st.number_input("Air Quality Index:", min_value=0, max_value
|
|
| 76 |
precipitation = st.number_input("Precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0)
|
| 77 |
atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa):", min_value=900, max_value=1100, value=1013)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
# Sports and athlete inputs
|
| 80 |
-
sports = st.multiselect("Select sports:", ["
|
| 81 |
athlete_types = st.multiselect("Select athlete types:", ["Professional", "Amateur", "Youth", "Senior"])
|
| 82 |
|
| 83 |
# Infrastructure inputs
|
| 84 |
-
infrastructure_types = st.multiselect("Select infrastructure types:", ["Outdoor Stadium", "Indoor Arena", "
|
| 85 |
|
| 86 |
-
if st.button("Generate Prediction"):
|
| 87 |
all_message = (
|
| 88 |
-
f"Assess the impact on sports performance, athletes, and infrastructure based on climate conditions: "
|
| 89 |
f"Temperature {temperature}°C, Humidity {humidity}%, Wind Speed {wind_speed} km/h, UV Index {uv_index}, "
|
| 90 |
f"Air Quality Index {air_quality_index}, Precipitation {precipitation} mm, Atmospheric Pressure {atmospheric_pressure} hPa. "
|
|
|
|
| 91 |
f"Sports: {', '.join(sports)}. Athlete types: {', '.join(athlete_types)}. "
|
| 92 |
f"Infrastructure types: {', '.join(infrastructure_types)}. "
|
| 93 |
-
f"Provide a detailed analysis of how these conditions affect performance, health, and infrastructure. "
|
| 94 |
f"Include specific impacts for each sport, athlete type, and infrastructure type. "
|
| 95 |
-
f"Also, provide an overall performance score and an infrastructure impact score, both as percentages.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
try:
|
| 99 |
-
with st.spinner("Analyzing climate conditions..."):
|
| 100 |
initial_response = call_ai_model_initial(all_message)
|
| 101 |
|
| 102 |
initial_text = ""
|
|
@@ -114,97 +124,129 @@ if st.button("Generate Prediction"):
|
|
| 114 |
except json.JSONDecodeError:
|
| 115 |
continue
|
| 116 |
|
| 117 |
-
st.success("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
f"both as percentages. Include lines that say 'Performance Score: XX%' and 'Infrastructure Impact Score: YY%' "
|
| 123 |
-
f"in your response. Here's the text to analyze: {initial_text}"
|
| 124 |
-
)
|
| 125 |
-
analysis_response = call_ai_model_analysis(analysis_text)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if line:
|
| 130 |
line_content = line.decode('utf-8')
|
| 131 |
if line_content.startswith("data: "):
|
| 132 |
-
line_content = line_content[6:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
try:
|
| 134 |
json_data = json.loads(line_content)
|
| 135 |
if "choices" in json_data:
|
| 136 |
delta = json_data["choices"][0]["delta"]
|
| 137 |
if "content" in delta:
|
| 138 |
-
|
| 139 |
except json.JSONDecodeError:
|
| 140 |
continue
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
performance_score = "N/A"
|
| 146 |
-
infrastructure_score = "N/A"
|
| 147 |
-
for line in analysis_result.split('\n'):
|
| 148 |
-
if "performance score:" in line.lower():
|
| 149 |
-
performance_score = line.split(":")[-1].strip()
|
| 150 |
-
elif "infrastructure impact score:" in line.lower():
|
| 151 |
-
infrastructure_score = line.split(":")[-1].strip()
|
| 152 |
-
|
| 153 |
-
# Prepare data for visualization
|
| 154 |
-
results_data = {
|
| 155 |
-
"Condition": ["Temperature", "Humidity", "Wind Speed", "UV Index", "Air Quality Index", "Precipitation", "Atmospheric Pressure"],
|
| 156 |
-
"Value": [temperature, humidity, wind_speed, uv_index, air_quality_index, precipitation, atmospheric_pressure]
|
| 157 |
-
}
|
| 158 |
-
results_df = pd.DataFrame(results_data)
|
| 159 |
-
|
| 160 |
-
# Display results in a table
|
| 161 |
-
st.subheader("Climate Conditions Summary")
|
| 162 |
-
st.table(results_df)
|
| 163 |
-
|
| 164 |
-
# Create a radar chart for climate conditions
|
| 165 |
-
fig = go.Figure(data=go.Scatterpolar(
|
| 166 |
-
r=[temperature/50*100, humidity, wind_speed/2, uv_index/11*100, air_quality_index/5, precipitation/5, (atmospheric_pressure-900)/2],
|
| 167 |
-
theta=results_df['Condition'],
|
| 168 |
-
fill='toself'
|
| 169 |
-
))
|
| 170 |
-
fig.update_layout(
|
| 171 |
-
polar=dict(
|
| 172 |
-
radialaxis=dict(visible=True, range=[0, 100])
|
| 173 |
-
),
|
| 174 |
-
showlegend=False
|
| 175 |
-
)
|
| 176 |
-
st.plotly_chart(fig)
|
| 177 |
-
|
| 178 |
-
# Display prediction
|
| 179 |
-
st.subheader("Predicted Impact on Performance and Infrastructure")
|
| 180 |
-
st.markdown(initial_text.strip())
|
| 181 |
-
|
| 182 |
-
# Display performance and infrastructure scores
|
| 183 |
-
col1, col2 = st.columns(2)
|
| 184 |
-
with col1:
|
| 185 |
-
st.metric("Performance Score", performance_score)
|
| 186 |
-
with col2:
|
| 187 |
-
st.metric("Infrastructure Impact Score", infrastructure_score)
|
| 188 |
-
|
| 189 |
-
# Display analyzed sports and infrastructure
|
| 190 |
-
st.subheader("Analyzed Components")
|
| 191 |
-
col1, col2, col3 = st.columns(3)
|
| 192 |
-
with col1:
|
| 193 |
-
st.write("**Sports:**")
|
| 194 |
-
for sport in sports:
|
| 195 |
-
st.write(f"- {sport}")
|
| 196 |
-
with col2:
|
| 197 |
-
st.write("**Athlete Types:**")
|
| 198 |
-
for athlete_type in athlete_types:
|
| 199 |
-
st.write(f"- {athlete_type}")
|
| 200 |
-
with col3:
|
| 201 |
-
st.write("**Infrastructure Types:**")
|
| 202 |
-
for infra_type in infrastructure_types:
|
| 203 |
-
st.write(f"- {infra_type}")
|
| 204 |
-
|
| 205 |
-
# Display raw analysis result for debugging
|
| 206 |
-
with st.expander("Show Raw Analysis"):
|
| 207 |
-
st.text(analysis_result)
|
| 208 |
|
| 209 |
except ValueError as ve:
|
| 210 |
st.error(f"Configuration error: {ve}")
|
|
|
|
| 64 |
return response
|
| 65 |
|
| 66 |
# Streamlit app layout
|
| 67 |
+
st.title("Climate Impact on Sports Performance and Infrastructure in Kenya")
|
| 68 |
+
st.write("Analyze and visualize the impact of climate conditions on sports performance and infrastructure, with a focus on Kenya.")
|
| 69 |
|
| 70 |
# Inputs for climate conditions
|
| 71 |
temperature = st.number_input("Temperature (°C):", min_value=-50, max_value=50, value=25)
|
|
|
|
| 76 |
precipitation = st.number_input("Precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0)
|
| 77 |
atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa):", min_value=900, max_value=1100, value=1013)
|
| 78 |
|
| 79 |
+
# Kenya-specific inputs
|
| 80 |
+
region = st.selectbox("Select region in Kenya:", ["Nairobi", "Mombasa", "Kisumu", "Nakuru", "Eldoret"])
|
| 81 |
+
elevation = st.number_input("Elevation (m):", min_value=0, max_value=5000, value=1000)
|
| 82 |
+
|
| 83 |
# Sports and athlete inputs
|
| 84 |
+
sports = st.multiselect("Select sports:", ["Athletics", "Football", "Rugby", "Volleyball", "Boxing", "Swimming"])
|
| 85 |
athlete_types = st.multiselect("Select athlete types:", ["Professional", "Amateur", "Youth", "Senior"])
|
| 86 |
|
| 87 |
# Infrastructure inputs
|
| 88 |
+
infrastructure_types = st.multiselect("Select infrastructure types:", ["Outdoor Stadium", "Indoor Arena", "Training Facility", "Community Sports Ground"])
|
| 89 |
|
| 90 |
+
if st.button("Generate Prediction and Analysis"):
|
| 91 |
all_message = (
|
| 92 |
+
f"Assess the impact on sports performance, athletes, and infrastructure in Kenya based on climate conditions: "
|
| 93 |
f"Temperature {temperature}°C, Humidity {humidity}%, Wind Speed {wind_speed} km/h, UV Index {uv_index}, "
|
| 94 |
f"Air Quality Index {air_quality_index}, Precipitation {precipitation} mm, Atmospheric Pressure {atmospheric_pressure} hPa. "
|
| 95 |
+
f"Region: {region}, Elevation: {elevation}m. "
|
| 96 |
f"Sports: {', '.join(sports)}. Athlete types: {', '.join(athlete_types)}. "
|
| 97 |
f"Infrastructure types: {', '.join(infrastructure_types)}. "
|
| 98 |
+
f"Provide a detailed analysis of how these conditions affect performance, health, and infrastructure in Kenya. "
|
| 99 |
f"Include specific impacts for each sport, athlete type, and infrastructure type. "
|
| 100 |
+
f"Also, provide an overall performance score and an infrastructure impact score, both as percentages. "
|
| 101 |
+
f"Suggest mitigation strategies for both performance and infrastructure. "
|
| 102 |
+
f"Assess the socio-economic implications of these climate impacts on sports in Kenya, including equitable access to sports facilities. "
|
| 103 |
+
f"Organize the information in tables with the following columns: Climate Conditions, Impact on Sports Performance, "
|
| 104 |
+
f"Impact on Athletes' Health, Impact on Infrastructure, Mitigation Strategies, Socio-Economic Implications. "
|
| 105 |
+
f"Be as accurate and specific to Kenya as possible in your analysis."
|
| 106 |
)
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
with st.spinner("Analyzing climate conditions and generating predictions..."):
|
| 110 |
initial_response = call_ai_model_initial(all_message)
|
| 111 |
|
| 112 |
initial_text = ""
|
|
|
|
| 124 |
except json.JSONDecodeError:
|
| 125 |
continue
|
| 126 |
|
| 127 |
+
st.success("Analysis completed!")
|
| 128 |
+
|
| 129 |
+
# Display prediction
|
| 130 |
+
st.subheader("Climate Impact Analysis for Sports in Kenya")
|
| 131 |
+
st.markdown(initial_text.strip())
|
| 132 |
+
|
| 133 |
+
# Extract and display scores
|
| 134 |
+
performance_score = "N/A"
|
| 135 |
+
infrastructure_score = "N/A"
|
| 136 |
+
for line in initial_text.split('\n'):
|
| 137 |
+
if "performance score:" in line.lower():
|
| 138 |
+
performance_score = line.split(":")[-1].strip()
|
| 139 |
+
elif "infrastructure impact score:" in line.lower():
|
| 140 |
+
infrastructure_score = line.split(":")[-1].strip()
|
| 141 |
+
|
| 142 |
+
# Display performance and infrastructure scores
|
| 143 |
+
col1, col2 = st.columns(2)
|
| 144 |
+
with col1:
|
| 145 |
+
st.metric("Overall Performance Score", performance_score)
|
| 146 |
+
with col2:
|
| 147 |
+
st.metric("Infrastructure Impact Score", infrastructure_score)
|
| 148 |
+
|
| 149 |
+
# Prepare data for visualization
|
| 150 |
+
results_data = {
|
| 151 |
+
"Condition": ["Temperature", "Humidity", "Wind Speed", "UV Index", "Air Quality Index", "Precipitation", "Atmospheric Pressure"],
|
| 152 |
+
"Value": [temperature, humidity, wind_speed, uv_index, air_quality_index, precipitation, atmospheric_pressure]
|
| 153 |
+
}
|
| 154 |
+
results_df = pd.DataFrame(results_data)
|
| 155 |
|
| 156 |
+
# Display results in a table
|
| 157 |
+
st.subheader("Climate Conditions Summary")
|
| 158 |
+
st.table(results_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
# Create a radar chart for climate conditions
|
| 161 |
+
fig = go.Figure(data=go.Scatterpolar(
|
| 162 |
+
r=[temperature/50*100, humidity, wind_speed/2, uv_index/11*100, air_quality_index/5, precipitation/5, (atmospheric_pressure-900)/2],
|
| 163 |
+
theta=results_df['Condition'],
|
| 164 |
+
fill='toself'
|
| 165 |
+
))
|
| 166 |
+
fig.update_layout(
|
| 167 |
+
polar=dict(
|
| 168 |
+
radialaxis=dict(visible=True, range=[0, 100])
|
| 169 |
+
),
|
| 170 |
+
showlegend=False
|
| 171 |
+
)
|
| 172 |
+
st.plotly_chart(fig)
|
| 173 |
+
|
| 174 |
+
# Display analyzed sports and infrastructure
|
| 175 |
+
st.subheader("Analyzed Components")
|
| 176 |
+
col1, col2, col3 = st.columns(3)
|
| 177 |
+
with col1:
|
| 178 |
+
st.write("**Sports:**")
|
| 179 |
+
for sport in sports:
|
| 180 |
+
st.write(f"- {sport}")
|
| 181 |
+
with col2:
|
| 182 |
+
st.write("**Athlete Types:**")
|
| 183 |
+
for athlete_type in athlete_types:
|
| 184 |
+
st.write(f"- {athlete_type}")
|
| 185 |
+
with col3:
|
| 186 |
+
st.write("**Infrastructure Types:**")
|
| 187 |
+
for infra_type in infrastructure_types:
|
| 188 |
+
st.write(f"- {infra_type}")
|
| 189 |
+
|
| 190 |
+
# Socio-economic impact analysis
|
| 191 |
+
st.subheader("Socio-Economic Impact Analysis")
|
| 192 |
+
socio_economic_prompt = (
|
| 193 |
+
f"Based on the climate conditions and sports analysis for {region}, Kenya, "
|
| 194 |
+
f"provide a brief assessment of the socio-economic implications, including impacts on: "
|
| 195 |
+
f"1) Local economy, 2) Community health, 3) Sports tourism, 4) Equitable access to sports facilities. "
|
| 196 |
+
f"Consider the specific context of Kenya and the selected region."
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
with st.spinner("Analyzing socio-economic impacts..."):
|
| 200 |
+
socio_economic_response = call_ai_model_analysis(socio_economic_prompt)
|
| 201 |
+
socio_economic_text = ""
|
| 202 |
+
for line in socio_economic_response.iter_lines():
|
| 203 |
if line:
|
| 204 |
line_content = line.decode('utf-8')
|
| 205 |
if line_content.startswith("data: "):
|
| 206 |
+
line_content = line_content[6:]
|
| 207 |
+
try:
|
| 208 |
+
json_data = json.loads(line_content)
|
| 209 |
+
if "choices" in json_data:
|
| 210 |
+
delta = json_data["choices"][0]["delta"]
|
| 211 |
+
if "content" in delta:
|
| 212 |
+
socio_economic_text += delta["content"]
|
| 213 |
+
except json.JSONDecodeError:
|
| 214 |
+
continue
|
| 215 |
+
|
| 216 |
+
st.markdown(socio_economic_text.strip())
|
| 217 |
+
|
| 218 |
+
# Mitigation strategies
|
| 219 |
+
st.subheader("Mitigation Strategies")
|
| 220 |
+
mitigation_prompt = (
|
| 221 |
+
f"Based on the climate conditions and sports analysis for {region}, Kenya, "
|
| 222 |
+
f"suggest specific mitigation strategies for: "
|
| 223 |
+
f"1) Improving athlete performance and health, 2) Enhancing infrastructure resilience, "
|
| 224 |
+
f"3) Ensuring equitable access to sports facilities. "
|
| 225 |
+
f"Consider the specific context of Kenya and the selected region."
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
with st.spinner("Generating mitigation strategies..."):
|
| 229 |
+
mitigation_response = call_ai_model_analysis(mitigation_prompt)
|
| 230 |
+
mitigation_text = ""
|
| 231 |
+
for line in mitigation_response.iter_lines():
|
| 232 |
+
if line:
|
| 233 |
+
line_content = line.decode('utf-8')
|
| 234 |
+
if line_content.startswith("data: "):
|
| 235 |
+
line_content = line_content[6:]
|
| 236 |
try:
|
| 237 |
json_data = json.loads(line_content)
|
| 238 |
if "choices" in json_data:
|
| 239 |
delta = json_data["choices"][0]["delta"]
|
| 240 |
if "content" in delta:
|
| 241 |
+
mitigation_text += delta["content"]
|
| 242 |
except json.JSONDecodeError:
|
| 243 |
continue
|
| 244 |
+
|
| 245 |
+
st.markdown(mitigation_text.strip())
|
| 246 |
|
| 247 |
+
# Display raw analysis result for debugging
|
| 248 |
+
with st.expander("Show Raw Analysis"):
|
| 249 |
+
st.text(initial_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
except ValueError as ve:
|
| 252 |
st.error(f"Configuration error: {ve}")
|