Update app.py
Browse files
app.py
CHANGED
|
@@ -35,13 +35,54 @@ def call_ai_model(all_message):
|
|
| 35 |
|
| 36 |
return response
|
| 37 |
|
| 38 |
-
# Function to request
|
| 39 |
-
def
|
| 40 |
all_message = (
|
| 41 |
-
f"Provide the expected sports performance value (as a
|
| 42 |
)
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
generated_text = ""
|
| 46 |
for line in response.iter_lines():
|
| 47 |
if line:
|
|
@@ -52,42 +93,47 @@ def get_performance_data(temperature):
|
|
| 52 |
json_data = json.loads(line_content)
|
| 53 |
if "choices" in json_data:
|
| 54 |
delta = json_data["choices"][0]["delta"]
|
| 55 |
-
if "content" in delta:
|
| 56 |
generated_text += delta["content"]
|
| 57 |
except json.JSONDecodeError:
|
| 58 |
continue
|
| 59 |
-
try:
|
| 60 |
-
performance_value = float(generated_text.strip())
|
| 61 |
-
return performance_value
|
| 62 |
-
except ValueError:
|
| 63 |
-
continue
|
| 64 |
|
| 65 |
-
|
| 66 |
-
st.title("Climate Impact on Sports Performance and Infrastructure")
|
| 67 |
-
st.write("Analyze and visualize the impact of climate conditions on sports performance and infrastructure.")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
performance_values.append(performance_value)
|
| 83 |
-
|
| 84 |
|
|
|
|
| 85 |
# Generate line graph
|
| 86 |
fig, ax = plt.subplots()
|
| 87 |
ax.plot(temperatures, performance_values, marker='o')
|
| 88 |
ax.set_xlabel('Temperature (°C)')
|
| 89 |
ax.set_ylabel('Performance Score')
|
| 90 |
-
ax.set_title('Temperature vs. Sports Performance')
|
| 91 |
st.pyplot(fig)
|
| 92 |
|
| 93 |
except ValueError as ve:
|
|
|
|
| 35 |
|
| 36 |
return response
|
| 37 |
|
| 38 |
+
# Function to request numeric performance data from AI
|
| 39 |
+
def get_numeric_performance_data(temperature):
|
| 40 |
all_message = (
|
| 41 |
+
f"Provide the expected numeric sports performance value (as a score) at a temperature of {temperature}°C."
|
| 42 |
)
|
| 43 |
+
response = call_ai_model(all_message)
|
| 44 |
+
generated_text = ""
|
| 45 |
+
for line in response.iter_lines():
|
| 46 |
+
if line:
|
| 47 |
+
line_content = line.decode('utf-8')
|
| 48 |
+
if line_content.startswith("data: "):
|
| 49 |
+
line_content = line_content[6:] # Strip "data: " prefix
|
| 50 |
+
try:
|
| 51 |
+
json_data = json.loads(line_content)
|
| 52 |
+
if "choices" in json_data:
|
| 53 |
+
delta = json_data["choices"][0]["delta"]
|
| 54 |
+
if "content" in delta and delta["content"].strip().replace('.', '', 1).isdigit():
|
| 55 |
+
return float(delta["content"].strip())
|
| 56 |
+
except json.JSONDecodeError:
|
| 57 |
+
continue
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
# Streamlit app layout
|
| 61 |
+
st.title("Climate Impact on Sports Performance and Infrastructure")
|
| 62 |
+
st.write("Analyze and visualize the impact of climate conditions on sports performance and infrastructure.")
|
| 63 |
+
|
| 64 |
+
# Inputs for climate conditions
|
| 65 |
+
temperature = st.number_input("Temperature (°C):", min_value=-50, max_value=50, value=25)
|
| 66 |
+
humidity = st.number_input("Humidity (%):", min_value=0, max_value=100, value=50)
|
| 67 |
+
wind_speed = st.number_input("Wind Speed (km/h):", min_value=0.0, max_value=200.0, value=15.0)
|
| 68 |
+
uv_index = st.number_input("UV Index:", min_value=0, max_value=11, value=5)
|
| 69 |
+
air_quality_index = st.number_input("Air Quality Index:", min_value=0, max_value=500, value=100)
|
| 70 |
+
precipitation = st.number_input("Precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0)
|
| 71 |
+
atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa):", min_value=900, max_value=1100, value=1013)
|
| 72 |
+
|
| 73 |
+
if st.button("Generate Prediction"):
|
| 74 |
+
all_message = (
|
| 75 |
+
f"Assess the impact on sports performance and infrastructure based on climate conditions: "
|
| 76 |
+
f"Temperature {temperature}°C, Humidity {humidity}%, Wind Speed {wind_speed} km/h, UV Index {uv_index}, "
|
| 77 |
+
f"Air Quality Index {air_quality_index}, Precipitation {precipitation} mm, Atmospheric Pressure {atmospheric_pressure} hPa."
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
with st.spinner("Analyzing climate conditions..."):
|
| 82 |
+
response = call_ai_model(all_message)
|
| 83 |
+
|
| 84 |
+
st.success("Initial analysis complete. Generating detailed predictions...")
|
| 85 |
+
|
| 86 |
generated_text = ""
|
| 87 |
for line in response.iter_lines():
|
| 88 |
if line:
|
|
|
|
| 93 |
json_data = json.loads(line_content)
|
| 94 |
if "choices" in json_data:
|
| 95 |
delta = json_data["choices"][0]["delta"]
|
| 96 |
+
if "content" in delta and delta["content"].strip().replace('.', '', 1).isdigit():
|
| 97 |
generated_text += delta["content"]
|
| 98 |
except json.JSONDecodeError:
|
| 99 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
st.success("Detailed predictions generated.")
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# Prepare data for visualization
|
| 104 |
+
results_data = {
|
| 105 |
+
"Condition": ["Temperature", "Humidity", "Wind Speed", "UV Index", "Air Quality Index", "Precipitation", "Atmospheric Pressure"],
|
| 106 |
+
"Value": [temperature, humidity, wind_speed, uv_index, air_quality_index, precipitation, atmospheric_pressure]
|
| 107 |
+
}
|
| 108 |
+
results_df = pd.DataFrame(results_data)
|
| 109 |
|
| 110 |
+
# Display results in a table
|
| 111 |
+
st.subheader("Results Summary")
|
| 112 |
+
st.table(results_df)
|
| 113 |
+
|
| 114 |
+
# Display prediction
|
| 115 |
+
st.markdown("**Predicted Impact on Performance and Infrastructure:**")
|
| 116 |
+
st.markdown(generated_text.strip())
|
| 117 |
+
|
| 118 |
+
st.success("Generating performance data...")
|
| 119 |
+
|
| 120 |
+
# Generate numeric performance data for different temperatures
|
| 121 |
+
temperatures = range(-10, 41, 5) # Temperatures from -10°C to 40°C in 5°C increments
|
| 122 |
+
performance_values = []
|
| 123 |
+
for temp in temperatures:
|
| 124 |
+
st.spinner(f"Fetching performance data for {temp}°C...")
|
| 125 |
+
performance_value = get_numeric_performance_data(temp)
|
| 126 |
+
if performance_value is not None:
|
| 127 |
performance_values.append(performance_value)
|
| 128 |
+
time.sleep(1)
|
| 129 |
|
| 130 |
+
if performance_values:
|
| 131 |
# Generate line graph
|
| 132 |
fig, ax = plt.subplots()
|
| 133 |
ax.plot(temperatures, performance_values, marker='o')
|
| 134 |
ax.set_xlabel('Temperature (°C)')
|
| 135 |
ax.set_ylabel('Performance Score')
|
| 136 |
+
ax.set_title('Temperature vs. Numeric Sports Performance')
|
| 137 |
st.pyplot(fig)
|
| 138 |
|
| 139 |
except ValueError as ve:
|