Spaces:
Sleeping
Sleeping
File size: 9,356 Bytes
4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 f9b4541 4bcde7e f9b4541 167d6d1 4bcde7e 167d6d1 4bcde7e f9b4541 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 997e037 4bcde7e 997e037 4bcde7e 997e037 0c040d4 4bcde7e 167d6d1 4bcde7e 997e037 4bcde7e 997e037 4bcde7e 997e037 4bcde7e 997e037 4bcde7e 69e8625 4bcde7e c57b02b 9eede26 beccddc 69e8625 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e 167d6d1 4bcde7e |
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 274 275 276 277 278 279 |
# 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,
# )
import os
import streamlit as st
from langchain.chains import RetrievalQA
from langchain_google_genai import ChatGoogleGenerativeAI
from src.PDFprocess_sample.py import process_pdf_from_path
from langchain.vectorstores import FAISS
import faiss
# Set up the page
st.set_page_config(page_title="Chat with your PDF", layout="wide")
# CSS Styling
with open("src/CSS/style.css") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
st.markdown("<h1 class='main-heading'>Chat with your PDF using Gemini AI</h1>", unsafe_allow_html=True)
# Sidebar Upload
st.sidebar.markdown("<h2 style='color: white;'>Upload PDFs</h2>", unsafe_allow_html=True)
uploaded_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)
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.sidebar.success(f"{uploaded_file.name} uploaded successfully!")
st.write(f"Processing file: {uploaded_file.name}")
process_pdf_from_path(file_path)
# Select input method
input_method = st.sidebar.selectbox("Choose input method...", ["Choose input method...", "Interact with Doc", "Get Ques from Doc"])
# Initialize LLM
llm = ChatGoogleGenerativeAI(model="gemini-pro")
if input_method == "Choose input method...":
st.title("Welcome You all!")
st.subheader("Choose an option in the sidebar")
st.write("Now, let's get started!")
elif input_method == "Interact with Doc":
st.title("Let's Interact with the PDF")
prompt1 = st.text_input("Ask a question", placeholder="Enter your Question")
if prompt1 and "vectors" in st.session_state:
document_chain = create_stuff_documents_chain(llm, prompt="Answer the question based on the document.")
retriever = st.session_state.vectors.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)
response = retrieval_chain.invoke({'input': prompt1})
st.markdown(
f"""
<div class="card">
<div class="response">{response['answer']}</div>
</div>
""",
unsafe_allow_html=True,
)
elif input_method == "Get Ques from Doc":
st.title("Let's Generate Questions from the Document")
topic = st.text_input("Enter a topic", placeholder="What is your topic?")
if topic and "vectors" in st.session_state:
prompt2 = f"""
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 at least 1000 words,
ensuring that the responses are as informative as possible.
Make sure you strictly follow the topic of {topic}.
"""
document_chain = create_stuff_documents_chain(llm, prompt="Generate questions and answers based on the topic and document.")
retriever = st.session_state.vectors.as_retriever()
retrieval_chain = create_retrieval_chain(retriever, document_chain)
response = retrieval_chain.invoke({'input': prompt2})
st.markdown(
f"""
<div class="card">
<div class="response">{response['answer']}</div>
</div>
""",
unsafe_allow_html=True,
)
|