Spaces:
Sleeping
Sleeping
tensorkelechi
commited on
Commit
•
7578e1e
1
Parent(s):
6993f7f
Update app.py
Browse files
app.py
CHANGED
@@ -21,106 +21,6 @@ st.write(
|
|
21 |
)
|
22 |
|
23 |
|
24 |
-
@st.cache_resource
|
25 |
-
def initialize_resources():
|
26 |
-
llm_gemini = ChatGoogleGenerativeAI(
|
27 |
-
model="gemini-1.5-flash-latest", google_api_key=os.getenv("GOOGLE_API_KEY")
|
28 |
-
)
|
29 |
-
return llm_gemini
|
30 |
-
|
31 |
-
#@st.cache_data
|
32 |
-
def get_retriever(pdf_file):
|
33 |
-
with NamedTemporaryFile(suffix="pdf") as temp:
|
34 |
-
temp.write(pdf_file.getvalue())
|
35 |
-
pdf_loader = PyPDFLoader(temp.name, extract_images=True)
|
36 |
-
pages = pdf_loader.load()
|
37 |
-
|
38 |
-
st.write(f"AI Chatbot for {course_material}")
|
39 |
-
|
40 |
-
underlying_embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
41 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
42 |
-
chunk_size=1000,
|
43 |
-
chunk_overlap=20,
|
44 |
-
length_function=len,
|
45 |
-
is_separator_regex=False,
|
46 |
-
separators="\n",
|
47 |
-
)
|
48 |
-
documents = text_splitter.split_documents(pages)
|
49 |
-
vectorstore = faiss.FAISS.from_documents(documents, underlying_embeddings)
|
50 |
-
doc_retiever = vectorstore.as_retriever()
|
51 |
-
# search_type="mmr", search_kwargs={"k": 5, "fetch_k": 10}
|
52 |
-
#)
|
53 |
-
|
54 |
-
return doc_retiever
|
55 |
-
|
56 |
-
|
57 |
-
chat_model = initialize_resources()
|
58 |
-
|
59 |
-
# Streamlit UI
|
60 |
-
# Course list and pdf retrieval
|
61 |
-
|
62 |
-
courses = ["PMB", "PCL", "Kelechi_research"] # "GSP", "CPM", "PCG", "PCH"
|
63 |
-
course_pdfs = None
|
64 |
-
doc_retriever = None
|
65 |
-
conversational_chain = None
|
66 |
-
|
67 |
-
# course = st.sidebar.selectbox("Choose course", (courses))
|
68 |
-
# docs_path = f"pdfs/{course}"
|
69 |
-
# course_pdfs = os.listdir(docs_path)
|
70 |
-
# pdfs = [os.path.join(docs_path, pdf) for pdf in course_pdfs]
|
71 |
-
|
72 |
-
course_material = "{Not selected}"
|
73 |
-
|
74 |
-
|
75 |
-
# @st.cache_resource
|
76 |
-
def query_response(query, _retriever):
|
77 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
78 |
-
conversational_chain = ConversationalRetrievalChain.from_llm(
|
79 |
-
llm=chat_model, retriever=_retriever, memory=memory, verbose=False
|
80 |
-
)
|
81 |
-
response = conversational_chain.run(query)
|
82 |
-
|
83 |
-
return response
|
84 |
-
|
85 |
-
|
86 |
-
if "doc" not in st.session_state:
|
87 |
-
st.session_state.doc = ""
|
88 |
-
|
89 |
-
course_material = st.file_uploader("or Upload your own pdf", type="pdf")
|
90 |
-
|
91 |
-
if st.session_state != "":
|
92 |
-
try:
|
93 |
-
if st.button("load"):
|
94 |
-
with st.spinner("loading document.."):
|
95 |
-
doc_retriever = get_retriever(course_material)
|
96 |
-
|
97 |
-
st.success("File loading successful, vector db initialize")
|
98 |
-
except Exception as e:
|
99 |
-
st.error(e)
|
100 |
-
|
101 |
-
import os
|
102 |
-
from langchain_community.document_loaders import PyPDFLoader
|
103 |
-
from langchain_community.vectorstores import faiss
|
104 |
-
from langchain.memory import ConversationBufferMemory
|
105 |
-
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
106 |
-
from tempfile import NamedTemporaryFile
|
107 |
-
from dotenv import load_dotenv
|
108 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
109 |
-
from langchain.chains import ConversationalRetrievalChain
|
110 |
-
import streamlit as st
|
111 |
-
import nest_asyncio
|
112 |
-
|
113 |
-
nest_asyncio.apply()
|
114 |
-
load_dotenv()
|
115 |
-
|
116 |
-
# Initialize app resources
|
117 |
-
st.set_page_config(page_title="StudyAssist", page_icon=":book:")
|
118 |
-
st.title("StudyAssist(pharmassist-v0)")
|
119 |
-
st.write(
|
120 |
-
"An AI/RAG application to aid students in their studies, specially optimized for the pharm 028 students. In simpler terms, chat with your pdf"
|
121 |
-
)
|
122 |
-
|
123 |
-
|
124 |
@st.cache_resource
|
125 |
def initialize_resources():
|
126 |
llm_gemini = ChatGoogleGenerativeAI(
|
|
|
21 |
)
|
22 |
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
@st.cache_resource
|
25 |
def initialize_resources():
|
26 |
llm_gemini = ChatGoogleGenerativeAI(
|