Chat_Assistant / app.py
YAMITEK's picture
Update app.py
7e5370e verified
import streamlit as st
import openai
import os
openai.api_key = os.getenv("openapikey")
if 'chat_log' not in st.session_state:
st.session_state.chat_log = []
def chat_with_gpt(prompt, history):
messages = [{"role": "system", "content": "You are a helpful assistant."},]
for message in history:
if "You:" in message:
messages.append({"role": "user", "content": message.replace("You:","")})
if "Bot:" in message:
messages.append({"role": "assistant", "content": message.replace("Bot:", "")})
messages.append({"role": "user", "content": prompt})
response = openai.chat.completions.create(
model="gpt-3.5-turbo", # Or another suitable model
messages=messages,
max_tokens=150
)
return response.choices[0].message.content.strip()
st.title("Contextual Chatbot using openai")
st.markdown(f"""
<style>
/* Set the background image for the entire app */
.stApp {{
background-image: url("https://i.pinimg.com/736x/29/51/8d/29518df9a720818938a3a58cf6c026df.jpg");
background-size: 1300px;
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}}
</style>
""", unsafe_allow_html=True)
user_input = st.text_input("You:")
if st.button("Enter"):
if user_input:
st.session_state.chat_log.append(f"You: {user_input}")
response = chat_with_gpt(user_input, st.session_state.chat_log)
st.session_state.chat_log.append(f"Bot: {response}")
for message in st.session_state.chat_log:
st.write(message)