Orami01's picture
Update app.py
91658ee
raw
history blame contribute delete
No virus
4.03 kB
import streamlit as st
from streamlit_chat import message
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.llms import CTransformers
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
import sys
import tempfile
# Initialize the CSVLoader to load the uploaded CSV file
from langchain.document_loaders.csv_loader import CSVLoader
DB_FAISS_PATH = 'vectorstore/db_faiss'
from transformers import pipeline
pipe = pipeline("text-generation",model="mistralai/Mistral-7B-v0.1",model_type="llama",max_new_tokens=512,temperature=0.1 )
# Display the title of the web page
st.title("Chat with CSV using open source LLM Inference Point πŸ¦™πŸ¦œ")
# Display a markdown message with additional information
st.markdown("<h3 style='text-align: center; color: white;'>Built by <a href='https://github.com/AIAnytime'>AI Anytime with ❀️ </a></h3>", unsafe_allow_html=True)
# Allow users to upload a CSV file
uploaded_file = st.sidebar.file_uploader("Upload your Data", type="csv")
if uploaded_file:
# Initialize the CSVLoader to load the uploaded CSV file
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
tmp_file.write(uploaded_file.getvalue())
tmp_file_path = tmp_file.name
# Initialize the CSVLoader to load the uploaded CSV file
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
data = loader.load()
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',model_kwargs={'device': 'cpu'})
db = FAISS.from_documents(data, embeddings)
db.save_local(DB_FAISS_PATH)
llm = load_llm()
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
def conversational_chat(query):
# Maintain and display the chat history
result = chain({"question": query, "chat_history": st.session_state['history']})
# Maintain and display the chat history
st.session_state['history'].append((query, result["answer"]))
return result["answer"]
# Maintain and display the chat history
if 'history' not in st.session_state:
# Maintain and display the chat history
st.session_state['history'] = []
# Maintain and display the chat history
if 'generated' not in st.session_state:
# Maintain and display the chat history
st.session_state['generated'] = ["Hello ! Ask me anything about " + uploaded_file.name + " πŸ€—"]
# Maintain and display the chat history
if 'past' not in st.session_state:
# Maintain and display the chat history
st.session_state['past'] = ["Hey ! πŸ‘‹"]
#container for the chat history
response_container = st.container()
#container for the user's text input
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_input("Query:", placeholder="Talk to your csv data here (:", key='input')
submit_button = st.form_submit_button(label='Send')
if submit_button and user_input:
output = conversational_chat(user_input)
# Maintain and display the chat history
st.session_state['past'].append(user_input)
# Maintain and display the chat history
st.session_state['generated'].append(output)
# Maintain and display the chat history
if st.session_state['generated']:
with response_container:
# Maintain and display the chat history
for i in range(len(st.session_state['generated'])):
# Maintain and display the chat history
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
# Maintain and display the chat history
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")