Spaces:
Configuration error

File size: 3,916 Bytes
3e412dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import streamlit as st
import yaml
from dotenv import load_dotenv
from src.document_loader import load_document
from src.chunking_embedding import setup_chunking_and_embedding
from src.vector_store import create_vectorstore
from langchain_groq import ChatGroq
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferWindowMemory
from src.utils import log_interaction

# Load environment variables from .env file
load_dotenv()


# Load configuration
config_path = os.path.join(os.getcwd(), "config", "config.yaml")  # Ensure correct path
with open(config_path, "r") as f:
    config = yaml.safe_load(f)

# Get the GROQ API key from environment variables
groq_api_key = os.getenv('groq_api_key')
if not groq_api_key:
    raise ValueError("GROQ_API_KEY not found. Please set it in the .env file.")

# Streamlit UI
st.set_page_config(page_title="Your Document AI Assistant", page_icon="📄", layout="centered")
st.title("🪅 Document AI Assistant!")

# Sidebar for document upload
uploaded_file = st.sidebar.file_uploader(label="Upload your document (PDF/DOC/DOCX)", type=['pdf', 'doc', 'docx'])
loader_type = st.sidebar.selectbox('Choose a loader type', config['loaders'])
embedding_model = st.sidebar.selectbox('Choose an embedding model', config['embedding_models'])
chunking_strategy = st.sidebar.selectbox('Choose a chunking strategy', config['chunking_strategies'])
chunk_size = st.sidebar.number_input('Chunk Size', min_value=100, value=500, step=100)
chunk_overlap = st.sidebar.number_input('Chunk Overlap', min_value=0, value=100, step=100)
temperature = st.sidebar.slider('Temperature', min_value=0.0, max_value=1.0, value=0.0, step=0.1)
top_p = st.sidebar.slider('Top-p', min_value=0.0, max_value=1.0, value=0.9, step=0.1)

# Initialize session state for conversation
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []
if 'conversation_chain' not in st.session_state:
    st.session_state.conversation_chain = None

# Process the uploaded file
if uploaded_file is not None:
    file_path = os.path.join("temp", uploaded_file.name)
    os.makedirs("temp", exist_ok=True)
    with open(file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    documents = load_document(file_path, loader_type)
    doc_chunks, embeddings = setup_chunking_and_embedding(documents, chunking_strategy, chunk_size, chunk_overlap, embedding_model)
    vectorstore = create_vectorstore(doc_chunks, embeddings)

    # Create the conversational retrieval chain
    llm = ChatGroq(groq_api_key=groq_api_key, model_name='llama-3.3-70b-versatile', temperature=temperature)
    retriever = vectorstore.as_retriever()
    memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history", return_messages=True)
    st.session_state.conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)

# User input for asking a question
user_input = st.text_input("Ask a question:")
if st.button("Ask Question"):
    if user_input:
        with st.chat_message("user"):
            st.markdown(user_input)
        
        with st.chat_message("assistant"):
            response = st.session_state.conversation_chain({"question": user_input})
            assistant_response = response['answer']
            st.markdown(assistant_response)
            st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})

            # Log interaction
            params = {
                "loader": loader_type,
                "chunking_strategy": chunking_strategy,
                "chunk_size": chunk_size,
                "chunk_overlap": chunk_overlap,
                "embedding_model": embedding_model,
                "temperature": temperature,
                "top_p": top_p
            }
            log_interaction(user_input, assistant_response, params)