Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,9 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_chroma import Chroma
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 5 |
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from sentence_transformers import SentenceTransformer
|
|
@@ -22,7 +23,8 @@ if 'initialized' not in st.session_state:
|
|
| 22 |
try:
|
| 23 |
with st.spinner("Initializing..."):
|
| 24 |
# Initialize embeddings model
|
| 25 |
-
model_path = "sentence-transformers/all-MiniLM-L12-v2"
|
|
|
|
| 26 |
st.session_state.embedding_function = HuggingFaceEmbeddings(
|
| 27 |
model_name=model_path,
|
| 28 |
model_kwargs={'device': 'cpu'},
|
|
@@ -59,7 +61,7 @@ if 'initialized' not in st.session_state:
|
|
| 59 |
# Load QA chain
|
| 60 |
st.session_state.qa_chain = load_qa_chain(
|
| 61 |
llm=st.session_state.chat_model,
|
| 62 |
-
chain_type="
|
| 63 |
memory=st.session_state.memory,
|
| 64 |
prompt=prompt
|
| 65 |
)
|
|
@@ -73,12 +75,12 @@ if 'initialized' not in st.session_state:
|
|
| 73 |
|
| 74 |
# Clear chat history buttons
|
| 75 |
if st.button("Clear Chat History"):
|
| 76 |
-
if 'memory' in st.session_state
|
| 77 |
st.session_state.memory.clear()
|
| 78 |
st.experimental_rerun() # Refresh the app to reflect the cleared history
|
| 79 |
|
| 80 |
# Display chat history if initialized
|
| 81 |
-
if st.session_state.initialized and 'memory' in st.session_state
|
| 82 |
if st.session_state.memory.buffer_as_messages:
|
| 83 |
for message in st.session_state.memory.buffer_as_messages:
|
| 84 |
if message.type == "ai":
|
|
@@ -87,21 +89,20 @@ if st.session_state.initialized and 'memory' in st.session_state and st.session_
|
|
| 87 |
st.chat_message(name="human", avatar="👤").write(message.content)
|
| 88 |
|
| 89 |
# Input for new query
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)["output_text"]
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_chroma import Chroma
|
| 4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 6 |
from langchain.memory import ConversationBufferMemory
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
from langchain_groq import ChatGroq
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 23 |
try:
|
| 24 |
with st.spinner("Initializing..."):
|
| 25 |
# Initialize embeddings model
|
| 26 |
+
model_path = "sentence-transformers/all-MiniLM-L12-v2" # Use a smaller, faster model
|
| 27 |
+
|
| 28 |
st.session_state.embedding_function = HuggingFaceEmbeddings(
|
| 29 |
model_name=model_path,
|
| 30 |
model_kwargs={'device': 'cpu'},
|
|
|
|
| 61 |
# Load QA chain
|
| 62 |
st.session_state.qa_chain = load_qa_chain(
|
| 63 |
llm=st.session_state.chat_model,
|
| 64 |
+
chain_type="stuff",
|
| 65 |
memory=st.session_state.memory,
|
| 66 |
prompt=prompt
|
| 67 |
)
|
|
|
|
| 75 |
|
| 76 |
# Clear chat history buttons
|
| 77 |
if st.button("Clear Chat History"):
|
| 78 |
+
if 'memory' in st.session_state:
|
| 79 |
st.session_state.memory.clear()
|
| 80 |
st.experimental_rerun() # Refresh the app to reflect the cleared history
|
| 81 |
|
| 82 |
# Display chat history if initialized
|
| 83 |
+
if st.session_state.initialized and 'memory' in st.session_state:
|
| 84 |
if st.session_state.memory.buffer_as_messages:
|
| 85 |
for message in st.session_state.memory.buffer_as_messages:
|
| 86 |
if message.type == "ai":
|
|
|
|
| 89 |
st.chat_message(name="human", avatar="👤").write(message.content)
|
| 90 |
|
| 91 |
# Input for new query
|
| 92 |
+
query = st.chat_input("Ask something")
|
| 93 |
+
if query:
|
| 94 |
+
try:
|
| 95 |
+
with st.spinner("Answering..."):
|
| 96 |
+
# Perform similarity search and get response
|
| 97 |
+
docs = st.session_state.docsearch.similarity_search(query, k=1) # Reduced k for speed
|
| 98 |
+
response = st.session_state.qa_chain(
|
| 99 |
+
{"input_documents": docs, "human_input": query},
|
| 100 |
+
return_only_outputs=True
|
| 101 |
+
)["output_text"]
|
|
|
|
| 102 |
|
| 103 |
+
# Display new message
|
| 104 |
+
st.chat_message(name="human", avatar="👤").write(query)
|
| 105 |
+
st.chat_message(name="ai", avatar="🤖").write(response)
|
| 106 |
|
| 107 |
+
except Exception as e:
|
| 108 |
+
st.error(f"An error occurred: {e}")
|