|
import streamlit as st |
|
import requests |
|
import os |
|
import json |
|
import pandas as pd |
|
|
|
|
|
def call_ai_model_initial(all_message): |
|
url = "https://api.together.xyz/v1/chat/completions" |
|
payload = { |
|
"model": "NousResearch/Nous-Hermes-2-Yi-34B", |
|
"temperature": 1.05, |
|
"top_p": 0.9, |
|
"top_k": 50, |
|
"repetition_penalty": 1, |
|
"n": 1, |
|
"messages": [{"role": "user", "content": all_message}], |
|
"stream_tokens": True, |
|
} |
|
|
|
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY') |
|
if TOGETHER_API_KEY is None: |
|
raise ValueError("TOGETHER_API_KEY environment variable not set.") |
|
|
|
headers = { |
|
"accept": "application/json", |
|
"content-type": "application/json", |
|
"Authorization": f"Bearer {TOGETHER_API_KEY}", |
|
} |
|
|
|
response = requests.post(url, json=payload, headers=headers, stream=True) |
|
response.raise_for_status() |
|
|
|
return response |
|
|
|
|
|
def call_ai_model_analysis(analysis_text): |
|
url = "https://api.together.xyz/v1/chat/completions" |
|
payload = { |
|
"model": "NousResearch/Nous-Hermes-2-Yi-34B", |
|
"temperature": 1.05, |
|
"top_p": 0.9, |
|
"top_k": 50, |
|
"repetition_penalty": 1, |
|
"n": 1, |
|
"messages": [{"role": "user", "content": analysis_text}], |
|
"stream_tokens": True, |
|
} |
|
|
|
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY') |
|
if TOGETHER_API_KEY is None: |
|
raise ValueError("TOGETHER_API_KEY environment variable not set.") |
|
|
|
headers = { |
|
"accept": "application/json", |
|
"content-type": "application/json", |
|
"Authorization": f"Bearer {TOGETHER_API_KEY}", |
|
} |
|
|
|
response = requests.post(url, json=payload, headers=headers, stream=True) |
|
response.raise_for_status() |
|
|
|
return response |
|
|
|
|
|
st.title("Climate Impact on Sports Performance and Infrastructure") |
|
st.write("Analyze and visualize the impact of climate conditions on sports performance and infrastructure.") |
|
|
|
|
|
temperature = st.number_input("Temperature (°C):", min_value=-50, max_value=50, value=25) |
|
humidity = st.number_input("Humidity (%):", min_value=0, max_value=100, value=50) |
|
wind_speed = st.number_input("Wind Speed (km/h):", min_value=0.0, max_value=200.0, value=15.0) |
|
uv_index = st.number_input("UV Index:", min_value=0, max_value=11, value=5) |
|
air_quality_index = st.number_input("Air Quality Index:", min_value=0, max_value=500, value=100) |
|
precipitation = st.number_input("Precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0) |
|
atmospheric_pressure = st.number_input("Atmospheric Pressure (hPa):", min_value=900, max_value=1100, value=1013) |
|
|
|
|
|
latitude = st.number_input("Latitude:", min_value=-90.0, max_value=90.0, value=0.0) |
|
longitude = st.number_input("Longitude:", min_value=-180.0, max_value=180.0, value=0.0) |
|
|
|
if st.button("Generate Prediction"): |
|
all_message = ( |
|
f"Assess the impact on sports performance and infrastructure based on climate conditions: " |
|
f"Temperature {temperature}°C, Humidity {humidity}%, Wind Speed {wind_speed} km/h, UV Index {uv_index}, " |
|
f"Air Quality Index {air_quality_index}, Precipitation {precipitation} mm, Atmospheric Pressure {atmospheric_pressure} hPa. " |
|
f"Location: Latitude {latitude}, Longitude {longitude}." |
|
f"After analyzing that, I want you to visualize the data in the best way possible, might be in a table, using a chart or any other way so that it could be easy to understand." |
|
) |
|
|
|
try: |
|
with st.spinner("Analyzing climate conditions..."): |
|
initial_response = call_ai_model_initial(all_message) |
|
|
|
initial_text = "" |
|
for line in initial_response.iter_lines(): |
|
if line: |
|
line_content = line.decode('utf-8') |
|
if line_content.startswith("data: "): |
|
line_content = line_content[6:] |
|
try: |
|
json_data = json.loads(line_content) |
|
if "choices" in json_data: |
|
delta = json_data["choices"][0]["delta"] |
|
if "content" in delta: |
|
initial_text += delta["content"] |
|
except json.JSONDecodeError: |
|
continue |
|
|
|
st.success("Initial analysis completed!") |
|
|
|
with st.spinner("Generating predictions..."): |
|
analysis_text = ( |
|
f"Analyze the following text and extract a performance score based on the climate conditions and their impact: {initial_text}" |
|
) |
|
analysis_response = call_ai_model_analysis(analysis_text) |
|
|
|
analysis_result = "" |
|
for line in analysis_response.iter_lines(): |
|
if line: |
|
line_content = line.decode('utf-8') |
|
if line_content.startswith("data: "): |
|
line_content = line_content[6:] |
|
try: |
|
json_data = json.loads(line_content) |
|
if "choices" in json_data: |
|
delta = json_data["choices"][0]["delta"] |
|
if "content" in delta: |
|
analysis_result += delta["content"] |
|
except json.JSONDecodeError: |
|
continue |
|
|
|
st.success("Predictions generated!") |
|
|
|
|
|
|
|
performance_score = "N/A" |
|
for line in analysis_result.split('\n'): |
|
if "Performance Score:" in line: |
|
performance_score = line.split(":")[1].strip() |
|
|
|
|
|
results_data = { |
|
"Condition": ["Temperature", "Humidity", "Wind Speed", "UV Index", "Air Quality Index", "Precipitation", "Atmospheric Pressure"], |
|
"Value": [temperature, humidity, wind_speed, uv_index, air_quality_index, precipitation, atmospheric_pressure] |
|
} |
|
results_df = pd.DataFrame(results_data) |
|
|
|
|
|
st.subheader("Results Summary") |
|
st.table(results_df) |
|
|
|
|
|
st.markdown("**Predicted Impact on Performance and Infrastructure:**") |
|
st.markdown(initial_text.strip()) |
|
|
|
|
|
st.markdown(f"**Performance Score:** {performance_score}") |
|
|
|
except ValueError as ve: |
|
st.error(f"Configuration error: {ve}") |
|
except requests.exceptions.RequestException as re: |
|
st.error(f"Request error: {re}") |
|
except Exception as e: |
|
st.error(f"An unexpected error occurred: {e}") |
|
|