Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,62 +12,60 @@ from dotenv import load_dotenv
|
|
12 |
import os
|
13 |
load_dotenv()
|
14 |
|
15 |
-
|
16 |
-
groq_api_key
|
17 |
-
os.environ["GOOGLE_API_KEY"]
|
18 |
|
19 |
st.title("Gemma Model Document Q&A")
|
20 |
|
21 |
-
llm
|
|
|
22 |
|
23 |
-
prompt
|
24 |
"""
|
25 |
Answer the questions based on the provided context only.
|
26 |
-
Please provide the most accurate response based on the question
|
27 |
<context>
|
28 |
{context}
|
29 |
<context>
|
30 |
-
Questions:
|
|
|
31 |
"""
|
32 |
)
|
33 |
|
34 |
-
def vector_embedding(
|
|
|
35 |
if "vectors" not in st.session_state:
|
36 |
-
st.session_state.embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
37 |
-
|
38 |
-
# Save the uploaded files and load them
|
39 |
-
with open("uploaded_files.zip", "wb") as f:
|
40 |
-
f.write(uploaded_files.getbuffer())
|
41 |
-
|
42 |
-
# Extract the uploaded files
|
43 |
-
os.system("unzip -o uploaded_files.zip -d ./uploaded_data")
|
44 |
-
|
45 |
-
st.session_state.loader = PyPDFDirectoryLoader("./uploaded_data") # Data Ingestion
|
46 |
-
st.session_state.docs = st.session_state.loader.load() # Document Loading
|
47 |
-
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunk Creation
|
48 |
-
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs) # Splitting
|
49 |
-
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings) # Vector OpenAI embeddings
|
50 |
-
|
51 |
-
uploaded_files = st.file_uploader("Upload Your PDF Files", accept_multiple_files=True, type=["pdf"])
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
st.
|
57 |
-
|
58 |
-
st.
|
|
|
|
|
|
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
import time
|
63 |
|
|
|
|
|
64 |
if prompt1:
|
65 |
-
document_chain
|
66 |
-
retriever
|
67 |
-
retrieval_chain
|
68 |
-
start
|
69 |
-
response
|
70 |
-
|
71 |
st.write(response['answer'])
|
72 |
|
73 |
# With a streamlit expander
|
@@ -75,4 +73,4 @@ if prompt1:
|
|
75 |
# Find the relevant chunks
|
76 |
for i, doc in enumerate(response["context"]):
|
77 |
st.write(doc.page_content)
|
78 |
-
st.write("--------------------------------")
|
|
|
12 |
import os
|
13 |
load_dotenv()
|
14 |
|
15 |
+
## load the GROQ And OpenAI API KEY
|
16 |
+
groq_api_key=os.getenv('GROQ_API_KEY')
|
17 |
+
os.environ["GOOGLE_API_KEY"]=os.getenv("GOOGLE_API_KEY")
|
18 |
|
19 |
st.title("Gemma Model Document Q&A")
|
20 |
|
21 |
+
llm=ChatGroq(groq_api_key=groq_api_key,
|
22 |
+
model_name="Llama3-8b-8192")
|
23 |
|
24 |
+
prompt=ChatPromptTemplate.from_template(
|
25 |
"""
|
26 |
Answer the questions based on the provided context only.
|
27 |
+
Please provide the most accurate response based on the question
|
28 |
<context>
|
29 |
{context}
|
30 |
<context>
|
31 |
+
Questions:{input}
|
32 |
+
|
33 |
"""
|
34 |
)
|
35 |
|
36 |
+
def vector_embedding():
|
37 |
+
|
38 |
if "vectors" not in st.session_state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
st.session_state.embeddings=GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
41 |
+
st.session_state.loader=PyPDFDirectoryLoader("./us_census") ## Data Ingestion
|
42 |
+
st.session_state.docs=st.session_state.loader.load() ## Document Loading
|
43 |
+
st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200) ## Chunk Creation
|
44 |
+
st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) #splitting
|
45 |
+
st.session_state.vectors=FAISS.from_documents(st.session_state.final_documents,st.session_state.embeddings) #vector OpenAI embeddings
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
|
50 |
+
|
51 |
+
prompt1=st.text_input("Enter Your Question From Doduments")
|
52 |
+
|
53 |
+
|
54 |
+
if st.button("Documents Embedding"):
|
55 |
+
vector_embedding()
|
56 |
+
st.write("Vector Store DB Is Ready")
|
57 |
|
58 |
import time
|
59 |
|
60 |
+
|
61 |
+
|
62 |
if prompt1:
|
63 |
+
document_chain=create_stuff_documents_chain(llm,prompt)
|
64 |
+
retriever=st.session_state.vectors.as_retriever()
|
65 |
+
retrieval_chain=create_retrieval_chain(retriever,document_chain)
|
66 |
+
start=time.process_time()
|
67 |
+
response=retrieval_chain.invoke({'input':prompt1})
|
68 |
+
print("Response time :",time.process_time()-start)
|
69 |
st.write(response['answer'])
|
70 |
|
71 |
# With a streamlit expander
|
|
|
73 |
# Find the relevant chunks
|
74 |
for i, doc in enumerate(response["context"]):
|
75 |
st.write(doc.page_content)
|
76 |
+
st.write("--------------------------------")
|