File size: 8,953 Bytes
21b6496
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f484d8
886ea8e
 
 
 
 
 
 
 
3cead23
886ea8e
 
8f484d8
886ea8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21b6496
886ea8e
 
 
21b6496
217d71b
21b6496
 
 
217d71b
cb5957f
8e3e411
886ea8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 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)

# 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")
# st.sidebar.write("Press Submit to process:")
# process = st.sidebar.button("Submit")

# # 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.")

# if process:
#     if uploaded_file is not None:
#         file_bytes = uploaded_file.read()
#         process_pdf(file_bytes)
#     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,
#     )


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)

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")

#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.")
        
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,
    )