File size: 2,379 Bytes
95d1bce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
import streamlit as st
from YoutubeChat.YoutubeChat import YoutubeChatBot
from openai import AuthenticationError

def get_user_input():
    st.title("Setup")

    # Input field for YouTube link
    youtube_link = st.text_input("Enter YouTube Video Link")

    # Input field for OpenAI API key
    openai_api_key = st.text_input("Enter OpenAI API Key", type = "password", help="Please delete the API key after use for your own security.")

    # Button to start chat
    if st.button("Start Chat"):
        if youtube_link.strip() == "":
            st.error("Please enter a valid YouTube link.")
        elif openai_api_key.strip() == "":
            st.error("Please enter your OpenAI API key.")
        else:
            try:
                yt = YoutubeChatBot(youtube_link, OPENAI_API_KEY=openai_api_key)
                start_message = yt.send('start protocol')
                st.session_state.start_message = start_message
                st.session_state.chat_object = yt
                st.session_state.messages = [{'role':'assistant', 'content': start_message}]
                st.rerun()  # Rerun the app to switch to the main chat page
            except AuthenticationError as e:
                st.error("Incorrect API key provided. Please check your API key.")

                # Optionally provide guidance on how to resolve the issue
                st.write("You can find your API key at https://platform.openai.com/account/api-keys.")
            except ValueError as e:
                st.error("Incorrect video link provided! ")


def main_chat_page():
    st.title("Main Chat Page")

    for message in st.session_state.messages:
        with st.chat_message(message.get("role")):
            st.write(message.get("content"))

    # Display chat input box
    prompt = st.chat_input("Ask something")
    
    # If user inputs something
    if prompt:
        st.session_state.messages.append({"role": "user", "content": prompt})

        with st.chat_message("user"):
            st.write(prompt)
        
        result = st.session_state.chat_object.send(prompt)
        st.session_state.messages.append({'role':'assistant', "content": result})

        with st.chat_message("assistant"):
            st.write(result)

if __name__ == "__main__":
    if "start_message" not in st.session_state:
        get_user_input()
    else:
        main_chat_page()