File size: 2,924 Bytes
a8dbf83
 
 
054592e
 
 
3a81b83
054592e
 
 
 
 
 
 
 
 
 
 
 
 
a44ffa4
054592e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
# Temporary fix for pydantic issue @see https://github.com/streamlit/streamlit/issues/3218
import pydantic
pydantic.class_validators._FUNCS.clear()
import streamlit as st
from streamlit_elements import elements, media
from chatbot import YouTubeChatbot
import sidebar
from langchain.memory import ConversationBufferWindowMemory
from streamlit_chat import message

def index():
    def put_media_player():
        with elements("media_player"):
            video_url = st.session_state.get('video_url')
            media.Player(url=video_url, controls=True)
    def clear_submit():
        st.session_state["submit"] = False

    st.set_page_config(page_title="YoutuberGPT", page_icon="🤖", layout="wide")
    st.header("🤖YoutuberGPT")
    sidebar.sidebar()

    if 'responses' not in st.session_state:
        st.session_state['responses'] = ["Ask any question related to the video"]

    if 'requests' not in st.session_state:
        st.session_state['requests'] = []

    if 'buffer_memory' not in st.session_state:
        st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True)

    if 'video_url' not in st.session_state:
        st.session_state['video_url']= []

    videocontainer = st.container()
    # container for chat history
    response_container = st.container()
    # container for text box
    textcontainer = st.container()


    with videocontainer:
        video_url = st.text_input("YouTube Video Url:", on_change=clear_submit)
        st.session_state['video_url'] = video_url
        if video_url:
            put_media_player()

    with textcontainer:
        question = st.text_area("Question:", key="question")
        if st.button("Run") or st.session_state.get("submit"):
            try:
                with st.spinner('preparing answer'):
                    chatbot = YouTubeChatbot()
                    db = chatbot.create_db_from_youtube_video_url(video_url)
                    if db is None:
                        return st.error("There is no transcript")

                    response = chatbot.get_response_from_query(db, question)
                    if response is None:
                        return st.error("There is no answer or something went wrong")
                    else:
                        st.session_state.requests.append(question)
                        st.session_state.responses.append(response)
                        st.session_state["submit"] = True
            except:
                return st.error("There is no answer or something went wrong")

    with response_container:
        if st.session_state['responses']:

            for i in range(len(st.session_state['responses'])):
                message(st.session_state['responses'][i],key=str(i))
                if i < len(st.session_state['requests']):
                    message(st.session_state["requests"][i], is_user=True,key=str(i)+ '_user')

index()