Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -28,14 +28,19 @@ set_global_service_context(service_context) | |
| 28 |  | 
| 29 | 
             
            # Transcribe function
         | 
| 30 | 
             
            def transcribe_video(youtube_url):
         | 
| 31 | 
            -
                 | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 39 |  | 
| 40 | 
             
            # Streamlit UI
         | 
| 41 | 
             
            st.title("YouTube Video Chatbot")
         | 
| @@ -47,32 +52,26 @@ if youtube_url and "query_engine" not in st.session_state: | |
| 47 | 
             
                st.write("Transcribing video... Please wait.")
         | 
| 48 | 
             
                st.session_state.query_engine = transcribe_video(youtube_url)
         | 
| 49 |  | 
| 50 | 
            -
            # Chatbot UI
         | 
| 51 | 
             
            if "messages" not in st.session_state:
         | 
| 52 | 
             
                st.session_state.messages = []
         | 
| 53 |  | 
| 54 | 
            -
            # Display chat history
         | 
| 55 | 
             
            for message in st.session_state.messages:
         | 
| 56 | 
            -
                if message["role"] ==  | 
| 57 | 
            -
                    st. | 
| 58 | 
            -
                else:
         | 
| 59 | 
            -
                    st.write(f"Chatbot: {message['content']}")
         | 
| 60 |  | 
| 61 | 
             
            # User input
         | 
| 62 | 
            -
            prompt = st. | 
| 63 |  | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
|  | |
| 66 | 
             
                # Add user message to chat history
         | 
| 67 | 
             
                st.session_state.messages.append({"role": "human", "content": prompt})
         | 
| 68 |  | 
| 69 | 
            -
                # Get response from the chatbot
         | 
| 70 | 
             
                response = st.session_state.query_engine.query(prompt)
         | 
| 71 | 
            -
                response_text = response.response | 
| 72 | 
            -
             | 
|  | |
| 73 | 
             
                # Add assistant response to chat history
         | 
| 74 | 
            -
                st.session_state.messages.append({"role": "assistant", "content":  | 
| 75 | 
            -
             | 
| 76 | 
            -
            # Refresh the page to show the updated chat history
         | 
| 77 | 
            -
            if prompt:
         | 
| 78 | 
            -
                st.experimental_rerun()
         | 
|  | |
| 28 |  | 
| 29 | 
             
            # Transcribe function
         | 
| 30 | 
             
            def transcribe_video(youtube_url):
         | 
| 31 | 
            +
                with st.status("Starting client"):
         | 
| 32 | 
            +
                    client = Client("https://sanchit-gandhi-whisper-jax.hf.space/")
         | 
| 33 | 
            +
                    st.write("Requesting client")
         | 
| 34 | 
            +
                with st.status("Requesting Whisper"):
         | 
| 35 | 
            +
                    result = client.predict(youtube_url, "transcribe", True, fn_index=7)
         | 
| 36 | 
            +
                    st.write("Requesting API...")
         | 
| 37 | 
            +
                    with open(f'{PATH}/docs.txt','w') as f:
         | 
| 38 | 
            +
                        f.write(result[1])
         | 
| 39 | 
            +
                    st.write('Writing File...')
         | 
| 40 | 
            +
                with st.status("Requesting Embeddings"):
         | 
| 41 | 
            +
                    documents = SimpleDirectoryReader(PATH).load_data()
         | 
| 42 | 
            +
                    index = VectorStoreIndex.from_documents(documents)
         | 
| 43 | 
            +
                    return index.as_query_engine()
         | 
| 44 |  | 
| 45 | 
             
            # Streamlit UI
         | 
| 46 | 
             
            st.title("YouTube Video Chatbot")
         | 
|  | |
| 52 | 
             
                st.write("Transcribing video... Please wait.")
         | 
| 53 | 
             
                st.session_state.query_engine = transcribe_video(youtube_url)
         | 
| 54 |  | 
|  | |
| 55 | 
             
            if "messages" not in st.session_state:
         | 
| 56 | 
             
                st.session_state.messages = []
         | 
| 57 |  | 
| 58 | 
            +
            # Display chat messages from history on app rerun
         | 
| 59 | 
             
            for message in st.session_state.messages:
         | 
| 60 | 
            +
                with st.chat_message(message["role"], avatar=("π§βπ»" if message["role"] == 'human' else 'π¦')):
         | 
| 61 | 
            +
                    st.markdown(message["content"])
         | 
|  | |
|  | |
| 62 |  | 
| 63 | 
             
            # User input
         | 
| 64 | 
            +
            prompt = st.chat_input("Ask something about the video:")
         | 
| 65 |  | 
| 66 | 
            +
            if prompt := textinput and "query_engine" in st.session_state:
         | 
| 67 | 
            +
                # Display user message in chat message container
         | 
| 68 | 
            +
                st.chat_message("human",avatar = "π§βπ»").markdown(prompt)
         | 
| 69 | 
             
                # Add user message to chat history
         | 
| 70 | 
             
                st.session_state.messages.append({"role": "human", "content": prompt})
         | 
| 71 |  | 
|  | |
| 72 | 
             
                response = st.session_state.query_engine.query(prompt)
         | 
| 73 | 
            +
                response_text = response.response
         | 
| 74 | 
            +
                with st.chat_message("assistant", avatar='π¦'):
         | 
| 75 | 
            +
                    st.markdown(response_text)
         | 
| 76 | 
             
                # Add assistant response to chat history
         | 
| 77 | 
            +
                st.session_state.messages.append({"role": "assistant", "content": response})
         | 
|  | |
|  | |
|  | |
|  | 

