Spaces:
Sleeping
Sleeping
BLJohnPrabhasith
commited on
Upload 2 files
Browse filesInitial_commit_01
- main.py +102 -0
- requirements.txt +10 -0
main.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.document_loaders import PyPDFLoader
|
2 |
+
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
|
3 |
+
from langchain.storage import LocalFileStore
|
4 |
+
from langchain.embeddings import CacheBackedEmbeddings
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
from langchain_groq import ChatGroq
|
7 |
+
from langchain_core.runnables import RunnablePassthrough
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
9 |
+
from langchain_core.output_parsers import StrOutputParser
|
10 |
+
import streamlit as st
|
11 |
+
import os
|
12 |
+
import shutil
|
13 |
+
from dotenv import load_dotenv
|
14 |
+
from streamlit_chat import message
|
15 |
+
|
16 |
+
load_dotenv()
|
17 |
+
|
18 |
+
os.environ['GROQ_API_KEY'] = os.getenv('GROQ_API')
|
19 |
+
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
20 |
+
os.environ["LANGCHAIN_API_KEY"] = os.getenv('LANGSMITH_API')
|
21 |
+
|
22 |
+
UPLOAD_DIR = "uploaded_files"
|
23 |
+
|
24 |
+
def cleanup_files():
|
25 |
+
if os.path.isdir(UPLOAD_DIR):
|
26 |
+
shutil.rmtree(UPLOAD_DIR, ignore_errors=True)
|
27 |
+
if 'file_handle' in st.session_state:
|
28 |
+
st.session_state.file_handle.close()
|
29 |
+
|
30 |
+
if 'cleanup_done' not in st.session_state:
|
31 |
+
st.session_state.cleanup_done = False
|
32 |
+
|
33 |
+
if not st.session_state.cleanup_done:
|
34 |
+
cleanup_files()
|
35 |
+
|
36 |
+
if not os.path.exists(UPLOAD_DIR):
|
37 |
+
os.makedirs(UPLOAD_DIR)
|
38 |
+
|
39 |
+
st.title("Chat with Your PDF!!")
|
40 |
+
uploaded_file = st.file_uploader("Upload a file")
|
41 |
+
|
42 |
+
if uploaded_file is not None:
|
43 |
+
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
44 |
+
file_path = os.path.abspath(file_path)
|
45 |
+
|
46 |
+
with open(file_path, 'wb') as f:
|
47 |
+
f.write(uploaded_file.getbuffer())
|
48 |
+
st.write("You're Ready For a Chat with your PDF")
|
49 |
+
|
50 |
+
docs = PyPDFLoader(file_path).load_and_split()
|
51 |
+
|
52 |
+
embedding = HuggingFaceBgeEmbeddings(
|
53 |
+
model_name='BAAI/llm-embedder',
|
54 |
+
)
|
55 |
+
|
56 |
+
store = LocalFileStore("./cache/")
|
57 |
+
cached_embedder = CacheBackedEmbeddings.from_bytes_store(
|
58 |
+
embedding, store, namespace='embeddings'
|
59 |
+
)
|
60 |
+
|
61 |
+
vector_base = FAISS.from_documents(
|
62 |
+
docs,
|
63 |
+
embedding
|
64 |
+
)
|
65 |
+
|
66 |
+
template = '''You are an Experienced Business Person Having a
|
67 |
+
great Knowledge About Various Types of Business Activities
|
68 |
+
but here you are hired to Give the answers to the {question} only based on {context}
|
69 |
+
.if you are unaware of the question Just reply it with I\'m Unaware of your Query.
|
70 |
+
Use three sentences maximum and keep the answer concise.'''
|
71 |
+
|
72 |
+
prompt = ChatPromptTemplate.from_template(template)
|
73 |
+
retriever = vector_base.as_retriever()
|
74 |
+
|
75 |
+
llm = ChatGroq(
|
76 |
+
model='mixtral-8x7b-32768',
|
77 |
+
temperature=0,
|
78 |
+
)
|
79 |
+
|
80 |
+
if 'history' not in st.session_state:
|
81 |
+
st.session_state.history = []
|
82 |
+
|
83 |
+
query = st.text_input("Enter your question")
|
84 |
+
|
85 |
+
if st.button("Submit !"):
|
86 |
+
if query:
|
87 |
+
chain = (
|
88 |
+
{'context': retriever, 'question': RunnablePassthrough()}
|
89 |
+
| prompt | llm | StrOutputParser()
|
90 |
+
)
|
91 |
+
answer = chain.invoke(query)
|
92 |
+
st.session_state.history.append({'question': query, 'answer': answer})
|
93 |
+
|
94 |
+
if st.session_state.history:
|
95 |
+
st.write("### Previous Questions and Answers")
|
96 |
+
for idx, entry in enumerate(st.session_state.history):
|
97 |
+
message(f"**Q{idx + 1}:** {entry['question']}", is_table=True, key=f"question_{idx}",avatar_style='no-avatar')
|
98 |
+
message(f"**A{idx + 1}:** {entry['answer']}", is_table=True, key=f"answer_{idx}",avatar_style='no-avatar')
|
99 |
+
|
100 |
+
|
101 |
+
if st.session_state.cleanup_done:
|
102 |
+
cleanup_files()
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv
|
2 |
+
streamlit
|
3 |
+
langchain-google-genai
|
4 |
+
langchain-core
|
5 |
+
langchain-community
|
6 |
+
pymupdf
|
7 |
+
faiss-cpu
|
8 |
+
langchain-groq
|
9 |
+
langsmith
|
10 |
+
streamlit-chat
|