Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
from PyPDF2 import PdfReader
|
4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
@@ -8,24 +7,49 @@ from langchain.vectorstores import FAISS
|
|
8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
9 |
from langchain.chains.question_answering import load_qa_chain
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
-
import os
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
|
|
|
|
|
17 |
|
18 |
#os.mkdir('faiss_index')
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
# Install packages
|
27 |
-
for package in packages:
|
28 |
-
subprocess.call(['pip', 'install', package])
|
29 |
|
30 |
def get_pdf_text(pdf_docs):
|
31 |
text = ""
|
@@ -36,7 +60,7 @@ def get_pdf_text(pdf_docs):
|
|
36 |
return text
|
37 |
|
38 |
def get_text_chunks(text):
|
39 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=
|
40 |
chunks = text_splitter.split_text(text)
|
41 |
return chunks
|
42 |
|
@@ -59,6 +83,31 @@ def get_conversational_chain():
|
|
59 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
60 |
return chain
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
def user_input(user_question, api_key):
|
63 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
64 |
new_db = FAISS.load_local("faiss_index", embeddings)
|
@@ -67,18 +116,23 @@ def user_input(user_question, api_key):
|
|
67 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
68 |
st.write("Reply: ", response["output_text"])
|
69 |
|
|
|
|
|
|
|
|
|
70 |
def main():
|
71 |
-
st.header("RAG based LLM
|
72 |
|
73 |
user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
|
74 |
|
75 |
-
if user_question and api_key:
|
76 |
user_input(user_question, api_key)
|
77 |
|
78 |
with st.sidebar:
|
79 |
st.title("Menu:")
|
|
|
80 |
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True, key="pdf_uploader")
|
81 |
-
if st.button("Submit & Process", key="process_button") and api_key:
|
82 |
with st.spinner("Processing..."):
|
83 |
raw_text = get_pdf_text(pdf_docs)
|
84 |
text_chunks = get_text_chunks(raw_text)
|
@@ -86,4 +140,4 @@ def main():
|
|
86 |
st.success("Done")
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
-
main()
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from PyPDF2 import PdfReader
|
3 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
|
|
7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
from langchain.chains.question_answering import load_qa_chain
|
9 |
from langchain.prompts import PromptTemplate
|
10 |
+
import os
|
11 |
+
import json
|
12 |
|
13 |
+
####CREDIT#####
|
14 |
+
#Credit to author (Sri Laxmi) of oringal code reference: SriLaxmi1993
|
15 |
+
#Sri LaxmiGithub Link: https://github.com/SriLaxmi1993/Document-Genie-using-RAG-Framwork
|
16 |
+
#Sri Laxmi Youtube:https://www.youtube.com/watch?v=SkY2u4UUr6M&t=112s
|
17 |
+
###############
|
18 |
|
19 |
+
st.set_page_config(page_title="Gemini RAG", layout="wide")
|
20 |
|
21 |
+
# This is the first API key input; no need to repeat it in the main function.
|
22 |
+
api_key = 'AIzaSyCvXRggpO2yNwIpZmoMy_5Xhm2bDyD-pOo'
|
23 |
|
24 |
#os.mkdir('faiss_index')
|
25 |
|
26 |
+
#empty faise_index and chat_history.json
|
27 |
+
def delete_files_in_folder(folder_path):
|
28 |
+
try:
|
29 |
+
# Iterate over all the files in the folder
|
30 |
+
chat_history_file = "chat_history.json"
|
31 |
+
if os.path.exists(chat_history_file):
|
32 |
+
os.remove(chat_history_file)
|
33 |
+
for file_name in os.listdir(folder_path):
|
34 |
+
file_path = os.path.join(folder_path, file_name)
|
35 |
+
if os.path.isfile(file_path): # Check if it's a file
|
36 |
+
os.remove(file_path) # Delete the file
|
37 |
+
print(f"Deleted file: {file_path}")
|
38 |
+
print("All files within the folder have been deleted successfully!")
|
39 |
+
except Exception as e:
|
40 |
+
print(f"An error occurred: {e}")
|
41 |
+
|
42 |
+
|
43 |
+
with st.sidebar:
|
44 |
+
st.title("Menu:")
|
45 |
|
46 |
+
if st.button("Reset Files", key="reset_button"):
|
47 |
+
folder_path = 'faiss_index'
|
48 |
+
delete_files_in_folder(folder_path)
|
49 |
+
|
50 |
+
CH_size = st.slider("Chunk Size", 0, 1000, 450)
|
51 |
+
CH_overlap = st.slider("Chunk Overlap", 0, 1000, 50)
|
52 |
|
|
|
|
|
|
|
53 |
|
54 |
def get_pdf_text(pdf_docs):
|
55 |
text = ""
|
|
|
60 |
return text
|
61 |
|
62 |
def get_text_chunks(text):
|
63 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=CH_size, chunk_overlap=CH_overlap)
|
64 |
chunks = text_splitter.split_text(text)
|
65 |
return chunks
|
66 |
|
|
|
83 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
84 |
return chain
|
85 |
|
86 |
+
#chat history functionality
|
87 |
+
def update_chat_history(question, reply):
|
88 |
+
# Check if chat history file exists
|
89 |
+
chat_history_file = "chat_history.json"
|
90 |
+
if os.path.exists(chat_history_file):
|
91 |
+
# If file exists, load existing chat history
|
92 |
+
with open(chat_history_file, "r") as file:
|
93 |
+
chat_history = json.load(file)
|
94 |
+
else:
|
95 |
+
# If file doesn't exist, initialize chat history
|
96 |
+
chat_history = {"conversations": []}
|
97 |
+
|
98 |
+
# Add current conversation to chat history
|
99 |
+
chat_history["conversations"].append({"question": question, "reply": reply})
|
100 |
+
|
101 |
+
# Write updated chat history back to file
|
102 |
+
with open(chat_history_file, "w") as file:
|
103 |
+
json.dump(chat_history, file, indent=4)
|
104 |
+
# Display chat history
|
105 |
+
st.subheader("Chat History")
|
106 |
+
for conversation in chat_history["conversations"]:
|
107 |
+
st.write(f"**Question:** {conversation['question']}")
|
108 |
+
st.write(f"**Reply:** {conversation['reply']}")
|
109 |
+
st.write("---")
|
110 |
+
|
111 |
def user_input(user_question, api_key):
|
112 |
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
|
113 |
new_db = FAISS.load_local("faiss_index", embeddings)
|
|
|
116 |
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
117 |
st.write("Reply: ", response["output_text"])
|
118 |
|
119 |
+
#chat history
|
120 |
+
update_chat_history(user_question, response["output_text"])
|
121 |
+
|
122 |
+
|
123 |
def main():
|
124 |
+
st.header("RAG based LLM Application")
|
125 |
|
126 |
user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
|
127 |
|
128 |
+
if user_question and api_key:
|
129 |
user_input(user_question, api_key)
|
130 |
|
131 |
with st.sidebar:
|
132 |
st.title("Menu:")
|
133 |
+
|
134 |
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True, key="pdf_uploader")
|
135 |
+
if st.button("Submit & Process", key="process_button") and api_key:
|
136 |
with st.spinner("Processing..."):
|
137 |
raw_text = get_pdf_text(pdf_docs)
|
138 |
text_chunks = get_text_chunks(raw_text)
|
|
|
140 |
st.success("Done")
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
+
main()
|