Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,60 +12,62 @@ from dotenv import load_dotenv
|
|
| 12 |
import os
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if st.button("Documents Embedding"):
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 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 |
-
|
| 69 |
st.write(response['answer'])
|
| 70 |
|
| 71 |
# With a streamlit expander
|
|
@@ -73,4 +75,4 @@ if prompt1:
|
|
| 73 |
# Find the relevant chunks
|
| 74 |
for i, doc in enumerate(response["context"]):
|
| 75 |
st.write(doc.page_content)
|
| 76 |
-
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, model_name="Llama3-8b-8192")
|
|
|
|
| 22 |
|
| 23 |
+
prompt = ChatPromptTemplate.from_template(
|
| 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: {input}
|
|
|
|
| 31 |
"""
|
| 32 |
)
|
| 33 |
|
| 34 |
+
def vector_embedding(uploaded_files):
|
|
|
|
| 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 |
if st.button("Documents Embedding"):
|
| 54 |
+
if uploaded_files:
|
| 55 |
+
vector_embedding(uploaded_files[0])
|
| 56 |
+
st.write("Vector Store DB Is Ready")
|
| 57 |
+
else:
|
| 58 |
+
st.write("Please upload PDF files.")
|
| 59 |
|
| 60 |
+
prompt1 = st.text_input("Enter Your Question From Documents")
|
| 61 |
|
| 62 |
+
import time
|
| 63 |
|
| 64 |
if prompt1:
|
| 65 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
| 66 |
+
retriever = st.session_state.vectors.as_retriever()
|
| 67 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
| 68 |
+
start = time.process_time()
|
| 69 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
| 70 |
+
st.write(f"Response time: {time.process_time() - start} seconds")
|
| 71 |
st.write(response['answer'])
|
| 72 |
|
| 73 |
# With a streamlit expander
|
|
|
|
| 75 |
# Find the relevant chunks
|
| 76 |
for i, doc in enumerate(response["context"]):
|
| 77 |
st.write(doc.page_content)
|
| 78 |
+
st.write("--------------------------------")
|