File size: 5,597 Bytes
167d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9b4541
 
 
 
 
 
 
167d6d1
2d9c151
 
 
f9b4541
167d6d1
2d9c151
167d6d1
f9b4541
 
167d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c040d4
 
 
997e037
 
 
0c040d4
 
 
 
 
167d6d1
0c040d4
167d6d1
0c040d4
 
 
 
 
 
1290ade
0c040d4
 
 
 
997e037
 
 
 
167d6d1
997e037
 
 
 
 
 
 
 
0c040d4
 
 
 
 
 
997e037
0c040d4
 
 
 
997e037
 
 
167d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
from langchain_groq import ChatGroq
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import create_retrieval_chain
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from dotenv import load_dotenv
from  PDFprocess_sample import process_pdf

# Loading GROQ and Google API
load_dotenv()

GROQ_API_KEY = os.getenv('GROQ_API_KEY')
os.environ["GOOGLE_API_KEY"]= os.getenv('GOOGLE_API_KEY')

#Loading CSS files

# def load_css(file_name):
#     with open(file_name) as f:
#         css = f.read()
#     st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)

import os

def load_css(file_name):
    current_dir = os.path.dirname(__file__)
    file_path = os.path.join(current_dir, file_name)
    with open(file_path, "r") as f:
        st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

load_css("CSS/style.css")



#setting up LLM
llm = ChatGroq(
    api_key=GROQ_API_KEY,
    model_name="Llama3-8b-8192"
)


prompt = ChatPromptTemplate.from_template(
    """
    Answer the questions based on the provided context only.
    Please provide the most accurate response based on the question. Try to answer in detail in 1500 words
    <context>
    {context}
    <context>
    Questions: {input}
    """
)

input_method = st.sidebar.selectbox("Choose a method" , ["Choose input method...","Interact with Doc", "Get Ques from Doc"])



st.sidebar.title("Upload your pdf")

main_placeholder = st.empty()
# #Document upload
# uploaded_file = st.sidebar.file_uploader("_____________________________________", type="pdf", accept_multiple_files=True)
# st.sidebar.write("Press Submit to process:")



# process = st.sidebar.button("Submit")

uploaded_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)


process = st.sidebar.button("Submit")
# Document processing

if process:
    if uploaded_files:
        for uploaded_file in uploaded_files:
            file_path = f"/tmp/{uploaded_file.name}"
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())
            st.write(f"Processing file: {file_path}")
            st.success(f"{uploaded_file.name} uploaded successfully!")
            process_pdf(file_path)
    else:
        st.warning("Please upload at least one PDF file.")




#Document processing to convert it into vectors
# if process:
#     if uploaded_file:
#         # Process the uploaded PDF file
#         process_pdf(uploaded_file)
#     else:
#         st.warning("Please upload a PDF file.")

# Document processing
# if process:
#     if uploaded_file:
#         # Save to /tmp/ before processing
#         file_path = f"/tmp/{uploaded_file.name}"
#         with open(file_path, "wb") as f:
#             f.write(uploaded_file.getbuffer())
        
#         # Call your existing logic with the saved path
#         process_pdf(file_path)
#     else:
#         st.warning("Please upload a PDF file.")



        
if input_method == "Choose input method...":
    st.title(f"Welcome You all!")
    st.title("Choose an option in the sidebar")
    st.title("Now, let's get started!") 
    
        
#If User wants to interact with the document
elif input_method == "Interact with Doc":
    st.title(f"let's Interact with pdf's")

    prompt1 = st.text_input("______", placeholder="Enter your Question")


    # Generate response if question is entered
    if prompt1 and "vectors" in st.session_state:
        document_chain = create_stuff_documents_chain(llm, prompt)
        retriever = st.session_state.vectors.as_retriever()
        retrieval_chain = create_retrieval_chain(retriever, document_chain)
        
        
        response = retrieval_chain.invoke({'input': prompt1})

        # st.write(response['answer'])
        
        #Get the respose in the card
        st.markdown(
        f"""
        <div class="card">
            <div class="response">{response['answer']}</div>
        </div>
        """,
        unsafe_allow_html=True,
    )
        
        
        
#When User wants to get questions from the doc based on certain topic
elif input_method == "Get Ques from Doc":
    st.title(f"Let's Get Ques from Document")
    
    prompt2 = """Based on the topic of {topic}, 
                kindly provide a comprehensive list of all possible questions that could arise. 
                For each question, provide detailed and explanatory answers in atleast 1000 words detail based on the context,
                ensuring that the responses are as informative as possible.
                make sure you strictly follow the {topic}"""
    topic = st.text_input("Enter a topic", placeholder="What is your topic")
    
    # Generate response if question is entered
    if topic and "vectors" in st.session_state:
        document_chain = create_stuff_documents_chain(llm, prompt)
        retriever = st.session_state.vectors.as_retriever()
        retrieval_chain = create_retrieval_chain(retriever, document_chain)
        
        
        response = retrieval_chain.invoke({'input': prompt2})
        
        #Get the respose in the card
        st.markdown(
        f"""
        <div class="card">
            <div class="response">{response['answer']}</div>
        </div>
        """,
        unsafe_allow_html=True,
    )