File size: 2,521 Bytes
9f54a3b
71ec4a8
9f54a3b
a9c7401
71ec4a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import streamlit as st
import requests
import os

# Function to call the Together API with the provided model
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()  # Ensure HTTP request was successful

    # Debugging line to print raw response
    for line in response.iter_lines():
        if line:
            yield line.decode('utf-8')

# Streamlit app layout
st.title("Climate Change Impact on Sports Using AI")
st.write("Predict and mitigate the impacts of climate change on sports performance and infrastructure.")

# Input fields for user to enter data
temperature = st.number_input("Enter temperature (°C):", min_value=-50, max_value=50, value=25)
humidity = st.number_input("Enter humidity (%):", min_value=0, max_value=100, value=50)
air_quality = st.number_input("Enter air quality index (AQI):", min_value=0, max_value=500, value=100)
precipitation = st.number_input("Enter precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0)

if st.button("Generate Prediction"):
    all_message = f"Predict the impact of the following climate conditions on sports performance and infrastructure: temperature {temperature}°C, humidity {humidity}%, air quality index {air_quality}, and precipitation {precipitation}mm."
    
    try:
        with st.spinner("Generating response..."):
            response_lines = call_ai_model(all_message)
            generated_text = "".join(response_lines)
            st.success("Response generated successfully!")
            st.write(generated_text)
    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}")