newchatbox / app.py
Ademola1448's picture
Update app.py
21dbf8c verified
import os
import streamlit as st
from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
# Load environment variables
load_dotenv()
# Set OpenAI API key
openai_api_key = os.getenv("OPENAI_API_KEY")
# Initialize ChatOpenAI
chat = ChatOpenAI(api_key=openai_api_key)
# Streamlit app setup
st.title("Football Chatbot")
# Initialize session state for conversation history
if 'history' not in st.session_state:
st.session_state.history = []
def get_openai_response(prompt):
try:
response = chat([HumanMessage(content=prompt)])
message = response.content.strip()
return message
except Exception as e:
return str(e)
def main():
st.write("Ask anything about football and get responses from the chatbot!")
user_input = st.text_input("You: ", "")
if st.button("Send"):
if user_input:
response = get_openai_response(user_input)
st.session_state.history.append(("You: ", user_input))
st.session_state.history.append(("Chatbot: ", response))
# Display conversation history
for sender, message in st.session_state.history:
st.write(f"{sender} {message}")
if __name__ == "__main__":
main()