|
import streamlit as st |
|
import requests |
|
import os |
|
import json |
|
import pandas as pd |
|
import time |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def call_ai_model(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 get_performance_data(temperature): |
|
all_message = ( |
|
f"Provide the expected sports performance value (as a numerical score) at a temperature of {temperature}°C." |
|
) |
|
while True: |
|
response = call_ai_model(all_message) |
|
generated_text = "" |
|
for line in 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: |
|
generated_text += delta["content"] |
|
except json.JSONDecodeError: |
|
continue |
|
try: |
|
performance_value = float(generated_text.strip()) |
|
return performance_value |
|
except ValueError: |
|
continue |
|
|
|
|
|
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) |
|
|
|
if st.button("Generate Prediction"): |
|
try: |
|
with st.spinner("Generating predictions..."): |
|
st.success("Predictions generated. Generating performance data...") |
|
|
|
|
|
temperatures = range(-10, 41, 5) |
|
performance_values = [] |
|
for temp in temperatures: |
|
performance_value = get_performance_data(temp) |
|
performance_values.append(performance_value) |
|
time.sleep(1) |
|
|
|
|
|
fig, ax = plt.subplots() |
|
ax.plot(temperatures, performance_values, marker='o') |
|
ax.set_xlabel('Temperature (°C)') |
|
ax.set_ylabel('Performance Score') |
|
ax.set_title('Temperature vs. Sports Performance') |
|
st.pyplot(fig) |
|
|
|
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}") |
|
|