File size: 3,033 Bytes
2eeebbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e71f6b
 
 
 
2eeebbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a2107b
 
 
 
 
 
 
2eeebbc
 
aa7278e
 
 
 
 
 
 
2eeebbc
 
 
5a2107b
 
 
 
 
2eeebbc
 
304262e
5a2107b
 
 
 
 
2eeebbc
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
import os
import json
import streamlit as st
from ind_checklist_stlit import load_preprocessed_data, init_vector_store, create_rag_chain

# Prevent Streamlit from auto-reloading on file changes
os.environ["STREAMLIT_WATCHER_TYPE"] = "none"

# Define the preprocessed file path
PREPROCESSED_FILE = "preprocessed_docs.json"

# Caching function to prevent redundant RAG processing
@st.cache_data
def cached_response(question: str):
    """Retrieve cached response if available, otherwise compute response."""
    return st.session_state.rag_chain.invoke({"question": question})["response"]

def main():
    st.title("Appian IND Application Assistant")
    st.markdown("Chat about Investigational New Drug Applications")

    # Button to clear chat history
    if st.button("Clear Chat History"):
        st.session_state.messages = []
        if hasattr(st, "rerun"):
            st.rerun()
        else:
            st.experimental_rerun()

    # Initialize session state
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # Load preprocessed data and initialize the RAG chain
    if "rag_chain" not in st.session_state:
        if not os.path.exists(PREPROCESSED_FILE):
            st.error(f"❌ Preprocessed file '{PREPROCESSED_FILE}' not found. Please run preprocessing first.")
            return  # Stop execution if preprocessed data is missing

        with st.spinner("πŸ”„ Initializing knowledge base..."):
            documents = load_preprocessed_data(PREPROCESSED_FILE)
            vectorstore = init_vector_store(documents)
            st.session_state.rag_chain = create_rag_chain(vectorstore.as_retriever())

    # Display chat history
    for message in st.session_state.messages:
        role = message["role"]
        content = message["content"]
        if hasattr(st, "chat_message"):
            with st.chat_message(role):
                st.markdown(content)
        else:
            st.write(f"**{role.capitalize()}:** {content}")

    # Chat input and response handling
    # Check if st.chat_input is available (Streamlit 1.2 or higher)
    if hasattr(st, "chat_input"):
        prompt = st.chat_input("Ask about IND requirements")
    else:
        prompt = st.text_input("Ask about IND requirements")

    if prompt:
        st.session_state.messages.append({"role": "user", "content": prompt})

        # Display user message
        if hasattr(st, "chat_message"):
            with st.chat_message("user"):
                st.markdown(prompt)
        else:
            st.write(f"**User:** {prompt}")

        # Generate response (cached if already asked before)
        response = cached_response(prompt)
        if hasattr(st, "chat_message"):
            with st.chat_message("assistant"):
                st.markdown(response)
        else:
            st.write(f"**Assistant:** {response}")

        # Store bot response in chat history
        st.session_state.messages.append({"role": "assistant", "content": response})

if __name__ == "__main__":
    main()