Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,61 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Simple Chatbot
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
import streamlit as st
|
| 6 |
-
import
|
| 7 |
import os
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
|
| 10 |
-
load_dotenv()
|
| 11 |
-
|
| 12 |
-
# Initialize the client
|
| 13 |
-
openai.api_key = os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
|
| 14 |
-
|
| 15 |
-
model_link = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 16 |
-
|
| 17 |
-
def reset_conversation():
|
| 18 |
-
"""Resets Conversation"""
|
| 19 |
-
st.session_state.conversation = []
|
| 20 |
-
st.session_state.messages = []
|
| 21 |
-
return None
|
| 22 |
-
|
| 23 |
-
# Set the temperature value directly in the code
|
| 24 |
-
temperature = 0.5
|
| 25 |
-
|
| 26 |
-
# Add a button to clear conversation
|
| 27 |
-
if st.button('Reset Chat'):
|
| 28 |
-
reset_conversation()
|
| 29 |
-
|
| 30 |
-
# Initialize chat history
|
| 31 |
-
if "messages" not in st.session_state:
|
| 32 |
-
st.session_state.messages = []
|
| 33 |
-
|
| 34 |
-
st.title("Mistral-7B Chatbot")
|
| 35 |
-
st.subheader("Ask me anything!")
|
| 36 |
-
|
| 37 |
-
# Display chat messages from history on app rerun
|
| 38 |
-
for message in st.session_state.messages:
|
| 39 |
-
with st.chat_message(message["role"]):
|
| 40 |
-
st.markdown(message["content"])
|
| 41 |
-
|
| 42 |
-
# Accept user input
|
| 43 |
-
prompt = st.chat_input("Type your message here...")
|
| 44 |
-
|
| 45 |
-
if prompt:
|
| 46 |
-
# Display user message in chat message container
|
| 47 |
-
with st.chat_message("user"):
|
| 48 |
-
st.markdown(prompt)
|
| 49 |
-
# Add user message to chat history
|
| 50 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 51 |
-
|
| 52 |
-
# Display assistant response in chat message container
|
| 53 |
-
with st.chat_message("assistant"):
|
| 54 |
-
try:
|
| 55 |
-
response = openai.Completion.create(
|
| 56 |
-
engine=model_link,
|
| 57 |
-
prompt=prompt,
|
| 58 |
-
max_tokens=3000,
|
| 59 |
-
temperature=temperature
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
response_content = response.choices[0].text.strip()
|
| 63 |
-
st.markdown(response_content)
|
| 64 |
-
st.session_state.messages.append({"role": "assistant", "content": response_content})
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Function to call the Together API with the provided model
|
| 6 |
+
def call_ai_model(all_message):
|
| 7 |
+
url = "https://api.together.xyz/v1/chat/completions"
|
| 8 |
+
payload = {
|
| 9 |
+
"model": "NousResearch/Nous-Hermes-2-Yi-34B",
|
| 10 |
+
"temperature": 1.05,
|
| 11 |
+
"top_p": 0.9,
|
| 12 |
+
"top_k": 50,
|
| 13 |
+
"repetition_penalty": 1,
|
| 14 |
+
"n": 1,
|
| 15 |
+
"messages": [{"role": "user", "content": all_message}],
|
| 16 |
+
"stream_tokens": True,
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
TOGETHER_API_KEY = os.getenv('TOGETHER_API_KEY')
|
| 20 |
+
if TOGETHER_API_KEY is None:
|
| 21 |
+
raise ValueError("TOGETHER_API_KEY environment variable not set.")
|
| 22 |
+
|
| 23 |
+
headers = {
|
| 24 |
+
"accept": "application/json",
|
| 25 |
+
"content-type": "application/json",
|
| 26 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
response = requests.post(url, json=payload, headers=headers, stream=True)
|
| 30 |
+
response.raise_for_status() # Ensure HTTP request was successful
|
| 31 |
+
|
| 32 |
+
# Debugging line to print raw response
|
| 33 |
+
for line in response.iter_lines():
|
| 34 |
+
if line:
|
| 35 |
+
yield line.decode('utf-8')
|
| 36 |
+
|
| 37 |
+
# Streamlit app layout
|
| 38 |
+
st.title("Climate Change Impact on Sports Using AI")
|
| 39 |
+
st.write("Predict and mitigate the impacts of climate change on sports performance and infrastructure.")
|
| 40 |
+
|
| 41 |
+
# Input fields for user to enter data
|
| 42 |
+
temperature = st.number_input("Enter temperature (°C):", min_value=-50, max_value=50, value=25)
|
| 43 |
+
humidity = st.number_input("Enter humidity (%):", min_value=0, max_value=100, value=50)
|
| 44 |
+
air_quality = st.number_input("Enter air quality index (AQI):", min_value=0, max_value=500, value=100)
|
| 45 |
+
precipitation = st.number_input("Enter precipitation (mm):", min_value=0.0, max_value=500.0, value=10.0)
|
| 46 |
+
|
| 47 |
+
if st.button("Generate Prediction"):
|
| 48 |
+
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."
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
with st.spinner("Generating response..."):
|
| 52 |
+
response_lines = call_ai_model(all_message)
|
| 53 |
+
generated_text = "".join(response_lines)
|
| 54 |
+
st.success("Response generated successfully!")
|
| 55 |
+
st.write(generated_text)
|
| 56 |
+
except ValueError as ve:
|
| 57 |
+
st.error(f"Configuration error: {ve}")
|
| 58 |
+
except requests.exceptions.RequestException as re:
|
| 59 |
+
st.error(f"Request error: {re}")
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.error(f"An unexpected error occurred: {e}")
|