Update app.py
Browse files
app.py
CHANGED
|
@@ -19,77 +19,72 @@ def load_llm():
|
|
| 19 |
temperature=0.5
|
| 20 |
)
|
| 21 |
return llm
|
| 22 |
-
# Function for conversational chat
|
| 23 |
-
def conversational_chat(query):
|
| 24 |
-
result = chain({"question": query, "chat_history": st.session_state['history']})
|
| 25 |
-
st.session_state['history'].append((query, result["answer"]))
|
| 26 |
-
return result["answer"]
|
| 27 |
-
def main():
|
| 28 |
-
# Set the title for the Streamlit app
|
| 29 |
-
st.title("Llama2 Chat CSV - π¦π¦")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 37 |
-
tmp_file.write(uploaded_file.getvalue())
|
| 38 |
-
tmp_file_path = tmp_file.name
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
db.save_local(DB_FAISS_PATH)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
# Load the language model
|
| 53 |
-
llm = load_llm()
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
container = st.container()
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
user_input = st.text_input("Query:", placeholder="Talk to csv data π (:", key='input')
|
| 79 |
-
submit_button = st.form_submit_button(label='Send')
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
| 91 |
-
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
if
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
temperature=0.5
|
| 20 |
)
|
| 21 |
return llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Set the title for the Streamlit app
|
| 24 |
+
st.title("Llama2 Chat CSV - π¦π¦")
|
| 25 |
|
| 26 |
+
# Create a file uploader in the sidebar
|
| 27 |
+
uploaded_file = st.sidebar.file_uploader("Upload File", type="csv")
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Handle file upload
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 32 |
+
tmp_file.write(uploaded_file.getvalue())
|
| 33 |
+
tmp_file_path = tmp_file.name
|
| 34 |
|
| 35 |
+
# Load CSV data using CSVLoader
|
| 36 |
+
loader = CSVLoader(file_path=tmp_file_path, encoding="utf-8", csv_args={'delimiter': ','})
|
| 37 |
+
data = loader.load()
|
| 38 |
|
| 39 |
+
# Create embeddings using Sentence Transformers
|
| 40 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', model_kwargs={'device': 'cpu'})
|
|
|
|
| 41 |
|
| 42 |
+
# Create a FAISS vector store and save embeddings
|
| 43 |
+
db = FAISS.from_documents(data, embeddings)
|
| 44 |
+
db.save_local(DB_FAISS_PATH)
|
| 45 |
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Load the language model
|
| 48 |
+
llm = load_llm()
|
| 49 |
|
| 50 |
+
# Create a conversational chain
|
| 51 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
| 52 |
|
| 53 |
+
# Function for conversational chat
|
| 54 |
+
def conversational_chat(query):
|
| 55 |
+
result = chain({"question": query, "chat_history": st.session_state['history']})
|
| 56 |
+
st.session_state['history'].append((query, result["answer"]))
|
| 57 |
+
return result["answer"]
|
| 58 |
|
| 59 |
+
# Initialize chat history
|
| 60 |
+
if 'history' not in st.session_state:
|
| 61 |
+
st.session_state['history'] = []
|
| 62 |
|
| 63 |
+
# Initialize messages
|
| 64 |
+
if 'generated' not in st.session_state:
|
| 65 |
+
st.session_state['generated'] = ["Hello ! Ask me(LLAMA2) about " + uploaded_file.name + " π€"]
|
| 66 |
|
| 67 |
+
if 'past' not in st.session_state:
|
| 68 |
+
st.session_state['past'] = ["Hey ! π"]
|
|
|
|
| 69 |
|
| 70 |
+
# Create containers for chat history and user input
|
| 71 |
+
response_container = st.container()
|
| 72 |
+
container = st.container()
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# User input form
|
| 75 |
+
with container:
|
| 76 |
+
with st.form(key='my_form', clear_on_submit=True):
|
| 77 |
+
user_input = st.text_input("Query:", placeholder="Talk to csv data π (:", key='input')
|
| 78 |
+
submit_button = st.form_submit_button(label='Send')
|
| 79 |
|
| 80 |
+
if submit_button and user_input:
|
| 81 |
+
output = conversational_chat(user_input)
|
| 82 |
+
st.session_state['past'].append(user_input)
|
| 83 |
+
st.session_state['generated'].append(output)
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
# Display chat history
|
| 86 |
+
if st.session_state['generated']:
|
| 87 |
+
with response_container:
|
| 88 |
+
for i in range(len(st.session_state['generated'])):
|
| 89 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="big-smile")
|
| 90 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="thumbs")
|