arnabbumba077 commited on
Commit
c35ea24
·
verified ·
1 Parent(s): e985130

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -108
app.py DELETED
@@ -1,108 +0,0 @@
1
- import streamlit as st
2
- import os
3
- from langchain_groq import ChatGroq
4
- from langchain_huggingface import HuggingFaceEmbeddings
5
- from langchain.text_splitter import RecursiveCharacterTextSplitter
6
- from langchain.chains.combine_documents import create_stuff_documents_chain
7
- from langchain_core.prompts import ChatPromptTemplate
8
- from langchain.chains import create_retrieval_chain
9
- from langchain_community.vectorstores import FAISS
10
- from langchain_community.document_loaders import PyPDFLoader
11
- from dotenv import load_dotenv
12
- import tempfile
13
-
14
-
15
- load_dotenv()
16
-
17
-
18
- groq_api_key = os.getenv('GROQ_API_KEY')
19
-
20
-
21
- st.markdown("<h2 style='text-align: center;'>PDF Insights: Interactive Q&A Chatbot with Groq API</h2>", unsafe_allow_html=True)
22
-
23
-
24
- llm = ChatGroq(groq_api_key=groq_api_key, model_name="Llama3-8b-8192")
25
-
26
-
27
- prompt = ChatPromptTemplate.from_template(
28
- """
29
- Answer the questions based on the provided context only.
30
- Please provide the most accurate response based on the question.
31
- If the answer is not in the document, just say "Please Contact the Business Directly". Dont say wrong answer.
32
- <context>
33
- {context}
34
- <context>
35
- Questions: {input}
36
- """
37
- )
38
-
39
- def create_vector_db_out_of_the_uploaded_pdf_file(pdf_file):
40
-
41
-
42
- if "vector_store" not in st.session_state:
43
-
44
- with tempfile.NamedTemporaryFile(delete=False) as temp_file:
45
-
46
- temp_file.write(pdf_file.read())
47
-
48
- pdf_file_path = temp_file.name
49
-
50
- st.session_state.embeddings = HuggingFaceEmbeddings(model_name='BAAI/bge-small-en-v1.5', model_kwargs={'device': 'cpu'}, encode_kwargs={'normalize_embeddings': True})
51
-
52
- st.session_state.loader = PyPDFLoader(pdf_file_path)
53
-
54
- st.session_state.text_document_from_pdf = st.session_state.loader.load()
55
-
56
- st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
57
-
58
- st.session_state.final_document_chunks = st.session_state.text_splitter.split_documents(st.session_state.text_document_from_pdf)
59
-
60
- st.session_state.vector_store = FAISS.from_documents(st.session_state.final_document_chunks, st.session_state.embeddings)
61
-
62
-
63
- pdf_input_from_user = st.file_uploader("Upload the PDF file", type=['pdf'])
64
-
65
-
66
- if pdf_input_from_user is not None:
67
-
68
- if st.button("Create the Vector DB from the uploaded PDF file"):
69
-
70
- if pdf_input_from_user is not None:
71
-
72
- create_vector_db_out_of_the_uploaded_pdf_file(pdf_input_from_user)
73
-
74
- st.success("Vector Store DB for this PDF file Is Ready")
75
-
76
- else:
77
-
78
- st.write("Please upload a PDF file first")
79
-
80
-
81
-
82
- if "vector_store" in st.session_state:
83
-
84
- user_prompt = st.text_input("Enter Your Question related to the uploaded PDF")
85
-
86
- if st.button('Submit Prompt'):
87
-
88
- if user_prompt:
89
-
90
- if "vector_store" in st.session_state:
91
-
92
- document_chain = create_stuff_documents_chain(llm, prompt)
93
-
94
- retriever = st.session_state.vector_store.as_retriever()
95
-
96
- retrieval_chain = create_retrieval_chain(retriever, document_chain)
97
-
98
- response = retrieval_chain.invoke({'input': user_prompt})
99
-
100
- st.write(response['answer'])
101
-
102
- else:
103
-
104
- st.write("Please embed the document first by uploading a PDF file.")
105
-
106
- else:
107
-
108
- st.error('Please write your prompt')