Spaces:
Sleeping
Sleeping
Update app.py
Browse files8 unmodified lines910111213141575 unmodified lines91929394959697989910010110210311 unmodified lines1151161171181191201211221231241255 unmodified lines1311321331341351361371381391408 unmodified linesfrom langchain_core.messages import HumanMessage, AIMessagefrom langchain.memory import ConversationBufferMemoryfrom langchain.document_loaders import PyPDFLoaderfrom fuzzywuzzy import process# Set page configst.set_page_config(page_title="Tbank Assistant", layout="wide")75 unmodified linesdocument_chain = create_stuff_documents_chain(chat, question_answering_prompt)important_terms = ["Tbank", "Chairman", "CEO", "products", "services"] # Add more terms as neededreturn retriever, document_chain, important_terms# Load componentswith st.spinner("Initializing Tbank Assistant..."):retriever, document_chain, important_terms = initialize_components()# Initialize memory for each sessionif "memory" not in st.session_state:11 unmodified lineswith st.chat_message(message["role"]):st.markdown(message["content"])def fuzzy_match(query, choices, threshold=80):result = process.extractOne(query, choices)if result and result[1] >= threshold:return result[0]return None# React to user inputif prompt := st.chat_input("What would you like to know about Tbank?"):5 unmodified lineswith st.chat_message("assistant"):message_placeholder = st.empty()# Fuzzy match important termsmatched_term = fuzzy_match(prompt.lower(), important_terms)if matched_term:prompt = f"{prompt} (Matched term: {matched_term})"# Retrieve relevant documentsdocs = retriever.get_relevant_documents(prompt)8 unmodified lines9101112131475 unmodified lines9091929394959697989910010110211 unmodified lines1141151161171181195 unmodified lines1251261271281291301311321331348 unmodified linesfrom langchain_core.messages import HumanMessage, AIMessagefrom langchain.memory import ConversationBufferMemoryfrom langchain.document_loaders import PyPDFLoader# Set page configst.set_page_config(page_title="Tbank Assistant", layout="wide")75 unmodified linesdocument_chain = create_stuff_documents_chain(chat, question_answering_prompt)return retriever, document_chain# Load componentswith st.spinner("Initializing Tbank Assistant..."):retriever, document_chain = initialize_components()# Initialize memory for each sessionif "memory" not in st.session_state:11 unmodified lineswith st.chat_message(message["role"]):st.markdown(message["content"])# React to user inputif prompt := st.chat_input("What would you like to know about Tbank?"):5 unmodified lineswith st.chat_message("assistant"):message_placeholder = st.empty()# # Fuzzy match important terms# matched_term = fuzzy_match(prompt.lower(), important_terms)# if matched_term:# prompt = f"{prompt} (Matched term: {matched_term})"# Retrieve relevant documentsdocs = retriever.get_relevant_documents(prompt)
app.py+7-13