Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import streamlit as st
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
from langchain.document_loaders import PyPDFLoader
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
8 |
+
from langchain.vectorstores import FAISS
|
9 |
+
from langchain.chains import RetrievalQA
|
10 |
+
|
11 |
+
# Load environment variables from .env file
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
def main():
|
15 |
+
# Retrieve API key from environment variables
|
16 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
17 |
+
|
18 |
+
# Verify API key is loaded
|
19 |
+
if not groq_api_key:
|
20 |
+
st.error("GROQ API Key not found. Please check your .env file.")
|
21 |
+
return
|
22 |
+
|
23 |
+
st.title("PDF Chat with Groq LLM")
|
24 |
+
|
25 |
+
# File uploader
|
26 |
+
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
27 |
+
|
28 |
+
if uploaded_file is not None:
|
29 |
+
# Save the uploaded PDF temporarily
|
30 |
+
with open("temp.pdf", "wb") as f:
|
31 |
+
f.write(uploaded_file.getbuffer())
|
32 |
+
|
33 |
+
# Load the PDF
|
34 |
+
loader = PyPDFLoader("temp.pdf")
|
35 |
+
pages = loader.load()
|
36 |
+
|
37 |
+
# Split the text
|
38 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
39 |
+
chunk_size=1000,
|
40 |
+
chunk_overlap=200
|
41 |
+
)
|
42 |
+
texts = text_splitter.split_documents(pages)
|
43 |
+
|
44 |
+
# Create embeddings
|
45 |
+
embeddings = HuggingFaceEmbeddings(
|
46 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Create vector store
|
50 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
51 |
+
|
52 |
+
# Initialize Groq LLM with API key
|
53 |
+
llm = ChatGroq(
|
54 |
+
temperature=0.7,
|
55 |
+
model_name='llama3-70b-8192',
|
56 |
+
api_key=groq_api_key
|
57 |
+
)
|
58 |
+
|
59 |
+
# Create QA chain
|
60 |
+
qa_chain = RetrievalQA.from_chain_type(
|
61 |
+
llm=llm,
|
62 |
+
chain_type="stuff",
|
63 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
|
64 |
+
)
|
65 |
+
|
66 |
+
# Chat input
|
67 |
+
query = st.text_input("Ask a question about the PDF:")
|
68 |
+
|
69 |
+
if query:
|
70 |
+
# Get response
|
71 |
+
response = qa_chain.invoke(query)
|
72 |
+
st.write("Response:", response['result'])
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
main()
|