File size: 7,752 Bytes
c9a97bb
 
 
b65a2d4
 
c9a97bb
 
 
 
b65a2d4
c9a97bb
b65a2d4
 
c9a97bb
 
 
5686026
c9a97bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b65a2d4
c9a97bb
 
b65a2d4
c9a97bb
 
b65a2d4
 
 
c9a97bb
 
 
 
b65a2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9a97bb
 
 
b65a2d4
5686026
 
 
 
 
b65a2d4
 
5686026
 
 
 
 
c9a97bb
 
b65a2d4
c9a97bb
 
 
 
 
 
 
b65a2d4
c9a97bb
b65a2d4
c9a97bb
b65a2d4
c9a97bb
 
 
 
b65a2d4
3432a8c
c9a97bb
 
 
 
 
 
 
 
5686026
 
3432a8c
c9a97bb
 
3432a8c
c9a97bb
 
 
 
 
b65a2d4
 
 
 
 
 
 
 
5686026
 
 
 
 
 
 
b65a2d4
 
 
 
c9a97bb
 
 
 
 
b65a2d4
c9a97bb
b65a2d4
c9a97bb
 
b65a2d4
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import shutil
import streamlit as st
import requests
from bs4 import BeautifulSoup
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_community.llms import Together
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import UnstructuredPDFLoader
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
from langchain_community.document_loaders import UnstructuredExcelLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings

# Set API key
os.environ["TOGETHER_API_KEY"] = os.getenv("TOGETHER_API_KEY")

def inference(chain, input_query):
    """Invoke the processing chain with the input query."""
    result = chain.invoke(input_query)
    return result

def create_chain(retriever, prompt, model):
    """Compose the processing chain with the specified components."""
    chain = (
        {"context": retriever, "question": RunnablePassthrough()}
        | prompt
        | model
        | StrOutputParser()
    )
    return chain

def generate_prompt():
    """Define the prompt template for question answering."""
    template = """<s>[INST] Answer the question in a simple sentence based only on the following context:
                  {context}
                  Question: {question} [/INST] 
               """
    return ChatPromptTemplate.from_template(template)

def configure_model():
    """Configure the language model with specified parameters."""
    return Together(
        model="mistralai/Mixtral-8x7B-Instruct-v0.1",
        temperature=0.1,
        max_tokens=3000,
        top_k=50,
        top_p=0.7,
        repetition_penalty=1.1,
    )

def configure_retriever(documents):
    """Configure the retriever with embeddings and a FAISS vector store."""
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    vector_db = FAISS.from_documents(documents, embeddings)
    return vector_db.as_retriever()

def load_pdf_documents(path):
    """Load and preprocess PDF documents from the specified path."""
    documents = []
    for file in os.listdir(path):
        if file.endswith('.pdf'):
            filepath = os.path.join(path, file)
            loader = UnstructuredPDFLoader(filepath)
            documents.extend(loader.load())
    return documents

def load_word_documents(path):
    """Load and preprocess Word documents from the specified path."""
    documents = []
    for file in os.listdir(path):
        if file.endswith('.docx'):
            filepath = os.path.join(path, file)
            loader = UnstructuredWordDocumentLoader(filepath)
            documents.extend(loader.load())
    return documents

def load_excel_documents(path):
    """Load and preprocess Excel documents from the specified path."""
    documents = []
    for file in os.listdir(path):
        if file.endswith('.xlsx'):
            filepath = os.path.join(path, file)
            loader = UnstructuredExcelLoader(filepath)
            documents.extend(loader.load())
    return documents

def load_documents(path):
    """Load and preprocess documents from PDF, Word, and Excel files."""
    pdf_docs = load_pdf_documents(path)
    word_docs = load_word_documents(path)
    excel_docs = load_excel_documents(path)
    return pdf_docs + word_docs + excel_docs

def scrape_url(url):
    """Scrape content from a given URL and save it to a text file."""
    try:
        response = requests.get(url)
        response.raise_for_status()  # Ensure we notice bad responses
        soup = BeautifulSoup(response.content, 'html.parser')
        text = soup.get_text()
        # Save the text content to a file for processing
        text_file_path = "data/scraped_content.txt"
        with open(text_file_path, "w") as file:
            file.write(text)
        return text_file_path
    except requests.RequestException as e:
        st.error(f"Error fetching the URL: {e}")
        return None

def process_document(path, input_query):
    """Process the document by setting up the chain and invoking it with the input query."""
    documents = load_documents(path)
    
    if not documents:
        st.error("No documents found. Please check the uploaded files or scraped content.")
        return "No documents found."

    text_splitter = CharacterTextSplitter(chunk_size=18000, chunk_overlap=10)
    split_docs = text_splitter.split_documents(documents)
    
    if not split_docs:
        st.error("No text could be extracted from the documents.")
        return "No text could be extracted."

    llm_model = configure_model()
    prompt = generate_prompt()
    retriever = configure_retriever(split_docs)
    chain = create_chain(retriever, prompt, llm_model)
    response = inference(chain, input_query)
    return response

def main():
    """Main function to run the Streamlit app."""
    tmp_folder = '/tmp/1'
    os.makedirs(tmp_folder, exist_ok=True)

    st.title("Q&A Document AI RAG Chatbot")

    uploaded_files = st.sidebar.file_uploader("Choose PDF, Word, or Excel files", accept_multiple_files=True, type=['pdf', 'docx', 'xlsx'])
    if uploaded_files:
        for file in uploaded_files:
            with open(os.path.join(tmp_folder, file.name), 'wb') as f:
                f.write(file.getbuffer())
        st.success('Files successfully uploaded. Start prompting!')

    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []

    if uploaded_files:
        with st.form(key='question_form'):
            user_query = st.text_input("Ask a question:", key="query_input")
            if st.form_submit_button("Ask") and user_query:
                response = process_document(tmp_folder, user_query)
                if response:  # Check if response is not empty
                    st.session_state.chat_history.append({"question": user_query, "answer": response})

        if st.button("Clear Chat History"):
            st.session_state.chat_history = []

        for chat in st.session_state.chat_history:
            st.markdown(f"**Q:** {chat['question']}")
            st.markdown(f"**A:** {chat['answer']}")
            st.markdown("---")
    else:
        st.success('Upload Documents to Start Processing!')

    url_input = st.sidebar.text_input("Or enter a URL to scrape content from:")
    if st.sidebar.button("Scrape URL"):
        if url_input:
            file_path = scrape_url(url_input)
            if file_path:
                documents = load_documents(tmp_folder)
                if documents:  # Check if documents are loaded after scraping
                    response = process_document(tmp_folder, "What is the content of the URL?")
                    if response:  # Check if response is not empty
                        st.session_state.chat_history.append({"question": "What is the content of the URL?", "answer": response})
                        st.success("URL content processed successfully!")
                else:
                    st.error("Failed to load any documents from the scraped URL content.")
            else:
                st.error("Failed to process URL content.")
        else:
            st.warning("Please enter a valid URL.")

    if st.sidebar.button("REMOVE UPLOADED FILES"):
        document_count = os.listdir(tmp_folder)
        if len(document_count) > 0:
            shutil.rmtree(tmp_folder)
            st.sidebar.write("FILES DELETED SUCCESSFULLY!")
        else:
            st.sidebar.write("NO DOCUMENT FOUND TO DELETE! PLEASE UPLOAD DOCUMENTS TO START PROCESS!")

if __name__ == "__main__":
    main()