cima-free-chat / app.py
ethanrom's picture
Upload 10 files
36c0029
raw
history blame
2.77 kB
import os
import gdown
file_id = "1G_OK4BPWgqpgEK540VReSKhzOxKspjgy"
output_folder = "faiss_index" # Specify the folder name
output_file = os.path.join(output_folder, "index.faiss")
if not os.path.exists(output_file):
os.makedirs(output_folder, exist_ok=True) # Create the folder if it doesn't exist
url = f"https://drive.google.com/uc?id={file_id}"
gdown.download(url, output_file, quiet=False)
import streamlit as st
from streamlit_option_menu import option_menu
from markup import app_intro
import langchain
from langchain.cache import InMemoryCache
from query_data import chat_chain
langchain.llm_cache = InMemoryCache()
def tab1():
st.header("CIMA Chatbot")
col1, col2 = st.columns([1, 2])
with col1:
st.image("image.jpg", use_column_width=True)
with col2:
st.markdown(app_intro(), unsafe_allow_html=True)
metadata_list = []
unique_metadata_list = []
seen = set()
def tab4():
if "messages" not in st.session_state:
st.session_state.messages = []
st.header("πŸ—£οΈ Chat with the AI about the ingested documents! πŸ“š")
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if user_input := st.chat_input("User Input"):
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
with st.spinner("Generating Response..."):
with st.chat_message("assistant"):
response = chat_chain({"question": user_input})
answer = response['answer']
source_documents = response['source_documents']
for doc in source_documents:
if hasattr(doc, 'metadata'):
metadata = doc.metadata
metadata_list.append(metadata)
for metadata in metadata_list:
metadata_tuple = tuple(metadata.items())
if metadata_tuple not in seen:
unique_metadata_list.append(metadata)
seen.add(metadata_tuple)
st.write(answer)
st.write(unique_metadata_list)
st.session_state.messages.append({"role": "assistant", "content": answer})
def main():
st.set_page_config(page_title="CIMA Chat", page_icon=":memo:", layout="wide")
tabs = ["Intro", "Chat"]
with st.sidebar:
current_tab = option_menu("Select a Tab", tabs, menu_icon="cast")
tab_functions = {
"Intro": tab1,
"Chat": tab4,
}
if current_tab in tab_functions:
tab_functions[current_tab]()
if __name__ == "__main__":
main()