Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,90 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
6 |
-
from src.
|
7 |
-
from src.
|
8 |
-
from
|
9 |
-
from
|
10 |
-
from langchain.
|
11 |
-
from
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
st.
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
os.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
st.
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
"
|
81 |
-
"
|
82 |
-
"
|
83 |
-
"
|
84 |
-
"
|
85 |
-
"
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
1 |
+
# import library
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
import yaml
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from src.document_loader import load_document
|
7 |
+
from src.chunking_embedding import setup_chunking_and_embedding
|
8 |
+
from src.vector_store import create_vectorstore
|
9 |
+
from langchain_groq import ChatGroq
|
10 |
+
from langchain.chains import ConversationalRetrievalChain
|
11 |
+
from langchain.memory import ConversationBufferWindowMemory
|
12 |
+
from src.utils import log_interaction
|
13 |
+
|
14 |
+
# Load environment variables from .env file
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
|
18 |
+
# Load configuration
|
19 |
+
config_path = os.path.join(os.getcwd(), "config", "config.yaml") # Ensure correct path
|
20 |
+
with open(config_path, "r") as f:
|
21 |
+
config = yaml.safe_load(f)
|
22 |
+
|
23 |
+
# Get the GROQ API key from environment variables
|
24 |
+
groq_api_key = os.getenv('groq_api_key')
|
25 |
+
if not groq_api_key:
|
26 |
+
raise ValueError("GROQ_API_KEY not found. Please set it in the .env file.")
|
27 |
+
|
28 |
+
# Streamlit UI
|
29 |
+
st.set_page_config(page_title="Your Document AI Assistant", page_icon="π", layout="centered")
|
30 |
+
st.title("πͺ
Document AI Assistant!")
|
31 |
+
|
32 |
+
# Sidebar for document upload
|
33 |
+
uploaded_file = st.sidebar.file_uploader(label="Upload your document (PDF/DOC/DOCX)", type=['pdf', 'doc', 'docx'])
|
34 |
+
loader_type = st.sidebar.selectbox('Choose a loader type', config['loaders'])
|
35 |
+
embedding_model = st.sidebar.selectbox('Choose an embedding model', config['embedding_models'])
|
36 |
+
chunking_strategy = st.sidebar.selectbox('Choose a chunking strategy', config['chunking_strategies'])
|
37 |
+
chunk_size = st.sidebar.number_input('Chunk Size', min_value=100, value=500, step=100)
|
38 |
+
chunk_overlap = st.sidebar.number_input('Chunk Overlap', min_value=0, value=100, step=100)
|
39 |
+
temperature = st.sidebar.slider('Temperature', min_value=0.0, max_value=1.0, value=0.0, step=0.1)
|
40 |
+
top_p = st.sidebar.slider('Top-p', min_value=0.0, max_value=1.0, value=0.9, step=0.1)
|
41 |
+
|
42 |
+
# Initialize session state for conversation
|
43 |
+
if 'chat_history' not in st.session_state:
|
44 |
+
st.session_state.chat_history = []
|
45 |
+
if 'conversation_chain' not in st.session_state:
|
46 |
+
st.session_state.conversation_chain = None
|
47 |
+
|
48 |
+
# Process the uploaded file
|
49 |
+
if uploaded_file is not None:
|
50 |
+
file_path = os.path.join("temp", uploaded_file.name)
|
51 |
+
os.makedirs("temp", exist_ok=True)
|
52 |
+
with open(file_path, "wb") as f:
|
53 |
+
f.write(uploaded_file.getbuffer())
|
54 |
+
|
55 |
+
documents = load_document(file_path, loader_type)
|
56 |
+
doc_chunks, embeddings = setup_chunking_and_embedding(documents, chunking_strategy, chunk_size, chunk_overlap, embedding_model)
|
57 |
+
vectorstore = create_vectorstore(doc_chunks, embeddings)
|
58 |
+
|
59 |
+
# Create the conversational retrieval chain
|
60 |
+
llm = ChatGroq(groq_api_key=groq_api_key, model_name='llama-3.3-70b-versatile', temperature=temperature)
|
61 |
+
retriever = vectorstore.as_retriever()
|
62 |
+
memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history", return_messages=True)
|
63 |
+
st.session_state.conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)
|
64 |
+
|
65 |
+
# User input for asking a question
|
66 |
+
user_input = st.text_input("Ask a question:")
|
67 |
+
if st.button("Ask Question"):
|
68 |
+
if user_input:
|
69 |
+
with st.chat_message("user"):
|
70 |
+
st.markdown(user_input)
|
71 |
+
|
72 |
+
with st.chat_message("assistant"):
|
73 |
+
response = st.session_state.conversation_chain({"question": user_input})
|
74 |
+
assistant_response = response['answer']
|
75 |
+
st.markdown(assistant_response)
|
76 |
+
st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
|
77 |
+
|
78 |
+
# Log interaction
|
79 |
+
params = {
|
80 |
+
"loader": loader_type,
|
81 |
+
"chunking_strategy": chunking_strategy,
|
82 |
+
"chunk_size": chunk_size,
|
83 |
+
"chunk_overlap": chunk_overlap,
|
84 |
+
"embedding_model": embedding_model,
|
85 |
+
"temperature": temperature,
|
86 |
+
"top_p": top_p
|
87 |
+
}
|
88 |
+
log_interaction(user_input, assistant_response, params)
|
89 |
+
|
90 |
+
|