File size: 1,668 Bytes
5785952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e5370e
5785952
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)